首页 > 解决方案 > 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.

标签: pythonpython-3.xdiscord.py

解决方案


你需要公会参数吗,你为什么不这样做:

@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) 

推荐阅读