如何让Embed的.setFooter方法在未传参数时设为null?
- 核心需求:让
setFooter和其他方法保持一致逻辑——未指定文本/图标参数时,对应字段设为null;若两个参数都为空,则整个footer设为null。
代码实现方案
.setTitle(args[1] ? args[1] : null) .setDescription(args[2] ? args[2] : null) // 处理setFooter的空值逻辑 .setFooter( // 先判断是否有有效参数,无则传null,有则生成对应对象 (args[3] || args[4]) ? { text: args[3] ? args[3] : null, iconURL: args[4] ? args[4] : null } : null ) .setThumbnail(args[5] ? args[5] : null) .setImage(args[6] ? args[6] : null) .setColor(args[7] ? args[7] : null);
逻辑说明
- 外层判断:
(args[3] || args[4])确保只有当文本或图标至少有一个有效时,才生成footer对象;如果两个参数都为空,直接传null,避免创建无内容的footer。 - 字段单独处理:对象内的
text和iconURL各自判断,空值时设为null,和setTitle等方法的逻辑完全统一。
简化版(仅处理字段为空,不判断整体)
如果不需要判断整体是否为空,只想让单个字段为空时设为null,直接这样写即可:
.setFooter({ text: args[3] ? args[3] : null, iconURL: args[4] ? args[4] : null })
内容的提问来源于stack exchange,提问作者coder1111111111111111111111111
相关产品推荐
相关产品推荐

