Discord.py“ ctx是缺少的必需参数。”

时间:2020-10-09 17:07:27

标签: python command bots discord.py ctx

我希望我的漫游器以hi回复|hello

import discord
from discord.ext import commands
from discord import Game

bot = commands.Bot(command_prefix = "|")
bot.remove_command('help')

#just some console feedback thing, this works fine, dont think it matters here
@bot.event 
async def on_ready():
    await bot.change_presence(game=Game(name="The Gaming"))
    print('All systems online!')
    print('Bot name: '+ bot.user.name)
    servers = list(bot.servers)
    print("Connected to " + str(len(bot.servers)) + " servers")
    for x in range(len(servers)):
        print(' - ' + servers[x-1].name)

#the thing that actually refuses to work
@bot.command()
async def hello(ctx):
    await ctx.send('hi')

bot.run('████████████████████████████████████████████')

当发送hi时,机器人会在控制台中显示以下错误消息,而不是用|hello回复|hello

Ignoring exception in command hello
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 345, in prepare
    yield from self._parse_arguments(ctx)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 304, in _parse_arguments
    transformed = yield from self.transform(ctx, param)
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 212, in transform
    raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

我已经检查了所有依赖项,似乎所有东西都已安装,这个精确的代码在一个月前完美运行了。

我还看到了有关此错误的其他多个问题,但是所有这些问题都使用了更复杂的代码,而我只是没有足够的经验来理解。

我怎样才能真正做到这一点?

1 个答案:

答案 0 :(得分:0)

该命令没有任何问题。另外,bot.servers不存在,bot.guilds


@bot.event 
async def on_ready():
    await bot.change_presence(activity=discord.Game(name="The Gaming"))
    print('All systems online!')
    print('Bot name: '+ bot.user.name)
    servers = list(bot.guilds)
    print("Connected to " + str(len(bot.guilds)) + " servers")
    for x in range(len(servers)):
        print(' - ' + servers[x-1].name)

#There's nothing wrong with this command
@bot.command()
async def hello(ctx):
    await ctx.send('hi')

我没有发现hello命令有任何错误。我想您正在使用旧版本的discord.py。所以你应该切换到discord.py rewrite