【问题标题】:How to read reactions from a DM channel on discord using discord.py如何使用 discord.py 从不和谐的 DM 频道读取反应
【发布时间】:2020-11-01 11:03:15
【问题描述】:

在编写不和谐机器人时,我意识到我需要阅读机器人对不同用户的反应。

代码:

@client.event
async def on_reaction_add(reaction, user):
    channel = reaction.message.channel
    await channel.send('{} has added {} to the message: {}'.format(user.name, reaction.emoji, reaction.message.content))

这基本上从机器人所在的任何频道(例如在公会中)读取消息,而不仅仅是 DM 频道。有解决办法吗?

提前致谢

【问题讨论】:

  • 您可以检查消息是否在公会中,然后以此为基础进行逻辑
  • 我已经尝试过这种方法,只是每当我将它放入条件时,它会读取 DMChannel 没有属性行会,这是有道理的,因此排除了这种方法。

标签: python discord.py


【解决方案1】:

我刚刚意识到这一点,所以我将其添加为答案。 您可以使用isinstance 来检查频道类型,因为 dpy 对所有频道都有不同的内部类。

@bot.event
async def on_reaction_add(reaction, user):
    if not isinstance(reaction.message.channel, discord.DMChannel):
        # Not a dm
        return

    print("Hey this should be a dm")

该代码只会在 dms 中运行。 或者,您可以删除 not 并将您的 dm 代码放入 if 语句中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 2021-10-17
    • 2021-02-27
    • 2019-10-11
    • 2020-11-16
    • 1970-01-01
    • 2022-01-02
    • 2021-11-14
    相关资源
    最近更新 更多