简体   繁体   中英

(Discord.js) FetchMessages in specific channel

I'm a beginner in bot creation but I have a few code bases. I've created a report command: !report

It works fine, and as soon as there is a report, an embed is created in the report channel, except that I can't add a reaction on it, it adds the reactions on the report command. I know you have to use FetchMessages but I don't understand how it works with the new version (12).

client.on( 'message', message => {
    if(message.content.startsWith("!report")) {
        let messageArray = message.content.split(" ");
        let args = messageArray.slice(1);
        let member = message.mentions.users.first();
        if(!member) return message.channel.send("Cannot find this user.");
        let reason = args.slice(1).join(" ");
        if(!reason) return message.channel.send("The reason for postponement is mandatory!");
        const embed = new MessageEmbed()
            .setAuthor("Mandela Bot", "https://i.ibb.co/wNZW68r/Logo.png")
            .setTitle("Report")
            .setFooter("Reports - Mandela Bot")
            .setColor("0x9500FF")
            .addField("User", (member), true)
            .addField("reason", (reason), true)
            .addField("Reporter", ("<@" + message.member + ">"), true)
            .addField("Delete report", "✅", true)
            .addField("Ban the user", "❌", true)
        client.channels.cache.get("689899680724811886").send(embed)
        const channel = client.channels.cache.get("689899680724811886")
        message.react('✅').then(() => message.react('❌'));
    }
})

I would like to be able to add my 2 reactions on the embed that is sent just before, can you please help me!

(I'm French, I use a translator, excuse me if there are mistakes)

You can use promise of sended message for this. Its better then try to fetch message, because all discord actions return a promise . So when you send message, update role, update member you can use .then(data => and data will be message, updated role or updated member.

client.on('message', message => {
    if (message.content.startsWith("!report")) {
        let messageArray = message.content.split(" ");
        let args = messageArray.slice(1);
        let member = message.mentions.users.first();
        if (!member) return message.channel.send("Cannot find this user.");
        let reason = args.slice(1)
            .join(" ");
        if (!reason) return message.channel.send("The reason for postponement is mandatory!");
        const embed = new MessageEmbed()
            .setAuthor("Mandela Bot", "https://i.ibb.co/wNZW68r/Logo.png")
            .setTitle("Report")
            .setFooter("Reports - Mandela Bot")
            .setColor("0x9500FF")
            .addField("User", (member), true)
            .addField("reason", (reason), true)
            .addField("Reporter", ("<@" + message.member + ">"), true)
            .addField("Delete report", "✅", true)
            .addField("Ban the user", "❌", true)
        client.channels.cache.get("689899680724811886")
            .send(embed)
            .then(msg => {
                msg.react('✅')
                    .then(() => msg.react('❌'));
                const filter = (reaction, user) => ['✅', '❌'].includes(reaction.emoji.name)
                msg.awaitReactions(filter, {
                        time: 300000,
                        max: 1
                    })
                    .then(collected => {
                        const reaction = collected.first()
                        if (reaction.emoji.name === '✅') {
                            // Do somthink
                        } else if (reaction.emoji.name === '❌') {
                            // Do somthink
                        }
                    })
                    .catch(collected => {
                        // Do somthink
                    })
            })
    }
})

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