0
votes

This might be a little bit complicated...

I have made on my server a report system, where if a user reacts with an "❗", this message gets reported in a channel only the owner sees. This is my code so far:

client.on("messageReactionAdd", (messageReaction, user) => {
    const msg = messageReaction.message;
    if (messageReaction.emoji.name == "❗") {
        if (messageReaction.count > 1) {
            // code missing here
        } else {
            const embed = new Discord.MessageEmbed()
            .setColor("#ff9e00")
            .setDescription("§ Report")
            .setFooter(`${msg.member.user.tag}`, msg.member.user.displayAvatarURL())
            .setTimestamp()
            .addFields(
                {name: "Message", value: `[${msg.cleanContent}](${msg.url})`, inline: false},
                {name: "Amount", value: messageReaction.count, inline: true},
                {name: "Channel", value: `${msg.channel.name}`, inline: true});

            msg.member.guild.channels.cache.get(config.channels.report).send((embed));
        }
    };
});

so, everytime someone reports a message, and he was the first one reporting it, my bot will send a new bot, but I want now if a reaction is not the first, I want that the bot edits the according embed, and increases/updates the messageReaction.count. Anyone an idea how I find the initial message without having a database?

Thanks in advance!

1
Please don't put two questions in one. - Nico Nekoru

1 Answers

1
votes

I have modified the code to keep a collection of message IDs from reports that have been sent.

When a report needs to be edited, its ID is fetched from the collection and used to get the actual message, then a new embed is then created from the old embed with the second field incremented. Finally, the message is edited with the new embed containing the incremented field.

client.on("messageReactionAdd", async (messageReaction, user) => {
    const msg = messageReaction.message;
    if (messageReaction.emoji.name == "❗") {
        if (messageReaction.count > 1) {
            const message = (await msg.guild.channels.cache.get(config.channels.report).messages.fetch()).find(message => message.embeds[0].fields[3].value === msg.id);

            const embed = new Discord.MessageEmbed(message.embeds[0])
            .spliceFields(1, 1, {name: "Amount", value: messageReaction.count, inline: true});

            message.edit(embed);
        } else {
            const embed = new Discord.MessageEmbed()
            .setColor("#ff9e00")
            .setDescription("§ Report")
            .setFooter(`${msg.member.user.tag}`, msg.member.user.displayAvatarURL())
            .setTimestamp()
            .addFields(
                {name: "Message", value: `[${msg.cleanContent}](${msg.url})`, inline: false},
                {name: "Amount", value: messageReaction.count, inline: true},
                {name: "Channel", value: `${msg.channel.name}`, inline: true},
                {name: "Message ID", value: msg.id, inline: false});

            msg.guild.channels.cache.get(config.channels.report).send(embed);
        }
    };
});

Here is some extra code if you want to edit reports when the original message gets deleted:

client.on("messageDelete", async (msg) => {
    const message = (await msg.guild.channels.cache.get(config.channels.report).messages.fetch()).find(message => message.embeds[0].fields[3].value === msg.id);

    if (message) {
        const embed = new Discord.MessageEmbed(message.embeds[0])
        .spliceFields(0, 1, {name: "Message", value: `[${msg.cleanContent}](*deleted*)`, inline: false})
        .spliceFields(3, 1, {name: "Message ID", value: '*deleted*', inline: false})
        .setDescription("§ Report\n**The user deleted their message, but here is its content.**");

        message.edit(embed);
    }
});