【问题标题】:How do you add your custom emoji in discord.js code?如何在 discord.js 代码中添加自定义表情符号?
【发布时间】:2021-08-24 17:20:48
【问题描述】:

每当我运行我的代码时,我都会收到此错误

(node:552) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Emoji.

我正在尝试添加自己的表情符号,但这不起作用,我是 discord.js 的初学者 这是我第一次实际使用reactionroles,所以我不知道如何使用它们。使用正常的表情符号工作,但我想使用我的自定义表情符号

这是我的代码:

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");
        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) {
                if (reaction.emoji.name === RavineaEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(RavineaRole);
                }
                if (reaction.emoji.name === YouAgainEmoji) {
                    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) {
                if (reaction.emoji.name === RavineaEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(RavineaRole);
                }
                if (reaction.emoji.name === YouAgainEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(YouAgainRole);
                }
            } else {
                return;
            }
        });
    }
 
}   


【问题讨论】:

  • 你不应该将事件放在这样的命令文件中,为它们创建一个单独的文件

标签: javascript discord discord.js


【解决方案1】:

当您将表情符号定义为字符串时,它会出现,因此您尝试使用字符串对消息做出反应,但这是行不通的。

解决方案:

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");
        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) {
                if (reaction.emoji.name === RavineaEmoji)
                    await reaction.message.guild.members.cache.get(user.id).roles.add(RavineaRole);
                if (reaction.emoji.name === YouAgainEmoji)
                    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) {
                if (reaction.emoji.name === RavineaEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(RavineaRole);
                }
                if (reaction.emoji.name === YouAgainEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(YouAgainRole);
                }
            } else {
                return;
            }
        });
    }
}

【讨论】:

  • @MercuryDaGod 在这种情况下,表情符号 ID 不正确。
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2022-11-22
  • 1970-01-01
  • 2022-07-14
相关资源
最近更新 更多