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

JS函数调用时设置可选embed参数的代码实现问题求助

问题根源

你现有代码的核心错误是无论用户是否传入embed参数,都会执行embed = {}把传入的配置强制覆盖清空,同时没有把embed对象正确传入消息发送的参数中,额外的调试判断还会导致重复发送消息。

修正后代码

async function cmd({ lang, client, type, content, embed }) {
  // 设置默认content
  if (!content) {
    content = `Pong! ${client.ws.ping}ms !`;
  }
  // 构造回复参数
  const replyParams = { content };
  // 仅当传入合法embed对象时,添加embed到回复配置
  if (embed && typeof embed === 'object') {
    // 多数机器人框架要求embed放在数组中,字段名为embeds
    replyParams.embeds = [embed];
    // 如果你所用的框架要求直接传embed对象,字段为embed,改成下方写法即可
    // replyParams.embed = embed;
  }
  // 发送回复
  await type.reply(replyParams);
}

module.exports = cmd;

使用示例

不传embed(普通文字消息)

和原有调用方式完全兼容:

bot.commands.others.ping({
  lang: "tr",
  client: client,
  type: msg,
});

传入embed(带嵌入卡片的消息)

传入embed配置对象即可自动生效:

bot.commands.others.ping({
  lang: "tr",
  client: client,
  type: msg,
  embed: {title: 'smth'}
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 22:45:00