重新启动后如何使机器人注册反应

时间:2020-07-28 22:06:26

标签: javascript node.js discord bots discord.js

我如何做到这一点,以便即使我重新启动bot后,如果我对在重新启动它之前发送的消息做出反应,该bot仍会注册?该命令的作用是让某人将消息发送到某个频道,然后可以访问该频道的mod会以竖起大拇指或不赞成的方式做出回应。但是,有时当我对bot进行更改时,我会重新启动它以保存更改,但是我没有意识到有些帖子尚未经过审查,因此我最终不得不要求他们发送由于反应不再起作用,这些帖子再次出现。

这是我到目前为止所拥有的:

const Discord = require("discord.js");
const talkedRecently = new Set();

module.exports.run = async (bot, message, args) => {
    let botmessage = args.join(" ");
    let pollchannel = bot.channels.cache.get("729855563974049803");
    let avatar = message.author.avatarURL({
        size: 2048
    });

    if (talkedRecently.has(message.author.id)) {
        message.reply(
            "please wait till your six hours are up before you type this again."
        );
    } else {
        if (!botmessage)
            return message.channel.send(
                "Please run the command like this: `?ad (advert message)`."
            );

        let helpembed = new Discord.MessageEmbed()
            .setAuthor(message.author.tag, avatar)
            .setColor("#8c52ff")
            .setDescription(botmessage)
            .setTimestamp();

        const emojis = ["715383579059945512", "715383579059683349"];

        message.delete();
        message.channel.send(`Sent advert message for review.`);
        pollchannel.send(helpembed).then(async msg => {

            await msg.react(emojis[0]);
            await msg.react(emojis[1]);

            const filter = (reaction, user) => emojis.includes(reaction.emoji.id) && user.id != bot.user.id;
            const options = {
                errors: ["time"],
                time: 86400000,
                max: 1
            };
            msg.awaitReactions(filter, options)
                .then(collected => {
                    const first = collected.first();
                    if (emojis.indexOf(first.emoji.id) === 0) {
                        msg.delete();
                        let certainChannel = bot.channels.cache.get("715734085854691329");

                        certainChannel.send(helpembed);
                        message.reply("your advert message has been approved.");
                    } else {
                        msg.delete();
                        message.reply("your advert message has been declined.");
                    }
                })
                .catch(err => {
                    console.log(err)
                });
        });

        talkedRecently.add(message.author.id);
        setTimeout(() => {
            talkedRecently.delete(message.author.id);
        }, 21600000);
    }
};

module.exports.help = {
    name: "ad"
};

1 个答案:

答案 0 :(得分:1)

来自discordjs.guide

在代码顶部(声明客户端的位置)添加以下内容

const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });

链接说明了它的工作原理和一些注意事项 上面网站的示例:

const Discord = require('discord.js');
const client = new Discord.Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
client.on('messageReactionAdd', async (reaction, user) => {
    // When we receive a reaction we check if the reaction is partial or not
    if (reaction.partial) {
        // If the message this reaction belongs to was removed the fetching might result in an API error, which we need to handle
        try {
            await reaction.fetch();
        } catch (error) {
            console.log('Something went wrong when fetching the message: ', error);
            // Return as `reaction.message.author` may be undefined/null
            return;
        }
    }
    // Now the message has been cached and is fully available
    console.log(`${reaction.message.author}'s message "${reaction.message.content}" gained a reaction!`);
    // The reaction is now also fully available and the properties will be reflected accurately:
    console.log(`${reaction.count} user(s) have given the same reaction to this message!`);
});