石头剪刀布 Discord.py

时间:2021-06-22 05:42:47

标签: python discord discord.py

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

    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'```

2 个答案:

答案 0 :(得分:1)

on_raw_reaction_add 事件开始时的简单 if 语句应该可以解决问题:

if reaction.member.bot:
    return

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

答案 1 :(得分:0)

我只需要改变:

if RPS_START is False and payload.member.id != RPS_AUTHOR and payload.member.bot:
      return

致:

if RPS_START is False or payload.member.id != RPS_AUTHOR or payload.member.bot:
      return