【问题标题】:Event get's ignored by discord bot when another exists. (python)当另一个事件存在时,事件获取被不和谐机器人忽略。 (Python)
【发布时间】:2020-11-29 03:14:14
【问题描述】:

我正在使用discord.py 创建一个不和谐机器人,它的主要功能是在给出命令时输入一个故事。 只有两个命令:一个是帮助功能,另一个是开始故事。寻求帮助的命令是r!help,而开始故事的命令是r!start,但由于某种原因,bot 似乎没有识别出r!help 已由用户发送并且只响应@987654326 @. 下面是我的这些事件的代码。

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if message.content.startswith('r!help'):
        channel = message.channel
        await channel.send('Help')

# bring up help

@client.event
async def on_message(message):
    if message.content.startswith('r!start'):
        channel = message.channel
        await channel.send('Starting Story...')

#start story

我的 IDE (pycharm) 没有向我显示任何错误,该机器人似乎在服务器上在线,并使用 starting story 回复 r!help。它只是不响应帮助命令。 我确实注意到,当 r!startevent 被注释掉时,帮助事件确实起作用了,所以我认为问题出在故事事件中。

@client.event
async def on_message(message):
    if message.content.startswith('r!help'):
        channel = message.channel
        await channel.send('Help')


# bring up help

#@client.event
#async def on_message(message):
 #   if message.content.startswith('r!start'):
  #      channel = message.channel
   #     await channel.send('Starting Spam...')
#works when this section is not there

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite


    【解决方案1】:

    另一种方法是,如果您想将代码分成两个函数,则使用 bot.listen

    文档:Here

    此外,如果您想要一个完全受控的机器人,您可能需要使用 discord.ext.commands.Bot,然后使用 @bot.command()

    定义多个命令

    例如:Here

    【讨论】:

      【解决方案2】:

      那是因为您定义了相同的函数两次,从而再次更改它。要让它识别两者,您必须将它们都放在同一个函数中。

      @client.event
      async def on_message(message):
          if message.content.startswith('r!start'):
              channel = message.channel
              await channel.send('Starting Story...')
          elif message.content.startswith('r!help'):
              channel = message.channel
              await channel.send('Help')
      

      像这样,你定义了一次函数,不要更改它,然后检查两个命令。

      【讨论】:

        猜你喜欢
        • 2011-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        • 2019-05-11
        • 2021-03-30
        • 2023-04-04
        • 2021-06-13
        相关资源
        最近更新 更多