如何为discord.js发送到applicationChannelId的申请消息添加审核按钮
实现方案
1. 修改申请提交后发送至审核频道的代码
找到你代码中提交申请的逻辑最后一行client.channels.cache.get(applicationChannelId).send(application);,替换为以下代码,实现发送申请时附带通过/拒绝按钮:
// 替换原有直接发送embed的代码,改为发送带按钮的消息 client.api.channels(applicationChannelId).messages.post({ data: { embeds: [application], components: [{ type: 1, components: [ { type: 2, style: 3, // 绿色样式代表通过 label: "通过", // 按钮ID拼接申请人ID,后续点击时可识别对应申请人 custom_id: `application_approve_${member.id}` }, { type: 2, style: 4, // 红色样式代表拒绝 label: "拒绝", custom_id: `application_reject_${member.id}` } ] }] } })
2. 新增审核按钮点击的处理逻辑
在已有的client.ws.on("INTERACTION_CREATE", async (interaction) => { ... })事件中,原有send_application按钮逻辑的同级位置,新增以下代码:
// 新增审核按钮判断逻辑 if (buttonId.startsWith('application_approve_') || buttonId.startsWith('application_reject_')) { const admins = ['741483726688747541']; // 可复用你原有定义的管理员ID数组 // 权限校验:仅管理员可操作 if (!admins.includes(userId)) { client.api.interactions(interaction.id, interaction.token).callback.post({ data: { type: 4, data: { content: "你没有权限操作审核按钮", flags: 64 // 仅操作人可见 } } }) return } // 拆分按钮ID获取申请人ID const applicantId = buttonId.split('_')[2] const guild = client.guilds.resolve(guildId) const applicantMember = guild.member(applicantId) const isApprove = buttonId.startsWith('application_approve_') const teamRoleId = "761996603434598460" // 替换为你自己的团队角色ID try { if (isApprove) { // 通过逻辑:加角色、发通知 const teamRole = guild.roles.cache.find(r => r.id === teamRoleId) if (teamRole && applicantMember) await applicantMember.roles.add(teamRole) if (applicantMember) await applicantMember.send("你已成功通过Blind Spot团队申请!") } else { // 拒绝逻辑:发通知 if (applicantMember) await applicantMember.send("很遗憾,你的Blind Spot团队申请未通过审核。") } // 更新原审核消息,禁用按钮避免重复操作 client.api.interactions(interaction.id, interaction.token).callback.post({ data: { type: 7, data: { embeds: interaction.message.embeds, components: [{ type: 1, components: [ { type: 2, style: 3, label: isApprove ? "已通过" : "通过", custom_id: buttonId, disabled: true }, { type: 2, style: 4, label: isApprove ? "拒绝" : "已拒绝", custom_id: buttonId, disabled: true } ] }] } } }) } catch (err) { // 异常捕获:避免申请人离服、关闭私信等场景导致崩溃 client.api.interactions(interaction.id, interaction.token).callback.post({ data: { type: 4, data: { content: `操作失败:可能申请人已离开服务器或关闭了私信权限`, flags: 64 } } }) } return }
注意事项
- 记得将代码中的
teamRoleId替换为你实际的团队角色ID admins数组可直接复用你原有定义的管理员ID列表,不需要重复定义- 所有异步操作都加了异常捕获,不会因为特殊场景直接崩溃
内容的提问来源于stack exchange,提问作者foreverlost
相关产品推荐
相关产品推荐

