如何修复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'包含大写字母和空格,直接触发了校验失败。
修复步骤
- 首先修改斜杠命令的
name参数,把'Throw Banana'替换为符合规则的名称,比如'throw_banana' - 顺带修正Bot初始化的参数拼写错误:当前代码写的是单数
intent=discord.Intents.all(),正确参数名为复数intents,否则会触发后续权限相关报错 - 注意脱敏的
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
相关产品推荐
相关产品推荐

