【问题标题】:Trying to make a "yes/no" reaction confirmation but bot does not respond to reactions试图做出“是/否”反应确认,但机器人不响应反应
【发布时间】:2021-04-30 07:52:06
【问题描述】:

我正在尝试对我的代码进行确认投票,以便我的机器人在没有确认我想要这样做的情况下不会清除消息。

我正在尝试通过反应投票来做到这一点,当我输入命令时,它会发送一条消息以及 tickcross 反应。如果我用 tick 表情符号 (<:nonatick:803586318369292289>) 做出反应,它会继续清除消息,而如果我用 cross 表情符号 (<:RedTick:801684348502933525>) 做出反应,它会发送另一条消息,然后什么都不做。

我已经得到了它发送Are you sure you want to delete 1 messages? 的部分以及机器人的反应。但是,如果我对任何一个表情符号做出反应,它都不会进一步响应。

@client.command(pass_context=True)
@commands.has_permissions(manage_messages=True)
async def clear(ctx, number):
    number = int(number)
    moreoma = ctx.author.id
    message = await ctx.send("Are you sure you want to delete " + str(number) + " messages?")
# Adds reaction to above message
    for emoji in ('<:nonatick:803586318369292289>', '<:RedTick:801684348502933525>'):
        await message.add_reaction(emoji)

    def check(reaction, user):
        return user.id == moreoma and str(reaction.emoji) in ['<:nonatick:803586318369292289>',
                                                              '<:RedTick:801684348502933525>']

    reaction, user = await client.wait_for('reaction_add', timeout=5, check=check)
    if reaction.emoji == '<:nonatick:803586318369292289>':
        await ctx.channel.purge(limit=number + 2, check=lambda msg: not msg.pinned)
    elif reaction.emoji == '<:RedTick:801684348502933525>':
        await ctx.send("aight looks like we are not clearing messages as of now. ")

在我的代码中启用了意图:

intents = discord.Intents(messages=True, guilds=True)
intents.reactions = True
intents.members = True

这就是我使用该命令时发生的情况。

它检查我是否有反应的方式有问题吗?

提前感谢您的帮助!

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你可以使用try/except来做到这一点

    @client.command(pass_context=True)
    @commands.has_permissions(manage_messages=True)
    async def clear(ctx, number):
        number = int(number)
        moreoma = ctx.author.id
        message = await ctx.send("Are you sure you want to delete " + str(number) + " messages?")
    
        emojis = ['<:nonatick:803586318369292289>', '<:RedTick:801684348502933525>']
    
        # Adds reaction to above message
        for emoji in (emojis):
            await message.add_reaction(emoji)
    
        def check(reaction, user):
            reacted = reaction.emoji
            return user.id == moreoma and str(reaction.emoji) in emojis
    
        try:
            reaction, user = await client.wait_for('reaction_add', timeout=10, check=check)
        except asyncio.TimeoutError:
            await ctx.send("timeout")
        else:
            if str(reacted) == '<:nonatick:803586318369292289>':
                await ctx.channel.purge(limit=number + 2, check=lambda msg: not msg.pinned)
            elif reaction.emoji == '<:RedTick:801684348502933525>':
                await ctx.send("aight looks like we are not clearing messages as of now. ")
    

    【讨论】:

    • 我收到了这个错误(我认为是因为self.reacted 部分) self is not defined 编辑:忘记输入将提交命令
    • 评论* discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'self' is not defined
    • 抱歉,我以为你是在齿轮上做的
    猜你喜欢
    • 2022-10-30
    • 2020-04-16
    • 2021-10-07
    • 1970-01-01
    • 2020-07-30
    • 2019-06-01
    • 2021-06-28
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多