【问题标题】:Discord.py | Bot responses when it is pinged不和谐.py |机器人在被 ping 时响应
【发布时间】:2020-11-21 22:45:00
【问题描述】:

如果我执行if message.content.startswith('hello'): 之类的操作,则效果很好。

但是,我找不到使它适用于 ping 的方法。我尝试了机器人 ID 和名称。我的机器人 ID 代码:

@client.event
async def on_message(message):
    await client.process_commands(message)
    if message.content.startswith('<@741352621826375781>'):
        embed = discord.Embed(
            colour = discord.Colour.red())
     
        embed.add_field(name='Hello', value='''My prefix is `.`, type `.help` for help.''', inline=False)
            
        await message.channel.send(embed=embed)

有人可以帮忙吗?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    这里是 Sofia 的一些改进版

    @client.event
    async def on_message(message): 
        # Adding `<!@{id_here}>` so it's gonna work whether it's mentioned by nick or simply by the name
        if message.content in [f'<!@{client.user.id}>', f'<@{client.user.id}>']:
            await message.channel.send('Hello there')
    
        await client.process_commands(message)
    

    【讨论】:

      【解决方案2】:

      这应该可以解决您的问题,您的代码不起作用的原因是属性错误。

      @client.event
      async def on_message(message):
         if str(message.content) == '<@741352621826375781>':
              await message.channel.send('Hello ?')
          await client.process_commands(message)
      

      【讨论】:

        【解决方案3】:

        有一种更简单的方法可以检查消息中是否提到了该机器人。

        @client.event
        async def on_message(message):
            if client.user in message.mentions:
                await message.channel.send("Hello!")
        

        【讨论】:

          猜你喜欢
          • 2021-07-07
          • 2021-03-03
          • 2020-06-20
          • 2021-06-25
          • 2020-12-18
          • 2019-04-13
          • 1970-01-01
          • 2021-06-17
          • 2018-11-17
          相关资源
          最近更新 更多