单击表情时,不会向用户发送 DM(直接消息)
No DM (direct message) is sent to the user when the emote is clicked
我正在尝试创建一个机器人,它将在拒绝规则后发送给 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'.
方法 .send() 不在作为用户传递给函数的 ClientUser 上,在文档中这是 ClientUser https://discordpy.readthedocs.io/en/stable/api.html#clientuser
但是你需要获取一个 User 对象传递给函数或者在某个地方获取它 https://discordpy.readthedocs.io/en/stable/api.html#discord.User
我正在尝试创建一个机器人,它将在拒绝规则后发送给 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'.
方法 .send() 不在作为用户传递给函数的 ClientUser 上,在文档中这是 ClientUser https://discordpy.readthedocs.io/en/stable/api.html#clientuser
但是你需要获取一个 User 对象传递给函数或者在某个地方获取它 https://discordpy.readthedocs.io/en/stable/api.html#discord.User