【问题标题】:Discord.py - How can I have a command invoked in an event?Discord.py - 如何在事件中调用命令?
【发布时间】:2020-08-02 09:10:38
【问题描述】:

所以我试图在 discord.py 中创建一个事件,每当用户提及/ping 机器人时,它都会说些什么。下面的代码运行没有错误,但唯一可行的方法是我的命令前缀在消息中。

对于我的机器人,前缀是“/”,所以每当我在消息中提到带有“/”的机器人时,它都会说些什么。如果我决定只提及机器人,机器人不会响应。我很确定这与最后一行代码有关,但我不知道如何解决这个问题。

@client.event
async def on_message(message):
    if client.user.mention in message.content.split():      
        await message.channel.send('You mentioned me!')
    else:
        await client.process_commands(message)

代码是用 Python 3.7.4 编写的。

所有帮助将不胜感激!

【问题讨论】:

  • 看起来机器人只会处理未提及的命令。您可能希望将process_commands 移出else
  • @PatrickHaugh 我很确定我以前做过,我删除了else 并且发生了同样的结果。 (“/@botname”有效,但“@botname”无效)

标签: python python-3.x discord python-3.7 discord.py


【解决方案1】:

当有人提到用户时,机器人会读取 。因此,如果您想使用 on_message 函数,则需要包含一个

if '<@bot/user id>' in message.content:
        await message.channel.send('hi')

你也可以做...

if bot.user in message.mentions:
            await message.channel.send('hi')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-09
    • 2022-01-07
    • 2020-08-17
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 2021-09-03
    相关资源
    最近更新 更多