【问题标题】:Adding and responding to reactions in discord.py在 discord.py 中添加和响应响应
【发布时间】:2021-07-03 15:39:25
【问题描述】:

代码:

import discord

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged in as')
        print(self.user.name)
        print(self.user.id)
        print('------')



    async def on_message(self, message):
        words = [] # List of words to look for

        if message.author.id == self.user.id:
            return

        for i in words:
            if i in message.content.lower():
                await message.channel.send(f"Hey <@{message.author.id}>, i have noticed that in your message is a word on a list")
                break


            await message.add_reaction("✅")

            await client.wait_for("reaction_add")
            await message.delete()





client = MyClient()
client.run("TOKEN")

我如何让机器人对自己的消息添加反应,如果用户使用它删除自己的消息 我确实在寻找答案,但上帝是 discord.py 乱七八糟,我已经看到了 6 个不起作用的答案,而且他们似乎都使用了不同的模块

如果答案很容易找到,我很抱歉,但我就是找不到

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    首先,您应该始终尝试在文档 (discord.py documentation) 中找到您的答案。 (题外话:不是discord.py 很乱;很可能是你用来寻找答案的方法很乱。)

    TextChannel.send() method 返回已发送的消息。因此,您可以将该返回值分配给一个变量。

    对于另一个问题,有一个事件监听器可以检测到消息删除,on_message_delete()

    import discord
    
    class MyClient(discord.Client):
    
        async def on_ready(self):
            ...
    
        async def on_message(self, message):
            words = []
            if message.author.id == self.user.id:
                return
    
            for i in words:
                if i in message.content.lower():
                    sent_message = await message.channel.send(
                        f"Hey {message.author.mention}, I have noticed that in your message is a word on a list"
                    )
                    break
    
                await sent_message.add_reaction("reaction")
                await message.add_reaction("✅")
                await client.wait_for("reaction_add")
                await message.delete()
    
        async def on_message_delete(self, message):
            # Do stuff here
    
    
    client = MyClient()
    client.run("TOKEN")
    

    (附带说明,您可以使用Member.mention 来提及/ping 成员,而不是"&lt;@{message.author.id}&gt;"。)

    【讨论】:

      【解决方案2】:

      在我看来,最好在事件方法上方使用@client.event 方法装饰器,而不是将它们放在自己的类中。您可以将客户端对象声明为顶部的client=discord.Client(),然后将@client.event 放在您的事件处理方法之上。 on_reaction_add 方法可以有响应和消息参数来响应。

      【讨论】:

        猜你喜欢
        • 2021-06-22
        • 2021-05-13
        • 1970-01-01
        • 2021-01-19
        • 2022-01-11
        • 2021-05-08
        • 2020-02-22
        • 2021-10-24
        • 2017-04-30
        相关资源
        最近更新 更多