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

Posted

技术标签:

【中文标题】重启后如何使机器人注册反应【英文标题】:How to make bot register reactions after getting restarted 【发布时间】:2020-11-18 10:42:26 【问题描述】:

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

这是我目前所拥有的:

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】:

来自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!`);
);

【讨论】:

抱歉,这不是我想要的,因为它无法跟踪单个消息并重新发布它们。 @CorruptBlu 你是什么意思?当有人对消息做出反应时,您可以再次发送。 机器人发送嵌入。该命令的作用是让某人向某个频道发送消息,然后可以访问该频道的模组会在他们批准时以竖起大拇指或不赞成的方式做出反应。但是,有时当我对机器人进行更改时,我会重新启动它以保存我的更改,但我没有意识到有些帖子还没有经过审核,所以我最终不得不让他们发送由于反应不再有效,因此再次发布帖子。 我明白,但这会解决这个问题!将第一行代码放在您的机器人中将使反应再次对旧消息起作用! ;)

以上是关于重启后如何使机器人注册反应的主要内容,如果未能解决你的问题,请参考以下文章

如何使用特定的 webhook 使机器人对特定频道下的消息做出反应

[使用message.content.startswith时如何使Discord机器人对服务器表情符号做出反应

如何让机器人在收到来自某个消息的反应后将消息发送到特定频道

您如何让机器人使用自定义表情符号添加反应?

重启后下拉失败 - discord.js

如何修复重新启动机器人后无法正常工作的事件