【问题标题】:Python Discord Bot - python clear_reaction() clears all reactions instead of a specific onePython Discord Bot - python clear_reaction() 清除所有反应而不是特定反应
【发布时间】:2020-08-16 03:22:49
【问题描述】:

我正在使用 python 制作一个 Discord Bot,我希望一条消息只有某些反应,并且在添加反应时,我希望代码删除每个不需要的反应。我真的希望它验证是否没有其他反应要删除,而不仅仅是刚刚添加的那个。

正如标题中所说,我的问题是我不知道为什么,但clear_reaction() 清除了所有反应。

这是我的代码:

inter_totale = ["✅", "❌"]


@bot.event
async def on_raw_reaction_add(payload):
    emoji, user, member, channel = payload.emoji, await bot.fetch_user(user_id=payload.user_id), payload.member, bot.get_channel(payload.channel_id)
    msg = await channel.fetch_message(payload.message_id)

    if payload.message_id == specific_message:
        if inter_totale.count(emoji.name):
            pass #  NOT DONE YET

        else:  #  deletes unwanted reactions
            for r in msg.reactions:
                if not inter_totale.count(r.emoji):
                    await msg.clear_reaction(r)

谢谢。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    这将删除所有反应而不是机器人它。

    你可以做这样的事情。 if 语句只是为了检查它是否不是机器人本身,它会删除所有不是机器人做出的反应。

    @bot.event
    async def on_raw_reaction_add(payload):
        channel = await bot.fetch_channel(payload.channel_id)
        message = await channel.fetch_message(payload.message_id)
        reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
        inter_totale = ["✅", "❌"]
    
        if user.id != bot.user.id and payload.emoji.name not in inter_total:
            await reaction.remove(payload.member)
    
    

    【讨论】:

    • 这是一个很好的方法,但它只会删除刚刚添加的反应。我希望它验证是否删除了所有不需要的。
    • 您可以清除所有反应并保留您必须执行一次的想要的反应。之后,它将消除任何不需要的反应。
    猜你喜欢
    • 2020-08-16
    • 2021-02-01
    • 2020-06-29
    • 2021-06-25
    • 2021-06-02
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多