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

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

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

问题

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

  1. 我正在尝试创建Discord机器人命令,其中机器人会创建表情符号,如果用户对该表情符号做出反应 - 机器人将执行一些所需的操作,但在检查部分出现以下错误:
  2. > discord.ext.commands.errors.CommandInvokeError: 命令引发了异常:TypeError: check()接受1个位置参数,但提供了2
  3. 机器人发送消息并附加表情符号,但是检查部分引发错误,我不知道如何解决它。
  4. 这是我的代码:
  5. 错误出现在代码的倒数第二行
  6. @commands.command()
  7. async def test(self, ctx):
  8. author = ctx.author
  9. message = await ctx.send('test')
  10. emote = '✔'
  11. for e in emote:
  12. await message.add_reaction(e)
  13. def check(author):
  14. def react_check(reaction, emoji):
  15. return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
  16. return check
  17. await self.client.wait_for('reaction_add', check=check(author, emote), timeout=60)
  18. 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

  1. @commands.command()
  2. async def test(self, ctx):
  3. author = ctx.author
  4. message = await ctx.send('test')
  5. emote = '✔'
  6. for e in emote:
  7. await message.add_reaction(e)
  8. def check(author):
  9. def react_check(reaction, emoji):
  10. return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
  11. return check
  12. await self.client.wait_for('reaction_add', check=check(author, emote), timeout=60)
  13. await ctx.send('some stuff after check')

答案1

得分: 1

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

  1. @client.event
  2. async def on_message(message):
  3. if message.content.startswith('$thumb'):
  4. channel = message.channel
  5. await channel.send('Send me that 👍 reaction, mate')
  6. def check(reaction, user):
  7. return user == message.author and str(reaction.emoji) == '👍'
  8. try:
  9. reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
  10. except asyncio.TimeoutError:
  11. await channel.send('👎')
  12. else:
  13. await channel.send('👍')

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

你实际上需要做的是:

  1. def check(author):
  2. def react_check(reaction, emoji):
  3. return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
  4. return check
  5. 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.

  1. @client.event
  2. async def on_message(message):
  3. if message.content.startswith('$thumb'):
  4. channel = message.channel
  5. await channel.send('Send me that 👍 reaction, mate')
  6. def check(reaction, user):
  7. return user == message.author and str(reaction.emoji) == '👍'
  8. try:
  9. reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
  10. except asyncio.TimeoutError:
  11. await channel.send('👎')
  12. else:
  13. 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:

  1. def check(author):
  2. def react_check(reaction, emoji):
  3. return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
  4. return check
  5. 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中,为了修复这个错误,它将稍微更改,但将正确工作。

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

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

  1. @commands.command()
  2. async def test(self, ctx):
  3. author = ctx.author
  4. message = await ctx.send('test')
  5. emote = '✔'
  6. for e in emote:
  7. await message.add_reaction(e)
  8. def check(reaction, user):
  9. return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)
  10. try:
  11. reaction, user = await self.client.wait_for('reaction_add', check=check, timeout=60)
  12. except asyncio.TimeoutError:
  13. await ctx.send("Timed out")
  14. return
  15. if str(reaction) == '✔':
  16. 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.

  1. def check(reaction, user):
  2. 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:

  1. @commands.command()
  2. async def test(self, ctx):
  3. author = ctx.author
  4. message = await ctx.send('test')
  5. emote = '✔'
  6. for e in emote:
  7. await message.add_reaction(e)
  8. def check(reaction, user):
  9. return (reaction.message.id == message.id) and (user.id == ctx.author.id) and (str(reaction) in emote)
  10. try:
  11. reaction, user = await self.client.wait_for('reaction_add', check=check, timeout=60)
  12. except asyncio.TimeoutError:
  13. await ctx.send("Timed out")
  14. return
  15. if str(reaction) == '✔':
  16. await ctx.send('some stuff')

答案3

得分: 1

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

  1. def check(author):
  2. def react_check(reaction, emoji):
  3. return message.author == author and reaction.message.id == msg.id and reaction.emoji == emoji
  4. return check
  5. 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:

  1. def check(author):
  2. def react_check(reaction, emoji):
  3. return message.author == author and reaction.message.id==msg.id and reaction.emoji==emoji
  4. return check
  5. 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:

确定