【问题标题】:ctx is a required argumentctx 是必需的参数
【发布时间】:2019-10-14 14:23:35
【问题描述】:

我正在尝试制作一个简单的音乐机器人。当我执行命令时出现此错误:discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

这是我的代码:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix= "-")

class VoiceConnectionError(commands.CommandError):
    """Custom Exception class for connection errors."""
    ...

class InvalidVoiceChannel(VoiceConnectionError):
    """Exception for cases of invalid Voice Channels."""
    ...

@bot.event
async def on_ready():
    print('Bot ready')

@bot.command(name='connect', aliases=['join'])
async def connect(self, ctx, *, channel: discord.VoiceChannel=None):
    await ctx.send(f'Connected to: **{channel}**', delete_after=10)


bot.run('TOKEN')

该命令应该将机器人移动到不和谐的语音通道中。

这是完整的回溯:

Full traceback:
Ignoring exception in command connect:
Traceback (most recent call last):
  File "/anaconda3/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 859, in invoke
    await ctx.command.invoke(ctx)
  File "/anaconda3/lib/python3.6/site-packages/discord/ext/commands/core.py", line 718, in invoke
    await self.prepare(ctx)
  File "/anaconda3/lib/python3.6/site-packages/discord/ext/commands/core.py", line 682, in prepare
    await self._parse_arguments(ctx)
  File "/anaconda3/lib/python3.6/site-packages/discord/ext/commands/core.py", line 596, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "/anaconda3/lib/python3.6/site-packages/discord/ext/commands/core.py", line 442, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

【问题讨论】:

  • 你能显示完整的回溯吗?

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

您的命令应该只接受self 参数,如果它们是part of a cog。删除参数:

@bot.command(name='connect', aliases=['join'], pass_context=True)
async def connect(ctx, *, channel: discord.VoiceChannel=None):
    ...

【讨论】:

    猜你喜欢
    • 2018-12-12
    • 2021-01-24
    • 2021-02-01
    • 2020-04-02
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2015-11-09
    • 2020-08-30
    相关资源
    最近更新 更多