单击表情时不会向用户发送 DM(直接消息)

Posted

技术标签:

【中文标题】单击表情时不会向用户发送 DM(直接消息)【英文标题】:No DM (direct message) is sent to the user when the emote is clicked 【发布时间】:2021-09-21 17:49:48 【问题描述】:

我正在尝试创建一个机器人,它会在拒绝规则后发送给 DM 用户。

@client.event
async def on_reaction_add(reaction, user):
    regulamin_channel = client.get_channel(864135527963820032)
    if reaction.emoji == "????":
        Role = discord.utils.get(user.guild.roles, name="Member")
        Role2 = discord.utils.get(user.guild.roles, name="Apply")
        await user.add_roles(Role)
        await user.remove_roles(Role2)
    if reaction.emoji == "????":
        await user.send("You must accept the rules")

不幸的是,我收到了这个错误:

Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
NameError: name 'ClientUser' is not defined
Ignoring exception in on_reaction_add
Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\abc.py", line 1013, in send
    channel = await self._get_channel()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 299, in _get_channel
    ch = await self.create_dm()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 142, in general
    return await getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
Ignoring exception in on_reaction_add
Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\abc.py", line 1013, in send
    channel = await self._get_channel()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 299, in _get_channel
    ch = await self.create_dm()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 142, in general
    return await getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
Ignoring exception in on_reaction_add
Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\abc.py", line 1013, in send
    channel = await self._get_channel()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 299, in _get_channel
    ch = await self.create_dm()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 142, in general
    return await getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'
Ignoring exception in on_reaction_add
Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
NameError: name 'ClientUser' is not defined
Ignoring exception in on_reaction_add
Traceback (most recent call last):
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "E:\Moje rzeczy\BOT\bot.py", line 47, in on_reaction_add
    await user.send("You must accept the rules")
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\abc.py", line 1013, in send
    channel = await self._get_channel()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 299, in _get_channel
    ch = await self.create_dm()
  File "C:\Users\Mikołaj\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 142, in general
    return await getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'.

【问题讨论】:

你应该解释DM,因为这是一个discord特定的缩写。请记住:请始终发布完整的错误消息(包括文件和行号)。这将有助于定位问题。期待你的edit??????️ 【参考方案1】:

方法 .send() 不在作为用户传递给功能的 ClientUser 上,在文档中这是 ClientUser https://discordpy.readthedocs.io/en/stable/api.html#clientuser

但是你需要获取一个 User 对象来传递给函数或者在某个地方获取它https://discordpy.readthedocs.io/en/stable/api.html#discord.User

【讨论】:

非常感谢!

以上是关于单击表情时不会向用户发送 DM(直接消息)的主要内容,如果未能解决你的问题,请参考以下文章

带有自定义表情符号的反应角色

如何使用 Telegram Bot API 发送表情符号?

Javafx模拟QQ机制实现发送表情包的功能

机器人对表情符号做出反应

如何向对消息做出反应的用户添加特定角色

如何让不和谐机器人使用 discord.py 向消息添加表情符号反应?