Discord Bot check message author and emoji reaction error: 1 positional argument but 2 were given

huangapple go评论80阅读模式
英文:

Discord Bot check message author and emoji reaction error: 1 positional argument but 2 were given

问题

抱歉,以下是翻译好的部分:

我正在尝试创建Discord机器人命令,其中机器人会创建表情符号,如果用户对该表情符号做出反应 - 机器人将执行一些所需的操作,但在检查部分出现以下错误:

> discord.ext.commands.errors.CommandInvokeError: 命令引发了异常:TypeError: check()接受1个位置参数,但提供了2个

机器人发送消息并附加表情符号,但是检查部分引发错误,我不知道如何解决它。

这是我的代码:

错误出现在代码的倒数第二行

@commands.command()
async def test(self, ctx):
    author = ctx.author
    message = await ctx.send('test')

    emote = '✔'

    for e in emote:
        await message.add_reaction(e)

    def check(author):
        def react_check(reaction, emoji):
            return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
        return check

    await self.client.wait_for('reaction_add', check=check(author, emote), timeout=60)
    await ctx.send('检查后的一些操作')
英文:

I'm trying to make Discord bot command, where Bot creates Emoji and if user reacts on this Emoji - bot doing some required stuff, but getting this error on check part:

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

The Bot sends the message and attaches Emoji to it, but however, the check part throws an error and I don’t know how to solve it.

Here's my code:

An error is issued in the penultimate line of code

@commands.command()
async def test(self, ctx):
    author = ctx.author
    message = await ctx.send('test')

    emote = '✔'

    for e in emote:
        await message.add_reaction(e)

    def check(author):
        def react_check(reaction, emoji):
            return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
        return check

    await self.client.wait_for('reaction_add', check=check(author, emote), timeout=60)
    await ctx.send('some stuff after check')

答案1

得分: 1

你实际上可以在discord.py文档中找到一个很好的示例。

@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('👍')

正如你所看到的问题是:你将已经带有参数执行的函数 "check" 传递给了 "wait_for" 函数。这是不正确的做法,尤其是因为你的变量 "reaction" 和 "user" 从未被定义过。

你实际上需要做的是:

def check(author):
    def react_check(reaction, emoji):
        return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
    return check

author, emote = await self.client.wait_for('reaction_add', check=check, timeout=60)

这将传递函数而不是函数的返回值到 "wait_for" 中。

英文:

You can actually find a very nice example in the discord.py documentation.

@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('👍')

As you can see your problem is the following: You are passing your function check already having executed with arguments into the wait_for function. That is the incorrect way to do it, especially because your variables reaction and user have never been defined.

What you actually need to do is the following:

def check(author):
    def react_check(reaction, emoji):
        return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
    return check

author, emote = await self.client.wait_for('reaction_add', check=check, timeout=60)

This passes the function, not the returned value from the function into wait_for.

答案2

得分: 1

主要错误在函数check中,为了修复这个错误,它将稍微更改,但将正确工作。

def check(reaction, user):
    return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)

还有一些代码改进,完整的代码如下:

@commands.command()
async def test(self, ctx):
    author = ctx.author
    message = await ctx.send('test')

    emote = '✔'

    for e in emote:
        await message.add_reaction(e)

    def check(reaction, user):
        return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)

    try:
        reaction, user = await self.client.wait_for('reaction_add', check=check, timeout=60)
    except asyncio.TimeoutError:
        await ctx.send("Timed out")
        return
    if str(reaction) == '✔':
        await ctx.send('some stuff')
英文:

Main error is in function check and for fixing the error it will be slightly changed, but will work correctly.

def check(reaction, user):
    return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)

And just some improvements in code, so full code is here below:

@commands.command()
async def test(self, ctx):
    author = ctx.author
    message = await ctx.send('test')

    emote = '✔'

    for e in emote:
        await message.add_reaction(e)

    def check(reaction, user):
        return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)

    try:
        reaction, user = await self.client.wait_for('reaction_add', check=check, timeout=60)
    except asyncio.TimeoutError:
        await ctx.send("Timed out")
                return
    if str(reaction) == '✔':
        await ctx.send('some stuff')

答案3

得分: 1

这里的check的目的是,它是一个函数,当被调用时返回一个可以作为此wait_for的检查条件使用的函数。正确的使用方式是:

def check(author):
    def react_check(reaction, emoji):
        return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
    return check

await self.client.wait_for('reaction_add', check=check(author), timeout=60)
英文:

The purpose of check here is that it is a function that when called returns a function that can be used as a check for this wait_for. The correct way to use it is:

def check(author):
    def react_check(reaction, emoji):
        return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
    return check

await self.client.wait_for('reaction_add', check=check(author), timeout=60)

huangapple
  • 本文由 发表于 2020年1月3日 14:55:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定