简体   繁体   English

Discord.py DM 中的机器人反应

[英]Discord.py Bot Reactions in DMs

I have a discord.py bot that I want to code to do the following things:我有一个 discord.py 机器人,我想编写代码来执行以下操作:

  1. When a user types the command, the bot should send the user a DM当用户键入命令时,机器人应该向用户发送 DM
  2. Then, it should add reactions to this message (A thumbs up and thumbs down)然后,它应该对此消息添加反应(竖起大拇指和向下竖起大拇指)
  3. Finally, it should wait for the user to react with one of those reactions.最后,它应该等待用户对这些反应之一做出反应。

Now based on the docs and previous projects, I came up with this function for adding reactions:现在基于文档和以前的项目,我想出了这个 function 用于添加反应:

async def get_reacts(user, client, message, emojis, timeout=None):
    for emoji in emojis:
        await message.add_reaction(emoji)
    try:
        def check(reaction, reactor):
            return reactor.id == user.id and reaction.emoji in emojis
        reaction, user = await client.wait_for("reaction_add", check=check, timeout=timeout)
        return reaction.emoji
    except:
        pass

This code works perfectly when in a server, but when the message is in a DM, it does something peculiar.此代码在服务器中运行良好,但当消息在 DM 中时,它会做一些特殊的事情。 First, it doesn't detect the user reactions at all.首先,它根本没有检测到用户的反应。 When I put a print statement within the check function, it told me that it parsed one reaction, and that reaction was the bot itself reacting with the thumbs down.当我在支票 function 中放入打印语句时,它告诉我它解析了一个反应,而该反应是机器人本身对拇指向下的反应。 When I reacted to the message, the check function was never called.当我对该消息做出反应时,从未调用过检查 function。

I saw some other solutions used a Cog listener to go through all added reactions, and use a global list of messages.我看到其他一些解决方案通过所有添加的反应使用了 go 的 Cog 侦听器,并使用全局消息列表。 However, this won't work for my bot as everything it does is in one command.但是,这对我的机器人不起作用,因为它所做的一切都在一个命令中。 Additionally, it takes up a lot of memory.此外,它占用了大量的 memory。 This is why I went with the client.wait_for approach instead.这就是我client.wait_for方法的原因。

Is there a problem with using client.wait_for in DMs?在 DM 中使用client.wait_for有问题吗? Should I use Cog listeners instead?我应该改用 Cog 监听器吗? Or is it a problem with my code?还是我的代码有问题? Any help is appreciated.任何帮助表示赞赏。 Thanks in advance!提前致谢!

EDIT: Intents enabled: None (Do I need an intent to check DM reactions?)编辑:启用意图:无(我是否需要意图来检查 DM 反应?)

EDIT 2: Added default intents, still the same issue编辑 2:添加了默认意图,仍然是同样的问题

EDIT 3: How I am using the get_reacts function:编辑 3:我如何使用get_reacts function:

msg = await context.author.send("Message")
reaction = await get_reacts(context.author, self.client, msg, ["👍", "👎"])

Solution解决方案

In order to receive reactions event in DM from users you need to enable the members intent.为了从用户接收 DM 中的反应事件,您需要启用members意图。

So the only thing you need to do is create your default intent and then set the members value to True:因此,您唯一需要做的就是创建默认意图,然后将members值设置为 True:

intents = discord.Intents.default()
intents.members = True

Explanation解释

Op was asking for clarification so here it is. Op要求澄清,所以在这里。

If you take a look at the discord.Intents.default() code:如果您查看discord.Intents.default()代码:

@classmethod
def default(cls):
    self = cls.all()
    self.presences = False
    self.members = False
    return self

You can see it does two things:你可以看到它做了两件事:

  • Generate intents for everything (using its own discord.Intents.all())为所有内容生成意图(使用它自己的 discord.Intents.all())
  • From those intents, remove the one corresponding to presence and members从这些意图中,删除与presencemembers相对应的意图

So the assumptions of op:所以op的假设:

This makes me think that maybe dm_reactions and reactions are not included in default intents这让我觉得默认意图中可能不包含 dm_reactions 和反应

is not true.不是真的。 Because only presence and members are removed, all the other ones are in it.因为只有存在和成员被删除,所有其他的都在其中。

Now, turns out even with dm_reactions , reactions , and dm_messages on, you still need to have the members on.现在,即使打开了dm_reactionsreactionsdm_messages ,您仍然需要打开members So that is what is happenning in the solution.这就是解决方案中正在发生的事情。

And as OP pointed out, it does work with discord.Intents.all() because, in this case, the member intent is enabled.正如 OP 所指出的,它确实适用于discord.Intents.all()因为在这种情况下, member意图已启用。

Yes, you need intents.是的,你需要意图。 dm_reactions and reactions (It shouldn't work without them). dm_reactionsreactions (没有它们就不应该工作)。 Easiest for you would be to simply enable default intents对您来说最简单的方法是简单地启用默认意图

intents = discord.Intents.default()
client = commands.Bot(..., intents=intents)

Adding on to Łukasz's answer, I tried some experimentation, and found that using discord.Intents.all() worked, but not discord.Intents.default() .添加到Łukasz的答案,我尝试了一些实验,发现使用discord.Intents.all()有效,但不是discord.Intents.default() This makes me think that maybe dm_reactions and reactions are not included in default intents, but are included in all intents?这让我觉得可能dm_reactionsreactions不包含在默认意图中,包含在所有意图中? Any clarification on this is still appreciated, as I just started learning discord.py a few months ago.对此的任何澄清仍然值得赞赏,因为几个月前我刚刚开始学习 discord.py。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM