首页 > 解决方案 > 石头剪刀布 Discord.py

问题描述

所以我试图在我的 discord.py 机器人上创建一个剪刀石头布命令,但不是发送响应,而是使用响应,而是响应它自己的响应。试过 代码是

    async def rps(self, ctx):
        global RPS_START, RPS_AUTHOR, channel
        channel = ctx.channel
        embed = discord.Embed(title="**Rock Paper Scissor**")
        msg = await ctx.send(embed=embed)
        await msg.add_reaction('')
        await msg.add_reaction('')
        await msg.add_reaction('✂')
        RPS_START = True
        RPS_AUTHOR = ctx.message.author

    @commands.Cog.listener()
    async def on_raw_reaction_add(self, reaction):
        global RPS_START, RPS_AUTHOR, human_decision, channel, reply
        if RPS_START is False and reaction.member != RPS_AUTHOR and not reaction.member.bot:
            return
        elif reaction.emoji.name == '':
            human_decision = 'rock'
        elif reaction.emoji.name == '':
            human_decision = 'paper'
        elif reaction.emoji.name == '✂':
            human_decision = 'scissor'
        RPS_START = False

        rock_paper_scissors = ('rock', 'paper', 'scissor')
        bot_decision = (random.choice(rock_paper_scissors)).lower()

        if human_decision in ['rock', 'paper', 'scissor']:
            if human_decision == bot_decision:
                reply = 'tie'
            elif human_decision == 'rock' and bot_decision == 'paper':
                reply = 'lost'
            elif human_decision == 'paper' and bot_decision == 'rock':
                reply = 'win'
            elif human_decision == 'scissor' and bot_decision == 'paper':
                reply = 'win'
            elif human_decision == 'paper' and bot_decision == 'scissor':
                reply = 'lost'
            elif human_decision == 'stone' and bot_decision == 'scissor':
                reply = 'win'
            elif human_decision == 'scissor' and bot_decision == 'rock':
                reply = 'lost'```

标签: pythondiscorddiscord.py

解决方案


在您的活动开始时使用一个简单的 if 语句on_raw_reaction_add应该可以解决问题:

if reaction.member.bot:
    return

reaction此外,当您用作变量的名称时,您可能会感到非常困惑。在文档中它被声明为payload所以你应该考虑改变它。


推荐阅读