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

Deno环境下Discord API斜杠命令发送embeds及附件失败问题

Discord API 斜杠命令响应携带embeds/attachments字段报错

问题背景

  • 运行环境为Deno,使用自行适配Deno的discord-api-types类型库,未使用discord.js框架,部署在Serverless架构下响应斜杠命令的ApplicationCommandType请求
  • 响应体仅返回content字段时消息可正常发送,携带embeds或attachments字段时请求失败
  • 暂无法获取Discord侧返回的错误详情,无法定位问题根因

复现代码

import {
  APIApplicationCommandInteraction,
  APIInteractionResponse,
  InteractionResponseType,
  APIAttachment,
  APIEmbed
} from "../lib/discord-api-types/payloads/index.ts";

import { Snowflake } from "../lib/discord-api-types/globals.ts";

import { DiscordSnowflake as sf } from "../lib/snowflake/DiscordSnowflake.ts";

export const onApplicationCommand = (
  interaction: APIApplicationCommandInteraction
) => {
  let res: APIInteractionResponse = {
    type: InteractionResponseType.ChannelMessageWithSource,
    data: {
      // 可正常返回
      content: "hello"

      // 报错写法1:embeds
      // embeds: [
      //   {url: "https://example.com/displayicon.png"} as APIEmbed
      // ]

      // 报错写法2:attachments
      // attachments: [
      //   {
      //     id: sf.generate().toString() as Snowflake,
      //     filename: "displayicon.png",
      //     content_type: "image/png",
      //     size: 56056,
      //     url: "https://example.com/displayicon.png",
      //     proxy_url: "https://example.com/displayicon.png",
      //   } as APIAttachment,
      // ],
    },
  };

  return res;
};
解决方法

第一步:先获取Discord侧的错误详情

你现在拿不到错误的核心原因是没有读取交互回调接口的响应内容:

  • 向Discord发送交互响应的POST请求,只要参数不符合规范会直接返回400状态码,响应体内会明确标注哪个字段传错了、错误原因是什么,直接打印该响应的文本内容即可定位具体问题
  • 额外排查项:交互回调必须在收到交互请求后的3秒内发出,Serverless函数冷启动超时也会导致请求失败,可以先打印函数执行耗时确认是否触发超时限制

第二步:修正现有代码的参数错误

你当前的两种报错写法都存在参数不符合接口规范的问题:

1. embeds字段写法错误

你构造的embed对象仅包含url字段,不符合接口要求:

  • Discord不允许发送空embed,embed对象必须至少包含title、description、fields、image、thumbnail、footer、author中的一个有效字段
  • 你如果要发送图片,不能将图片地址放在embed的根url字段下,需要放在image.url属性中,正确示例:
embeds: [
  {
    image: {
      url: "你的图片地址"
    }
  } as APIEmbed
]

2. attachments字段写法错误

你当前对attachments字段的用法存在认知错误:

  • url、proxy_url、size、content_type这些字段是Discord接收完你上传的附件后,返回给你的附件元数据,不是你请求时需要传入的参数
  • 初始交互响应如果要携带附件,必须使用multipart/form-data格式发送请求:将文件二进制内容放在form-data的files[n]字段,JSON格式的响应体放在payload_json字段,请求体里的attachments数组仅需要传入id、filename、description(可选)这几个自定义字段,多余字段不要传
  • 如果你只是需要展示远程图片,完全不需要使用attachments字段,直接按上述embed的写法嵌入远程图片地址即可,Discord会自动抓取远程资源生成预览。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 05:00:57