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

discord.py运行serverinfo命令触发CommandInvokeError报错怎么解决

问题排查与修复方案

错误原因

CommandInvokeError是discord.py对命令执行过程中抛出的异常的统一包装,结合你的代码,具体触发错误的原因如下:

  • 嵌入构造参数错误:discord.Embed类没有name构造参数,你需要使用title参数设置嵌入卡片的标题,错误参数会直接抛出构造异常。
  • 类型不匹配错误:ctx.guild.id为整数类型,你直接用+和字符串拼接会触发类型错误,必须先将id转为字符串再拼接,或使用f-string格式化。
  • 权限意图缺失(discord.py 2.x版本必现):discord.py 2.0及以上版本默认禁用成员敏感意图,未开启members意图时访问ctx.guild.members会返回空或抛出权限异常。
  • 冗余无效代码:pass_context参数在discord.py v1.0之后已默认开启,无需额外声明;重复调用两次set_thumbnail属于无效代码,不会报错但无实际作用。

修复步骤

1. 调整代码逻辑

修复后的完整代码示例:

import discord
from discord.ext import commands

# 配置网关意图,v2.x版本必须正确配置
intents = discord.Intents.default()
intents.members = True # 开启成员读取意图,需和开发者面板配置同步

# 初始化客户端,替换为你自己的命令前缀
client = commands.Bot(command_prefix="!", intents=intents)

@client.command()
async def serverinfo(ctx):
    guild = ctx.guild
    # 替换name参数为title
    embed = discord.Embed(title=f"{guild.name}'s info", color=0x176cd5)
    embed.add_field(name="guild Name", value=guild.name, inline=True)
    embed.add_field(name="Roles", value=len(guild.roles), inline=True)
    embed.add_field(name="Members", value=len(guild.members))
    embed.add_field(name="Channels", value=len(guild.channels))
    embed.add_field(name="Region", value=guild.region)
    embed.add_field(name="Verification Level", value=guild.verification_level)
    embed.add_field(name="Owner", value=guild.owner.mention)
    embed.add_field(name="Emojis", value=len(guild.emojis))
    embed.set_thumbnail(url=guild.icon_url)
    embed.set_author(name=guild.name, icon_url=guild.icon_url)
    # 使用f-string处理id拼接,避免类型错误
    embed.set_footer(text=f"Server ID is {guild.id}")
    await ctx.send(embed=embed)

2. 开启开发者面板特权意图

  • 打开Discord开发者平台,进入对应的机器人应用页面
  • 切换到「Bot」标签页,找到「Privileged Gateway Intents」区域
  • 打开Server Members Intent对应的开关,保存更改即可。

内容的提问来源于stack exchange,提问作者Crazy Jaguar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 03:33:02