【问题标题】:How to enable more than one command [duplicate]如何启用多个命令[重复]
【发布时间】:2019-12-17 16:12:52
【问题描述】:

我对这种 discord.py 格式相当陌生,我似乎无法让两个命令工作。 例如,我有命令; “你好”和命令“有效”。每当我激活机器人时,它只会响应“有效”或“你好”,而不会同时响应。 有没有办法解决这个问题?

我尝试的很少,因为这个问题对我来说很新,我不知道如何解决它。

这是我用于命令的代码:

 @client.event

 async def on_message(message):

     if message.content.startswith('!hello'):

         messages= ["*tips hat* G'day ", "Yeehaw pardner,  ", "Howdy, ", "Gutentag! "]

         await client.send_message(message.channel, random.choice(messages) + message.author.mention)


 @client.event

 async def on_message(message):

     if message.content.startswith('!valid'):

         rannum = random.randint(0, 100)

         await client.send_message(message.channel, (message.author.mention + " is",rannum,"% valid!"))


     client.run(TOKEN)

发生这种情况时不会显示错误消息。在这种情况下,我将不胜感激!

【问题讨论】:

  • 您需要将它们组合成一个on_message事件

标签: discord discord.py


【解决方案1】:

就像@Patrick Haugh 提到的那样,您只能拥有on_message 事件中的1 个。
另外,您可能只需要其中一个事件。
因为on_message 定义了当您收到某人的消息时发生的事件。您可以只检查消息的内容是什么,并据此执行操作。

像这样:

@client.event
async def on_message(message):

    if message.content.startswith('!valid'):
        rannum = random.randint(0, 100)
        await client.send_message(message.channel, (message.author.mention + " is",rannum,"% valid!"))
    elif message.content.startswith('!hello'):
        messages= ["*tips hat* G'day ", "Yeehaw pardner,  ", "Howdy, ", "Gutentag! "]
        await client.send_message(message.channel, random.choice(messages) + message.author.mention)

【讨论】:

  • 非常感谢!对不起,如果这个问题看起来很愚蠢,这种格式是我不熟悉的。
猜你喜欢
  • 2013-02-04
  • 1970-01-01
  • 2018-06-11
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2020-12-03
相关资源
最近更新 更多