python - discord.py bot 只响应一个命令

标签 python error-handling discord bots discord.py

它在给定时间只显示其中一个命令。
如果我写 !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/

相关文章:

tsql - 外键约束冲突,但行仍然插入?

actionscript-3 - 如何使Flashdevelop错误出现在结果面板中?

python - 如何解封用户?

python - 处理 Pandas 中的稀疏类别 - 用 "Other"替换所有不在顶级类别中的内容

Python GRPC 服务器性能瓶颈

python - 在python网络抓取脚本中为错误创建异常

node.js - NodeJS 返回未处理的 promise 拒绝错误

discord - discord bot 1.7.3 错误给出很长的错误

Python 参数解析器,在 -h 之前引发异常

python - 在 Python 中调用函数列表的惯用方式是什么?