将某人的评论传递给机器人功能(Discord

时间:2018-09-05 19:05:03

标签: python bots discord discord.py

是否可以向机器人功能传递消息?像这样:

@bot.command(pass_context=True)
async def message(ctx, user.message):
    if user.message == "xyz":
        await bot.say("Hi")

最好传递实际消息,而不要传递内容相同的字符串。

1 个答案:

答案 0 :(得分:0)

是的,你可以做

@bot.event
async def on_message(message):
  if message.content == "Hi":
    await bot.send_message(message.channel,"Hello")

通过这种方式,您可以使用事件而不是命令,因此,当您在聊天室中输入“ Hi”时,机器人会捕获它并运行方法

如果要将某些内容传递给命令函数,可以像使用常规方法一样进行传递

@bot.command()
async def example(something):
  print(something)

当您执行!example Hello There时,机器人会打印出Hello,因为它从输入中得到的字符串中(由空格分隔)选择了第一个值并将其分配给变量,就像普通程序一样从命令行获取其输入。

要捕获所有元素,可以使用*args

@bot.command()
async def example(*args):
  print(args)

,它将返回您从聊天中提供的所有值的元组。 !example Hello There将打印出('Hello', 'There')