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

如何修复Discord斜杠命令机器人出现的Invalid Form Body报错

问题原因

discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In name: String value did not match validation regex.

该报错明确指向斜杠命令的name字段不符合Discord官方的正则校验规则,Discord对斜杠命令名称有强制要求:

  • 长度必须在1~32个字符之间
  • 只能包含小写英文字母、数字、下划线_、短横线-
  • 不允许包含大写字母、空格、其他特殊符号
  • 同服务器/全局范围内不能重名,且不区分大小写

你代码中@slash.slash装饰器里的name='Throw Banana'包含大写字母和空格,直接触发了校验失败。


修复步骤
  1. 首先修改斜杠命令的name参数,把'Throw Banana'替换为符合规则的名称,比如'throw_banana'
  2. 顺带修正Bot初始化的参数拼写错误:当前代码写的是单数intent=discord.Intents.all(),正确参数名为复数intents,否则会触发后续权限相关报错
  3. 注意脱敏的guild_ids里的0123456789在Python3.10+版本会触发“十进制数不能以0开头”的语法错误,实际使用时替换为你的真实服务器ID即可,不要保留开头的0

完整修复后参考代码
import discord
from discord.ext import commands
from discord_slash import SlashCommand

client = commands.Bot(
    command_prefix='/',
    help_command=None,
    intents=discord.Intents.all()
)
slash = SlashCommand(
    client,
    sync_commands=True)

guild_ids = [
    123456789 # 替换为你的实际服务器ID
]

@client.event
async def on_ready():
    print("*throws banana*")

@slash.slash(
    name='throw_banana',
    description='Throw a banana at an unsuspecting humanoid...',
    guild_ids=guild_ids)
async def throw_banana(ctx):
    await ctx.send(f'*throws banana at {ctx.author}')

client.run(
    '你的实际机器人token'
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 16:15:01