python - ctx 是必需的参数
问题描述
我正在尝试制作一个简单的音乐机器人。当我执行命令时,我收到此错误: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.
解决方案
self
如果它们是cog 的一部分,您的命令应该只接受参数。删除参数:
@bot.command(name='connect', aliases=['join'], pass_context=True)
async def connect(ctx, *, channel: discord.VoiceChannel=None):
...
推荐阅读
- vue.js - 如何在 nuxt axios instanse 中添加令牌
- python - 如何使用日期作为过滤器将列添加到数据框中
- docker - 如何通过“docker logs”获取嵌入在 docker 容器中的 VM 的控制台日志
- scala - 如何解决编译时的类型错误:Any to T?
- mysql - MySQL 根据存储过程中传递的 id 添加新的 LIST 分区?
- c++ - 我的应用程序名称在运行后更改,但它没有文件扩展名,我希望它在 .exe 中
- c++ - 数独求解器生成值为 50+ 的单元格
- c# - 我是否正确理解如何通过 RSA + AES 加密 TCP 流量?
- javascript - 使用过滤器获取对象的数据
- asp.net-core-mvc - ASP.NET Core MVC 中的类别/子类别(/subsubcategory*)路由