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

discord.js-commando嵌入命令报错:未注册colorcode类型求替代方案

解决Discord.js-Commando中"colorcode"类型未注册的错误

嘿,我看到你在开发Embed命令时碰到了RangeError: Argument type "colorcode" isn't registered的问题——这是因为Discord.js-Commando根本没有内置名为colorcode的参数类型,咱们得换个合适的类型替代,同时修正代码里的其他小问题,让命令正常跑起来。

具体解决步骤:

  1. 替换参数类型为string
    Commando的内置参数类型里没有专门的颜色代码类型,用string就能接收用户输入的颜色值(不管是十六进制代码、颜色名称还是RANDOM)。

  2. 修正run函数的参数解构
    你原来的run函数只解构了text,漏掉了color参数,这样调用setColor(color)会直接报错,得把color也加进解构列表里。

  3. 处理颜色值逻辑
    当用户输入RANDOM时,直接给setColor传'RANDOM'就行;如果是其他颜色代码或名称,直接传入即可(Discord.js的setColor方法支持十六进制、RGB数组、标准颜色名称等格式)。

修正后的完整代码:

const { Command } = require('discord.js-commando');
// 注意:如果你的discord.js版本是v12及以上,建议用MessageEmbed替代RichEmbed
const { RichEmbed } = require('discord.js');

module.exports = class EmbedCommand extends Command {
  constructor(client) {
    super(client, {
      name: 'embed',
      group: 'extra commands',
      memberName: 'embed',
      description: 'Replies with the text you provide, But in Embed.',
      args: [
        { 
          key: "text", 
          prompt: "What Should the Embed Say?", 
          type: "string" 
        },
        { 
          key: "color", 
          prompt: "What Color should the Embed be? (Use Color Codes.). Use RANDOM for Random Color. (Must Be All Caps!).", 
          type: 'string' // 把colorcode替换为string
        }
      ]
    });
  }

  run(msg, { text, color }) { // 这里新增color的解构
    msg.delete();
    let embed = new RichEmbed()
      .setTitle(text)
      .setColor(color.toUpperCase() === 'RANDOM' ? 'RANDOM' : color); // 处理RANDOM的特殊情况

    msg.embed(embed);
  }
};

额外提示:

如果你的项目使用的是Discord.js v12或更高版本,RichEmbed已经被废弃,建议替换成MessageEmbed:

const { MessageEmbed } = require('discord.js');
// 然后把new RichEmbed()改成new MessageEmbed()

这样修改后,你的命令就能正常接收用户输入的颜色值(包括RANDOM随机颜色),不会再出现类型未注册的错误啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:35:25