与许多其他人一样,我最近开始对自己的Discord机器人进行编程... 到目前为止,我所得到的是这样:
@bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
它不会崩溃,但也不起作用...
答案 0 :(得分:0)
似乎您正在使用旧版本discord.py的教程。 最新版本中有一些big changes-重写。请花一些时间查找更多更新的教程,或阅读上面链接的文档。
以下是同时使用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()
答案 1 :(得分:-1)
因此,首先,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!发送Hello world! args为["Hello", "world!"]
。使用.join
,我们可以将它们连接成一个字符串(message
)