【问题标题】:Discord.py detecting reactionsDiscord.py 检测反应
【发布时间】:2021-03-12 22:11:51
【问题描述】:

我正在制作一个使用 DM 的 Discord.py 机器人。我有一个命令,机器人会向您发送消息,要求您对您尝试执行的操作做出反应。问题是当用户添加它不起作用的反应时。

这是我目前所拥有的

@client.command()
async def suggest(ctx):
        embed = discord.Embed(
            title = 'What kind of suggestion are you making?',
            description = '<:Yes:755187983585509517> Car Suggestion\n<:Maybe:755187983623258123> Job Suggestion\n<:No:755187984067854367> Other Suggestion Type',
            colour = discord.Colour.green()
        )

        await ctx.message.delete()
        await ctx.send(f'{ctx.message.author.mention} Please check your DMs to continue!')

        embed.set_footer(text='Pembroke Pines Bot - Made By: parker02311')

        msg = await ctx.author.send(embed=embed)
        await msg.add_reaction('<:Yes:755187983585509517>')
        await msg.add_reaction('<:Maybe:755187983623258123>')
        await msg.add_reaction('<:No:755187984067854367>')

        def check(reaction, user):
            return user == msg.author and str(reaction.emoji) == '<:Yes:755187983585509517>'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await ctx.author.send("Timed out")
        else:
            embed = discord.Embed(
                title = 'Car Suggestion',
                description = 'What is your username?',
                colour = discord.Colour.green()
            )

            embed.set_footer(text='Pembroke Pines Bot - Made By: parker02311')
            await ctx.author.send(embed=embed)
            
            def check(m):
                return m.content
            msg = await client.wait_for('message', check=check)
            await ctx.author.send(f'Okay, your username is: **{msg}**')

            embed = discord.Embed(
                title = 'Car Suggestion',
                description = 'What is the car model?',
                colour = discord.Colour.green()
            )

            embed.set_footer(text='Pembroke Pines Bot - Made By: parker02311')
            await ctx.author.send(embed=embed)
            msg = await client.wait_for('message', check=check)
            await ctx.author.send(f'Okay, the model is: **{msg}**')

        #channel = client.get_channel(734882692537253900)
        embed = discord.Embed(
            description = f'{ctx.message.author.mention} **made a suggestion\nChannel:** {ctx.message.channel}\n**Message Content:** {ctx.message.content}',
            colour = discord.Colour.red()
        )
        embed.set_footer(text='Pembroke Pines Bot - Made By: parker02311')
        embed.set_author(name=ctx.message.author)
        await logschannel.send(embed=embed)

这几乎直接来自 discord.py 文档。 https://discordpy.readthedocs.io/en/latest/api.html#discord.Client.wait_for

【问题讨论】:

  • 您的@client.event@bot.command 等在哪里?
  • 我会为完整的命令编辑它我刚刚给出了重要的部分,因为完整的命令很长
  • @KarlKnechtel 你去那里有完整的代码
  • 好的,当你说它“不起作用”时 - 是否有错误消息?有什么可观察到的事情发生吗?
  • @KarlKnechtel 不,什么都没有发生,没有错误,没有结果

标签: python discord.py


【解决方案1】:

原来如此

return user == msg.author and str(reaction.emoji) == '<:Yes:755187983585509517>'

我正在检查 BOT 添加的反应而不是用户,希望这可以节省其他人的时间。

【讨论】:

    【解决方案2】:

    这是我想出的一个建议命令:

    @client.command()
    async def suggest(ctx, *, suggestion):
        author = ctx.message.author
        file = open("suggestions.txt", "a+")
        file.write(str(author) + " : " + suggestion + "\n")
        embed = discord.Embed(title='Suggestion',
                              description="This Was Suggested By",
                              color=0xE0B802)
    
        embed.set_footer(text=f"Made By {ctx.author.mention}")
        embed.set_author(name=f"{author}")
        embed.add_field(name=author, value=suggestion)
        await ctx.send("Suggestion Submitted")
        msg = await channel.send(embed=embed)
        await msg.add_reaction('?')
        await msg.add_reaction('?')
        await ctx.send(f"{ctx.author.mention} suggested {suggestion}!")
    

    它将发送包含建议的嵌入并添加反应以及在其页脚中创建建议的作者。

    【讨论】:

    • 这已经解决了,但它不允许我将我的其他帖子标记为已解决,我认为您对原始帖子没有任何理解。
    【解决方案3】:

    你可以试试wait_for。例如here

    @client.event
    async def on_message(message):
        if message.content.startswith('$thumb'):
            channel = message.channel
            await channel.send('Send me that ? reaction, mate')
    
            def check(reaction, user):
                return user == message.author and str(reaction.emoji) == '?'
    
            try:
                reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
            except asyncio.TimeoutError:
                await channel.send('?')
            else:
                await channel.send('?')
    

    (取自 discord.py API 参考 https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.wait_for

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2021-03-04
    • 2021-03-13
    • 2021-05-13
    • 2022-01-24
    • 2020-10-06
    • 2020-11-24
    • 2021-03-11
    • 2021-10-22
    相关资源
    最近更新 更多