繁体   English   中英

discord.py bot 只响应一个命令

[英]discord.py bot only responds to one command

它仅在给定时间显示其中一个命令。
如果我写!hi!bye它将不起作用,但如果我写!sing它将 output la la la
如果我切换它之前的角色,它会变成!hi!sing not working 但!bye working and say 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
  elif message.content.startswith('!sing'):
    await message.channel.send('La La la.')
  elif 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('!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 这应该这样做。

不要重复 on_message 事件。 只有一个并将 if 语句放入这一事件中。

这是使用commands.Bot()的完美示例:

from discord.ext import commands

bot = commands.Bot(command_prefix="!")

@bot.command()
async def hi(ctx):
    await ctx.send("Hello!")

@bot.command()
async def sing(ctx):
    await ctx.send("la la la!")

@bot.command()
async def bye(ctx):
    await ctx.send("Goodbye friend!")

Bot()继承自Client() ,在处理命令时提供了更多功能!


参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM