当提到Discord.py时,如何使用Discord.py中的bot回复Discord频道上的消息?

2024-04-19 21:56:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这一点很难理解-以下是代码:

“如果提到机器人”的变体似乎不起作用-有解决方案吗

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()

Tags: 代码inselfclientmessageasyncreturnif
1条回答
网友
1楼 · 发布于 2024-04-19 21:56:58

您应该使用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

现在,如果ping bot并说Hi(区分大小写),它应该会响应

相关问题 更多 >