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

使用discord.py斜杠命令触发「用户名含discord」异常的问题

解决discord.py斜杠命令触发「用户名不能包含'discord'」异常

使用discord.py开发斜杠命令时,触发了如下HTTP异常,但机器人和用户的名称中均不含"discord"字符。尝试更换不同机器人/名称、不同用户,以及改用discord.ext.commands.Bot,问题依旧。

相关代码

import os
import discord
from dotenv import load_dotenv
from discord import app_commands

load_dotenv()

token = os.getenv('TOKEN')

intents = discord.Intents.all()

client = discord.Client(intents=intents)

tree = app_commands.CommandTree(client)

@client.event
async def on_ready():
    await tree.sync(guild=discord.Object(id=1128263298152927294))
    print('Command tree synced.')

@tree.command(description="Sends the bot's latency.",name='ping',guild=discord.Object(1128263298152927294))
async def ping(ctx):
    await ctx.response.send_message("Hello!")

client.run(token)

抛出的异常信息

discord.app_commands.errors.CommandInvokeError: Command 'ping' raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In name: Username cannot contain "discord"

解决方案

  • 检查机器人全局用户名:登录Discord开发者门户,进入机器人设置页面,确认机器人的全局用户名(非服务器内昵称)不包含"discord"相关字符,包括大小写变体(如Discord)、相似拼写(如disc0rd)等,如有则修改后重试。
  • 升级discord.py到最新稳定版:执行命令pip install --upgrade discord.py,旧版本可能存在API交互时的参数错误导致误报。
  • 测试全局命令同步:临时修改同步代码为全局同步,取消guild指定,验证是否能正常运行:
    @client.event
    async def on_ready():
        await tree.sync()
        print('Command tree synced globally.')
    
  • 重新邀请机器人:确保邀请机器人时勾选了applications.commands权限,重新生成邀请链接并邀请机器人到服务器。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 12:33:23