python - discord.py 模块中 MissingRequiredArgument 错误的解决方案是什么?
问题描述
我想创建一个机器人,它会在被调用时显示服务器信息,例如服务器名称、服务器所有者的名称、成员总数等。我多次阅读该模块的文档并尝试了很多东西。不管所有尝试如何,每当我通过命令调用机器人时,我都会收到此错误。顺便说一句,我正在使用 Python 3.9.2 和 1.6.0 版本的 discord.py 模块。我该如何解决这个问题?提前致谢!
我的代码:
@bot.command()
async def server(ctx, guild: discord.Guild):
await ctx.send(guild.name)
await ctx.send(guild.owner)
await ctx.send(guild.owner_id)
await ctx.send(guild.members)
await ctx.send(guild.member_count)
错误:
Ignoring exception in command server:
Traceback (most recent call last):
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 856, in invoke
await self.prepare(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 790, in prepare
await self._parse_arguments(ctx)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 706, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "...\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 542, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: guild is a required argument that is missing.
解决方案
你需要公会参数吗,你为什么不这样做:
@bot.command()
async def server(ctx):
await ctx.send(ctx.guild.name)
await ctx.send(ctx.guild.owner)
await ctx.send(ctx.guild.owner_id)
await ctx.send(ctx.guild.members)
await ctx.send(ctx.guild.member_count)
推荐阅读
- gwt - 带有 http 负载均衡器的 GWT 提供了无效的 SID 值
- python - 带有线程 Python 的 HTTP 请求
- amazon-dynamodb - K6 与 DynamoDB
- java - 如何使在方法之间传递的文件扫描器工作?
- macos - (Mac OS) 找不到匹配的射线分布
- odoo - oboo[12] : 如何仅显示登录用户创建的发票?
- laravel - Laravel 转换咨询 sql 一个 queryBuilder(Laravel)
- javascript - 如何设置 Jquery Toggle 默认显示或隐藏使用 html 类控件?
- android - 尝试使用 MutableLiveData 值时出现 NullPointerException
- php - Laravel 获取连接表的最新行