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

Slash Command执行失败求助:代码仅兼容Legacy命令

问题分析与修复方案

错误原因

触发ValueError的核心问题:

  • Slash命令参数顺序违规:interaction必须作为函数的第一个参数,且不能和*args这类可变参数混用,Nextcord的Slash命令不支持这种参数组合的类型提示。
  • Slash命令不兼容*args:这类可变位置参数无法被Slash命令的选项系统识别,必须通过明确的类型提示参数(如msg: str)接收用户输入。

修复后的完整代码

from nextcord import Interaction, File
from PIL import Image, ImageDraw, ImageFont

@bot.slash_command(name="dog_message", description="Message and share to your friends!", guild_ids=[serverID])
async def furry_message(interaction: Interaction, msg: str):
    img = Image.open("dog.png")
    font = ImageFont.truetype("PatrickHand-Regular.ttf", 50)

    cx, cy = (200, 300)
    draw = ImageDraw.Draw(img)
    draw.text((cx, cy), msg, (0, 0, 0), font=font)
    img.save("dog_edited.png")

    with open("dog_edited.png", "rb") as f:
        img_file = File(f)
        await interaction.response.send_message(file=img_file)

关键修复点

  • 调整参数顺序:将interaction: Interaction移至函数第一个参数位置,移除*args,用明确的msg: str接收用户输入。
  • 统一文件名:保存和读取的文件名保持一致(均为dog_edited.png),避免文件找不到的问题。
  • 正确创建文件对象:使用nextcord.File()替代错误的file(f),确保Discord能正确解析文件。
  • 直接使用参数:Slash命令会自动将用户输入的内容传入msg参数,无需手动拼接参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 20:55:11