它在给定时间只显示其中一个命令。
如果我写 !hi
或 !bye
它不会工作,但如果我写 !sing
它会输出 la la la
。
如果我在它之前切换角色,它会变成
!hi
或 !sing
不工作
但是 !bye
工作并说 Goodbye!
import os
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'
.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi'):
await message.channel.send('Hello!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!bye'):
await message.channel.send('Goodbye Friend!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!sing'):
await message.channel.send('la la la')
client.run(os.getenv('TOKEN'))```
最佳答案
您正在尝试使用多个事件,而不是像这样在一个事件中使用所有事件:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hi'):
await message.channel.send('Hello!')
elif message.content.startswith('!bye'):
await message.channel.send('Goodbye Friend!')
elif message.content.startswith('!sing'):
await message.channel.send('la la la')
此外,确保在其他事件中使用 elif
而不是 if
。
这应该可以做到。
关于python - discord.py bot 只响应一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65542062/