繁体   English   中英

如何获得 DMChannel object? - discord.py

[英]How to get a DMChannel object? - discord.py

我希望机器人检查带有新表情符号的频道,然后根据新表情符号做不同的事情。 它可以正常工作,直到表情符号位于私人频道上,这使得bot.get_channel(payload.channel_id)返回 None。

我对用户或会员 ID 有同样的问题。 payload.member返回 None,但bot.get_user(payload.user_id)返回成员 object。 那么,有没有这样的东西,但有渠道? 用什么来获取 DMChannel object?

@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:
           ...

output如果反应是在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'

您是否有理由需要使用on_raw_reaction_add而不是on_reaction_add 在 99% 的情况下,后者的作用与前者相同。 话虽如此,它的参数是reaction, user [文档]更容易解析,因为它们是 discord.py 对象。

而不是通过bot.get_channel检索频道,您可以只调用

channel = reaction.channel.message

完整示例:

@bot.event
async def on_reaction_add(reaction, user):
    channel = reaction.message.channel

如果您需要使用on_raw_reaction_add ,您可以使用bot.private_channels (文档)结合discord.utils.get (文档) (请记住检查您的导入)来获取 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)

我添加了一个新的 function on_reaction_add 它将在私人频道中响应并做不同的事情,而on_raw_reaction_add将在公会中出现反应时引发。 on_reaction_add仅适用于缓存中的消息的问题在我的情况下是可以解决的。 带有 bot 的服务器具有完全不可编辑的确切消息的通道,因此没有人可以添加新的反应,只能增加已经存在的反应的数量。 当有人点击反应时,他会得到不同的反应,这取决于他点击的表情符号是什么。 其中一个反应是发送一条私人消息(在 DMChannel 中)并向其添加反应,由于机器人没有关闭,他在缓存中有这条消息(因为他发送了它)。 现在,如果用户增加机器人添加的表情符号数量,机器人将以某种方式做出反应。 为了检查通道是否为 DMChannel,我在第一个 function 和if isinstance((reaction.message.channel), discord.channel.DMChannel):中写了if "None" in str(type(channel)) ): in other。

@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):
        ...

暂无
暂无

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

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