我想让机器人检查新的表情符号的频道,然后做不同的事情,取决于新的表情符号是什么。它会正常工作,直到表情符号在私人频道上,这会使bot.get_channel(payload.channel_id)
返回None。
我在用户或成员id上也遇到了同样的问题。payload.member
返回None,但bot.get_user(payload.user_id)
返回成员对象。那么,有没有类似的东西,但是有通道呢?使用什么来获取DMChannel对象?
@bot.event
async def on_raw_reaction_add(payload):
print(bot.get_channel(payload.channel_id), payload.channel_id) # This line will be deleted, it is used for showing the problem.
if payload.user_id != None:
channel = bot.get_channel(payload.channel_id)
msg = await channel.fetch_message(payload.message_id)
emoji = payload.emoji
author = payload.member
if emoji.is_custom_emoji():
emoji_count = discord.utils.get(msg.reactions, emoji=emoji).count
else:
emoji_count = discord.utils.get(msg.reactions, emoji = emoji.name).count
if payload.channel_id == channel_i:
if emoji_count > 1:
...
输出如果反应在DM通道中,则会出现错误,因为该通道是NoneType,这是由前一行引起的。
None 782664385889959976
Ignoring exception in on_raw_reaction_add
Traceback (most recent call last):
File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py",
line 312, in _run_event
await coro(*args, **kwargs)
File "C:\Users\plays\OneDrive\Рабочий стол\Python\bot2.py", line 122, in on_raw_reaction_add
msg = await channel.fetch_message(payload.message_id)
AttributeError: 'NoneType' object has no attribute 'fetch_message'
发布于 2021-01-09 20:06:26
你需要使用on_raw_reaction_add
而不是on_reaction_add
有什么原因吗?在99%的情况下,后者的作用与前者相同。也就是说,它的参数是reaction, user
[documentation],因为它们是discord.py对象,所以更容易解析。
而不是通过bot.get_channel
检索通道,然后只需调用
channel = reaction.channel.message
完整示例:
@bot.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
发布于 2021-01-09 21:22:03
如果您需要使用on_raw_reaction_add
,您可以结合使用bot.private_channels
(Documentation)和discord.utils.get
(Documentation) (请记住检查您的导入以获取DM通道)。
@bot.event
async def on_raw_reaction_add(payload):
if payload.user_id != None:
channel = bot.get_channel(payload.channel_id)
if channel is None: # checking if we are in a DM channel
channel = discord.utils.get(bot.private_channels, id=payload.channel_id)
发布于 2021-01-11 19:25:03
我添加了一个新函数on_reaction_add
。它将在私人渠道中做出反应并做不同的事情,而如果反应在行会中,on_raw_reaction_add
将会提高。在我的例子中,on_reaction_add
只能处理缓存中的消息的问题是可以解决的。带有机器人的服务器有带有确切消息的频道,这些频道是完全不可编辑的,所以没有人可以添加新的反应,只能增加已经存在的反应的数量。当一个人点击这个反应时,他会得到不同的反应,这取决于他点击的是什么表情。其中一个反应是发送一条私有消息(在DMChannel中),并向其添加反应,由于机器人没有关闭,它在缓存中有这条消息(因为它发送了它)。现在,如果用户增加机器人添加的表情符号的数量,机器人将以某种方式做出反应。为了检查通道是否为DMChannel,我在第一个函数中编写了if "None" in str(type(channel))
,在其他函数中编写了if isinstance((reaction.message.channel), discord.channel.DMChannel):
。
@bot.event
async def on_raw_reaction_add(payload):
channel = bot.get_channel(payload.channel_id)
if payload.user_id != 773501851396079618 and not "None" in str(type(channel)):
msg = await channel.fetch_message(payload.message_id)
emoji = payload.emoji
author = payload.member
if emoji.is_custom_emoji():
emoji_count = discord.utils.get(msg.reactions, emoji=emoji).count
else:
emoji_count = discord.utils.get(msg.reactions, emoji = emoji.name).count
...
...
@bot.event
async def on_reaction_add(reaction, user):
if isinstance((reaction.message.channel), discord.channel.DMChannel):
...
https://stackoverflow.com/questions/65642072
复制相似问题