[英]Discord.py Bot Reactions in DMs
I have a discord.py bot that I want to code to do the following things:我有一个 discord.py 机器人,我想编写代码来执行以下操作:
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, ["👍", "👎"])
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
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:你可以看到它做了两件事:
presence
and members
presence
和members
相对应的意图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_reactions
、 reactions
和dm_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_reactions
和reactions
(没有它们就不应该工作)。 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_reactions
和reactions
不包含在默认意图中,但包含在所有意图中? 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.