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

PyCord Discord Bot斜杠命令双参数问题:命令无法运行求助

多参数Discord斜杠命令修复方案

核心问题排查与修复

1. 字符串输出格式修正

原代码中ctx.send的字符串写法存在冗余,合并为单个格式化字符串可避免潜在的显示异常:

await ctx.send(f'{name} {collar}\nhttp://imgur.com/gallery/YiMUiop')

2. 版本兼容性验证

@bot.slash_command和@discord.option是discord.py 2.0及以上版本的特性,若版本过低需升级:

pip install -U discord.py

3. 命令同步与权限配置

  • 邀请机器人时必须勾选applications.commands权限,否则无法注册斜杠命令。
  • 开发环境下可添加命令同步逻辑,确保命令快速生效:
    @bot.event
    async def on_ready():
        await bot.sync_commands()
        print(f'已登录为 {bot.user}')
    

4. 参数类型一致性检查

collar参数默认值为字符串类型,与函数定义的str类型匹配,若需接收数字可改为int类型,同步调整默认值为数字(如1123)。

修复后完整代码

import os
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())

@bot.slash_command(name='greet', description='Greet someone!')
@discord.option(
    "name",
    description="Enter Name in format: John Doe",
    required=False,
    default='John Doe'
)
@discord.option(
    "collar",
    description="Enter Collar number e.g. 1789",
    required=False,
    default='1123'
)
async def greet(
    ctx: discord.ApplicationContext,
    name: str,
    collar: str
):
    await ctx.send(f'{name} {collar}\nhttp://imgur.com/gallery/YiMUiop')

@bot.event
async def on_ready():
    await bot.sync_commands()
    print(f'已登录为 {bot.user}')

bot.run(os.getenv('TOKEN'))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 20:25:25