【问题标题】:missing 1 required positional argument: 'ctx'缺少 1 个必需的位置参数:“ctx”
【发布时间】:2021-02-01 06:32:36
【问题描述】:

我试图让我的机器人在用户对消息做出反应后赋予角色,但每次我都遇到同样的错误。我不明白错误是什么意思。有人可以帮忙吗? 这是代码:

import discord
from discord.ext import commands
from discord import Member
from discord.ext.commands import has_permissions
from discord.ext import tasks


#sets the command prefix
Client = commands.Bot(command_prefix = "!")

#print Nuggie is ready in the console when the bot is activeted
@Client.event
async def on_ready():
    print("Bot is ready")

@Client.event
async def on_ready(ctx):
    myRole = Role.id("Member")
    channel = discord.utils.get(ctx.guild.channels, name=input("What is the name of the channel?  "))
    channel_id = channel.id
    channel = Client.get_channel(channel_id)
    role = discord.utils.get(message.server.roles, name=myRole)
    message = await Client.send_message(channel, "React with the tick to get access to the server!")
    while True:
        reaction = await Client.wait_for_reaction(emoji="✔", message=message)
        await Client.add_roles(reaction.message.author, role)

我得到的错误是:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
TypeError: on_ready() missing 1 required positional argument: 'ctx'

【问题讨论】:

  • 你使用的是哪个版本的 discord.py,rewrite 还是 repl.it?

标签: python discord.py


【解决方案1】:

函数on_ready被定义了两次,当第二次定义时,第一次被删除。错误告诉你函数调用需要一个参数传递给它(ctx)

阅读不和谐文档:https://discordpy.readthedocs.io/en/latest/quickstart.html#a-minimal-bot。 on_ready 方法的定义必须不带任何参数。

【讨论】:

  • 感谢您的评论!但是即使我将代码更新为一个 on_ready 并添加了 ctx ,它仍然会给出相同的错误。
  • 成功了!现在我收到另一个与此无关的错误,但您已修复它!
猜你喜欢
  • 2020-04-02
  • 1970-01-01
  • 2021-01-16
  • 2014-09-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 2017-03-27
相关资源
最近更新 更多