简体   繁体   中英

Emojis discord.js

for the past couple of days i've been super hung up on fixing this code to work when a user reacts to the message and i have no idea how, i've been also trying to make it work with global emojis and not just emojis on the specific guild. Could anybody show me what i have to do? I've not seen any documentation on finding global default discord emojis such as:laughing: etc.

Heres my code

const {
    MessageCollector
} = require("discord.js")
const MessageModel = require('../../models/message')
const {
    MessageEmbed
} = require('discord.js')
const {
    prefix
} = require('../../botconfig.json')
const {
    channels
} = require("./../../botconfig.json");
let msgCollectorFilter = (newMsg, originalMsg) => newMsg.author.id === originalMsg.author.id;
module.exports = {
    config: {
        name: "addreaction",
        category: "rolereactions",
        aliases: ['rr', 'rolereaction'],
        description: "creates a reaction",
        usage: "!rr <message id>",
    },
    run: async (bot, message, args) => {
        if(!message.member.hasPermission('MANAGE_GUILD')) return message.channel.send('You are missing the permission \`Manage server\`').then(m => m.delete({timeout:10000}))
        if (args.slice().length !== 1 && message.member.hasPermission("MANAGE_MESSAGES")) {
            const embed1 = new MessageEmbed()
                .setTitle("Enter atleast one Message Id, this message also can occur due to more than 1 message id used.")
                .setColor("RANDOM")
                .setDescription("**Usage:**\n!addreaction <Message Id>")
                .addField("**Important**", "The message id can be found by right clicking a message and clicking 'Copy ID'.")
                .setFooter("This message will be deleted after 15 seconds");
            let msg = await message.channel.send(embed1) || message.member.hasPermission("MANAGE_MESSAGES");
            await msg.delete({
                timeout: 15000
            }).catch(err => console.log(err));
            const logChannel = message.channel || message.guild.channels.cache.find(c => c.name === channels.logs);
        } else {
            try {
                let FetchedMessage = await message.channel.messages.fetch(args.join(" "));
                if (FetchedMessage && message.member.hasPermission("MANAGE_MESSAGES")) {
                    const emojiEmbed = new MessageEmbed()
                        .setColor("RANDOM")
                        .setFooter("This message will be removed after 20 seconds.")
                        .setDescription("Please provide all of the emoji names with the role name.\n Usage: <Emoji name>, <Role name>.")
                    message.channel.send(emojiEmbed)
                        .then(msg => msg.delete({
                            timeout: 20000
                        }))


                    let collector = new MessageCollector(message.channel, msgCollectorFilter.bind(null, message));
                    let emojiRoleMappings = new Map()
                    collector.on('collect', msg => {
                        let {
                            cache
                        } = bot.emojis;
                        if (msg.content.toLowerCase() === `${prefix}done` && message.member.hasPermission("MANAGE_MESSAGES")) {
                            collector.stop(/*console.log('Done command issued.')*/);
                            return message.channel.send("Role Reaction `Done` command was issued.")
                                .then(msg => msg.delete({
                                    timeout: 3500
                                }));
                        }
                        let [emojiName, roleName] = msg.content.split(/,\s+/);
                        if (!emojiName && !roleName) return;
                        let emoji = cache.find(emoji => emoji.name.toLowerCase() === emojiName.toLowerCase());
                        if (!emoji && message.member.hasPermission("MANAGE_MESSAGES")) {
                            msg.channel.send("`Emoji does not exist. Try again.`")
                                .then(msg => msg.delete({
                                    timeout: 3500
                                }))
                                .catch(err => console.log(err))
                            return;
                        }

                        let role = msg.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
                        if (!role) {
                            msg.channel.send("`Role does not exist. Try again.`")
                                .then(msg => msg.delete({
                                    timeout: 3500
                                }))
                                .catch(err => console.log(err))
                            return;
                        }
                        const reactEmbed = new MessageEmbed()
                            .setColor("RANDOM")
                            .setFooter("This message will be removed after 10 seconds.")
                            .setDescription("I reacted to your message, if you wish to add another reaction follow the same format\n Usage: <Emoji name>, <Role name>.");
                        FetchedMessage.react(emoji)
                            .then(message.channel.send(reactEmbed).then(msg => msg.delete({
                                timeout: 10000
                            })))
                            .catch(err => console.log(err));
                        let emojirole = {};
                        emojirole[emoji.id] = role.id;
                        bot.cachedMessageReactions.set(FetchedMessage.id, emojirole);
                        emojiRoleMappings.set(emoji.id, role.id);

                    });
                    collector.on('end', async (collected, reason) => {
                        let findMsgDocument = await MessageModel.findOne({
                            messageId: FetchedMessage.id
                        }).catch(err => console.log(err));

                        if (findMsgDocument) {
                           // console.log("The message exists.. Don't save me plz.");
                            message.channel.send("A role reaction for this message exists already...").then(msg => msg.delete({
                                timeout: 3500
                            }));
                        } else {
                            let msg = await message.channel.send("Saving...")
                            let dbMsgModel = new MessageModel({
                                messageId: FetchedMessage.id,
                                emojiRoleMappings: emojiRoleMappings

                            });
                            dbMsgModel.save().catch(err => console.log(err))
                                .then(msg.edit("Saved!").then(msg => msg.delete({
                                    timeout: 3500
                                })));
                        }
                    });

                }
            } catch (err) {
                console.log(err)
    
                let msg = await message.channel.send("This message doesnt exist.");
                await msg.delete({
                    timeout: 3500
                }).catch(err => console.log(err));
            }

        }


    }
}

To get a default emoji, for example :laughing :, simply send the emoji in discord with a backslash in front of it: \:laughing:

Copy the result and paste it in your code. This will only work on PC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM