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

discord.js实现反应角色时自定义表情报Unknown Emoji错误如何解决?

错误原因
  • 核心错误是变量引用错误:你已经通过client.emojis.cache.get()获取到了自定义表情对象,后续赋值时错误地将变量名用反引号包裹为字符串,导致传入react()方法的是纯文本"FortniteEmoji"而非真实表情对象,Discord接口无法识别到对应表情,因此抛出Unknown Emoji报错。
  • 次要逻辑错误:自定义表情的匹配不能直接比对reaction.emoji.name,需要通过表情ID做匹配,避免不同服务器存在同名表情的冲突,也符合discord.js自定义表情的判断规范。
修复步骤
  1. 修正表情变量的赋值逻辑,移除多余的字符串包裹:
// 原错误代码
const RavineaEmoji = `FortniteEmoji`;
const YouAgainEmoji = `ApexEmoji`;

// 替换为
const RavineaEmoji = FortniteEmoji;
const YouAgainEmoji = ApexEmoji;
  1. 增加表情获取失败的兜底判断,避免空值报错:
    在表情获取代码后新增判断:
let FortniteEmoji = client.emojis.cache.get("879625063643635713");
let ApexEmoji = client.emojis.cache.get("879624843920809985");
// 新增兜底判断
if (!FortniteEmoji || !ApexEmoji) return message.channel.send("无法获取指定自定义表情,请检查表情ID是否正确、机器人是否在表情所属服务器");
  1. 修正反应事件中的表情匹配逻辑,改用表情ID比对:
    将messageReactionAdd和messageReactionRemove事件中的如下判断:
if (reaction.emoji.name === RavineaEmoji)
if (reaction.emoji.name === YouAgainEmoji)

替换为:

if (reaction.emoji.id === RavineaEmoji.id)
if (reaction.emoji.id === YouAgainEmoji.id)
完整修复后代码
const Discord = require("discord.js");
const { MessageEmbed } = require("discord.js");
const { Color } = require("../../config.js");
module.exports = {
    name: 'reactionrole',
    aliases: [],
    permissions: [],
    description: "Sets up a reaction role message!",
    run: async (client, message, args) => {
        let FortniteEmoji = client.emojis.cache.get("879625063643635713");
        let ApexEmoji = client.emojis.cache.get("879624843920809985");
        // 新增表情获取失败判断
        if (!FortniteEmoji || !ApexEmoji) return message.channel.send("无法获取指定自定义表情,请检查ID是否正确");
        const channel = '869871201621790741';
        const RavineaRole = message.guild.roles.cache.find(role => role.name === "Fortnite Tryouts");
        const YouAgainRole = message.guild.roles.cache.find(role => role.name === "Apex Legends Tryouts");
 
        // 修正变量引用
        const RavineaEmoji = FortniteEmoji;
        const YouAgainEmoji = ApexEmoji;
 
        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Tryout For The Following Games!')
            .setDescription('Enter The Tryouts!\n\n'
                + `${RavineaEmoji} for Fortnite\n`
                + `${YouAgainEmoji} for Apex Legends`);
 
        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(RavineaEmoji);
        messageEmbed.react(YouAgainEmoji);
 
        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
            if (reaction.message.channel.id == channel) {
                // 改用表情ID匹配
                if (reaction.emoji.id === RavineaEmoji.id) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(RavineaRole);
                }
                if (reaction.emoji.id === YouAgainEmoji.id) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(YouAgainRole);
                }
            } else {
                return;
            }
 
        });
 
        client.on('messageReactionRemove', async (reaction, user) => {
 
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;
 
 
            if (reaction.message.channel.id == channel) {
                // 改用表情ID匹配
                if (reaction.emoji.id === RavineaEmoji.id) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(RavineaRole);
                }
                if (reaction.emoji.id === YouAgainEmoji.id) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(YouAgainRole);
                }
            } else {
                return;
            }
        });
    }
}
额外注意事项
  • 填写的自定义表情ID必须是机器人已加入的服务器内的表情ID,跨服表情需要机器人拥有使用外部表情权限
  • 执行命令的频道需要给机器人开放添加反应、管理消息权限,避免反应添加失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 01:18:02