从不工作的反应中获取输入 discord.py

时间:2021-02-11 07:06:39

标签: discord.py

我想为我的机器人制作一个石头剪刀布游戏: The bot creates an embed with instructions and reacts with a "rock", "paper", and "scissor" emoji, which the user has to click to input his/her choice.

但问题是代码没有继续前进并显示错误。

代码如下:

@client.command(aliases = ["rock_paper_scissors","rps"])
async def _rps(ctx):    #rps is short for rock, paper, scissor
    emojis = ['✊', '?️', '✌️']
    
    embedVar = discord.Embed(title="CHOOSE YOUR WEAPON!",description = "Choose between rock, paper, or scissors, {}." . format(ctx.author.mention), color = 0xff9900)
    embedVar.add_field(name=":fist: ROCK", value="React with :fist: emoji to choose rock.", inline = False)
    embedVar.add_field(name=":hand_splayed: PAPER", value="React with :hand_splayed: emoji to choose paper.", inline = False)
    embedVar.add_field(name=":v: SCISSORS", value="React with :v: emoji to choose scissors.", inline = False)
    emb = await ctx.send(embed = embedVar)

    for emoji in emojis:
        await emb.add_reaction(emoji)

    def chk(reaction):
        return reaction.emb == emb and reaction.channel == ctx.channel
    
    react = await client.wait_for('reaction_add', check=chk)

    if react == '✊':
        await ctx.send("You chose rock!")
    elif react == '?️':
        await ctx.send("You chose paper!")
    elif react == '✌️':
        await ctx.send("You chose scissors!")

我收到此错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: chk() takes 1 positional argument but 2 were given

我尝试了很多修复,但都无济于事。

另外,我希望机器人只接受最初通过命令请求石头剪刀布游戏的用户(换句话说,消息的作者)的输入,但我不确定如何实施,我试过了,但没有奏效。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

reaction_add 给出了反应和用户的元组,因此在 chk 函数中,您需要同时拥有反应和用户。您还应该在 and user == ctx.author 函数中使用 chk 以确保用户相同。

@client.command(aliases = ["rock_paper_scissors","rps"])
async def _rps(ctx):    #rps is short for rock, paper, scissor
    emojis = ['✊', '?️', '✌️']
    
    embedVar = discord.Embed(title="CHOOSE YOUR WEAPON!",description = "Choose between rock, paper, or scissors, {}." . format(ctx.author.mention), color = 0xff9900)
    embedVar.add_field(name=":fist: ROCK", value="React with :fist: emoji to choose rock.", inline = False)
    embedVar.add_field(name=":hand_splayed: PAPER", value="React with :hand_splayed: emoji to choose paper.", inline = False)
    embedVar.add_field(name=":v: SCISSORS", value="React with :v: emoji to choose scissors.", inline = False)
    emb = await ctx.send(embed = embedVar)

    for emoji in emojis:
        await emb.add_reaction(emoji)

    def chk(reaction, user):
        return reaction.emb == emb and reaction.channel == ctx.channel and user == ctx.author
    
    react, user = await client.wait_for('reaction_add', check=chk)

    if react == '✊':
        await ctx.send("You chose rock!")
    elif react == '?️':
        await ctx.send("You chose paper!")
    elif react == '✌️':
        await ctx.send("You chose scissors!")