Discord.py - 如何在事件中调用命令?
Discord.py - How can I have a command invoked in an event?
所以我试图在 discord.py 中创建一个事件,每当用户 mentions/pings 机器人时,它都会说些什么。下面的代码运行没有错误,但是唯一可行的方法是我的命令前缀在消息中。
对于我的机器人,前缀是“/”,所以每当我在消息中提到带有“/”的机器人时,它都会说些什么。如果我决定只提及机器人,机器人不会回应。我很确定它与最后一行代码有关,但我不知道如何解决这个问题。
@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)
代码写在Python3.7.4.
如有帮助,我们将不胜感激!
当有人提到用户时,机器人会读取 <@userid>。因此,如果您想使用 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')
所以我试图在 discord.py 中创建一个事件,每当用户 mentions/pings 机器人时,它都会说些什么。下面的代码运行没有错误,但是唯一可行的方法是我的命令前缀在消息中。
对于我的机器人,前缀是“/”,所以每当我在消息中提到带有“/”的机器人时,它都会说些什么。如果我决定只提及机器人,机器人不会回应。我很确定它与最后一行代码有关,但我不知道如何解决这个问题。
@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)
代码写在Python3.7.4.
如有帮助,我们将不胜感激!
当有人提到用户时,机器人会读取 <@userid>。因此,如果您想使用 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')