首页 > 解决方案 > 当被提及时,如何使用 discord.py 中的机器人回复 Discord 频道上的消息?

问题描述

非常坚持这个 - 这是这里的代码:

'如果提到机器人'似乎没有变化 - 任何解决方案?

import discord

class MyClient(discord.Client):
    async def on_ready(self):
       print('Logged on as', self.user)

    async def on_message(self, message):

    # don't respond to ourselves
       if message.author == self.user:
          return
    
       if client.user.mentioned_in(message) and 'Hi' in message.content:
          await message.channel.send('_Solara hooman_ :wink:')

       else:
         return

client = MyClient()

标签: pythondiscorddiscord.py

解决方案


您应该使用self.user而不是client. 这应该有效:

import discord

class MyClient(discord.Client):
    async def on_ready(self):
       print('Logged on as', self.user)

    async def on_message(self, message):
        # don't respond to ourselves
        if message.author == self.user:
            return

        if self.user.mentioned_in(message) and 'Hi' in message.content:
            await message.channel.send('_Solara hooman_ :wink:')
        else:
            return

现在,如果您 p​​ing 机器人并说Hi(区分大小写),它应该会响应。


推荐阅读