【问题标题】:Getting input from reactions not working discord.py从无效的反应中获取输入 discord.py
【发布时间】:2021-05-28 20:55:34
【问题描述】:

我想为我的机器人制作一个石头剪刀布游戏: 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

我尝试了许多修复方法,但都是徒劳的。

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

我该如何解决这个问题?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

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

    @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!")
    

    【讨论】:

    • 嘿,我试过你说的,在这里:: python def chk(reaction): return reaction.emb == emb and reaction.channel == ctx.channel and user == ctx.author react, user = await client.wait_for('reaction_add', check=chk) 但我得到了同样的错误......我错过了什么吗?
    • 请使用我提供的代码@NIH_Original
    • @NIH_Original 如果我的回答解决了您的问题,您可以用打勾将我的回答标记为正确。
    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 2020-11-07
    相关资源
    最近更新 更多