如何用switch结构实现Discord Bot !help命令的有无参数判断(JS)
解决Discord Bot帮助命令的实现问题
我来帮你搞定这个Discord Bot帮助命令的问题!先说说你现有代码的小问题:你写的break语句会直接跳出整个switch块,而不是当前的if分支,这会导致逻辑执行到break就直接退出switch,不过调整结构后就能实现需求。下面分两种方式给你提供可运行的解决方案:
一、用if/else结构实现(更直观易读)
假设你的命令解析逻辑已经能正确捕获!help命令,并且arguments数组是拆分后的命令参数(比如输入!help utility对应arguments = ["help", "utility"]),可以这样写:
// 当检测到help命令时 if (arguments[0] === "help") { let embed; // 无参数的情况(仅输入!help) if (!arguments[1]) { embed = new Discord.RichEmbed() .setTitle("General Commands") .setColor([60, 215, 115]) .setDescription("Commands used throughout the server") .setTimestamp(); } // 匹配utility分类的情况(!help utility) else if (arguments[1] === "utility") { embed = new Discord.RichEmbed() .setTitle("Utility Commands") .setColor([60, 215, 115]) .setDescription("Commands used by moderators") .setTimestamp(); } // 新增:处理用户输入未知分类的情况 else { embed = new Discord.RichEmbed() .setTitle("Unknown Category") .setColor([255, 0, 0]) .setDescription("Invalid command category! Try `general` or `utility`") .setTimestamp(); } // 统一发送生成的Embed消息 message.channel.send(embed); }
这里把embed变量声明提升到外部,避免重复声明,最后统一发送消息,逻辑更清晰。
二、用switch结构实现
如果想用switch来处理参数判断,有两种实用思路:
方式1:针对分类参数做switch匹配
直接把arguments[1]作为switch的判断目标,无参数时它的值是undefined,刚好可以作为一个case:
if (arguments[0] === "help") { let embed; switch(arguments[1]) { // 无参数的情况 case undefined: case "general": // 兼容用户输入!help general的情况 embed = new Discord.RichEmbed() .setTitle("General Commands") .setColor([60, 215, 115]) .setDescription("Commands used throughout the server") .setTimestamp(); break; // 匹配utility分类 case "utility": embed = new Discord.RichEmbed() .setTitle("Utility Commands") .setColor([60, 215, 115]) .setDescription("Commands used by moderators") .setTimestamp(); break; // 未知分类的默认处理 default: embed = new Discord.RichEmbed() .setTitle("Unknown Category") .setColor([255, 0, 0]) .setDescription("Invalid command category! Try `general` or `utility`") .setTimestamp(); break; } message.channel.send(embed); }
方式2:用switch(true)实现多条件判断
这种方式更灵活,适合处理复杂的条件逻辑,本质是把每个case当成独立的条件判断:
if (arguments[0] === "help") { let embed; switch(true) { // 无参数的情况 case !arguments[1]: embed = new Discord.RichEmbed() .setTitle("General Commands") .setColor([60, 215, 115]) .setDescription("Commands used throughout the server") .setTimestamp(); break; // 匹配utility分类 case arguments[1] === "utility": embed = new Discord.RichEmbed() .setTitle("Utility Commands") .setColor([60, 215, 115]) .setDescription("Commands used by moderators") .setTimestamp(); break; // 匹配general分类 case arguments[1] === "general": embed = new Discord.RichEmbed() .setTitle("General Commands") .setColor([60, 215, 115]) .setDescription("Commands used throughout the server") .setTimestamp(); break; // 默认分支 default: embed = new Discord.RichEmbed() .setTitle("Unknown Category") .setColor([255, 0, 0]) .setDescription("Invalid command category! Try `general` or `utility`") .setTimestamp(); break; } message.channel.send(embed); }
小提示
如果你的Discord.js版本是v12及以上,RichEmbed已经被官方废弃,建议改用MessageEmbed,用法几乎一致,只需要把new Discord.RichEmbed()替换成new Discord.MessageEmbed()即可。
另外,建议把命令分类的配置抽成一个独立对象,比如:
const commandCategories = { general: { title: "General Commands", description: "Commands used throughout the server", color: [60, 215, 115] }, utility: { title: "Utility Commands", description: "Commands used by moderators", color: [60, 215, 115] } };
后续添加新分类时只需要修改这个对象,不用重复编写Embed代码,维护起来会更方便。
内容的提问来源于stack exchange,提问作者Gage Brush
相关产品推荐
相关产品推荐

