[英]How do I resend user input after a command (e.g. User: !send 1234 Bot: 1234) in discord.py?
我最近开始编写自己的 discord 机器人,就像许多其他人正在做的那样……到目前为止,我得到的是:
@bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
它不会崩溃,但它也不起作用......
好像您正在使用旧版本 discord.py 的教程。 最新版本有一些大的变化——重写。 请花一些时间寻找更多更新的教程,或阅读上面链接的文档。
以下是同时使用on_message
事件和命令的情况:
@bot.command()
async def send(ctx, *, sentence):
await ctx.send(sentence)
######################################
@bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
if message.content.startswith("t!send"):
await message.channel.send(" ".join(args))
else:
await bot.process_commands(message) # only add this if you're also using command decorators
参考:
commands.Context()
- 如果不熟悉使用命令装饰器discord.on_message()
Bot.process_commands()
所以首先,on_message 不属于这里,你也不必使用它。 ( on_message()
将使用装饰器@bot.event
)假设您已经使用bot = commands.Bot(command_prefix = 't!')
为您的机器人设置了前缀,您可以执行以下操作:
@bot.command()
async def send(ctx, *args):
message = " ".join(args)
await ctx.send(message)
*args 是用户在 t.send 之后输入的所有内容: 例如:如果用户输入 t!send Hello world! args 将是["Hello", "world!"]
。 使用.join
我们可以将它们连接成一个字符串( message
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.