You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py无参数!info命令获取Server对象报错求助

Fixing the MissingRequiredArgument Error for Your !info Command

It looks like the main issue here stems from using an older version of discord.py where commands don’t automatically pass the context (ctx) to your function unless you explicitly enable it. Let’s walk through the fixes step by step:

Key Issues in Your Code

  1. Missing pass_context=True: In discord.py v0.16.x (the version your error traceback points to), you need to add pass_context=True to the @bot.command() decorator to make the context available to your function. Without this, the bot doesn’t pass ctx, hence the "MissingRequiredArgument" error.
  2. Outdated Coroutine Syntax: Using @asyncio.coroutine with a regular def is legacy syntax—switching to async def is cleaner and aligns with modern async Python practices.
  3. Incorrect Server Access: ctx.message.author.server isn’t the right way to get the server where the command was sent. Use ctx.message.server instead (since the message itself is tied to the server’s channel).
  4. Unformatted Output: Your print statement concatenates strings without spaces or newlines, making the output hard to read.

Corrected Code

@bot.command(pass_context=True)
async def info(ctx):
    # Retrieve the server from the message context
    server = ctx.message.server
    server_name = server.name
    server_id = server.id
    server_owner = server.owner.name
    
    # Print neatly formatted server information
    print(f"Server Name: {server_name}")
    print(f"Server ID: {server_id}")
    print(f"Server Owner: {server_owner}")

Additional Notes

  • If you upgrade to a newer discord.py version (v1.0+), server was renamed to guild, so you’d use ctx.message.guild instead. Also, pass_context=True is no longer required—context is passed automatically in v1.0+.
  • Basic commands like this don’t need special bot permissions, but ensure your bot is properly invited to the server with standard access.

Content of the question originates from Stack Exchange, asked by TermoZour

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:23:49