Discord.js角色编辑命令触发DiscordAPIError[50035]求助
问题排查与修复
错误原因
- 异步操作未等待:你用
.then()处理角色编辑的异步逻辑,但后续直接执行interaction.editReply(),此时Embed的内容还没被异步回调修改,发送的是初始空Embed,触发Discord API的LIST_ITEM_VALUE_REQUIRED错误(空Embed不符合平台要求)。 - 内容覆盖逻辑错误:多个独立的
if-else分支会互相覆盖Embed内容,比如修改名称成功后,若其他参数未修改,后续的else分支会把Embed改成"未更改"提示,直接覆盖之前的成功信息。 - 最终判断逻辑异常:最后判断四个参数都存在的条件几乎不会触发(用户通常只修改部分参数),导致直接发送未正确初始化的Embed。
修复方案
改用async/await处理异步操作,统一收集需要修改的字段后一次性编辑角色,再统一构建Embed,避免回调和内容覆盖问题:
else if (subcommand === 'edit') { const { options } = interaction; // 直接获取Role对象,无需手动处理ID与缓存查询 const rid = options.getRole('role'); const name = options.get('name')?.value; const color = options.get('color')?.value; const hoist = options.get('hoist')?.value; const mentionable = options.get('mentionable')?.value; const embed = new EmbedBuilder(); const changes = {}; const fields = []; let hasChanged = false; // 收集所有需要修改的字段与变更记录 if (name !== undefined && rid.name !== name) { changes.name = name; fields.push({ name: '名稱', value: `${rid.name} → ${name}`, inline: true }); hasChanged = true; } if (color !== undefined && rid.hexColor !== color) { changes.color = color; fields.push({ name: '顏色', value: `${rid.hexColor} → ${color}`, inline: true }); hasChanged = true; } if (hoist !== undefined && rid.hoist !== hoist) { changes.hoist = hoist; fields.push({ name: '分開顯示', value: `${rid.hoist} → ${hoist}`, inline: true }); hasChanged = true; } if (mentionable !== undefined && rid.mentionable !== mentionable) { changes.mentionable = mentionable; fields.push({ name: '是否可被提及', value: `${rid.mentionable} → ${mentionable}`, inline: true }); hasChanged = true; } try { if (hasChanged) { // 一次性提交所有修改,减少API调用次数 await rid.edit(changes); embed.setDescription(`已編輯身分組 ${rid}`) .setColor(rid.hexColor) .addFields(fields); } else { embed.setColor('Red') .setDescription(`身分組 ${rid} 並未做出任何更改`); } await interaction.editReply({ embeds: [embed] }); } catch (err) { console.error('角色编辑失败:', err); embed.setColor('Red') .setDescription(`編輯身分組 ${rid} 時發生錯誤`); await interaction.editReply({ embeds: [embed] }); } }
关键改进点
- 直接使用
options.getRole('role')获取Role对象,省去手动查询缓存的步骤。 - 统一收集所有修改字段,一次性调用
rid.edit(),降低API请求频率。 - 用
async/await处理异步操作,确保角色编辑完成后再构建并发送Embed。 - 把所有变更记录收集到数组中统一添加到Embed,避免内容被覆盖。
- 统一捕获错误,编辑失败时给用户明确的错误提示。
内容的提问来源于stack exchange,提问作者Squl032
相关产品推荐
相关产品推荐

