首页 > 解决方案 > discord.js Bot 重启前没有看到消息

问题描述

对不起,我的英语不好。

我想让一个角色对机器人做出反应我的问题是我的机器人在重启之前看不到消息发送

我接受了这段代码,但他没有看到对旧消息的反应。

我该怎么做才能让这段代码看到对旧消息的反应。

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

client.once('ready', () => {
    console.log('Ready!');
});

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.message.partial) {
        try {
            await reaction.message.fetch();
        }
        catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
        }
    }
    console.log(`${user.username} reacted with "${reaction.emoji.name}".`);
});

client.on('messageReactionRemove', async (reaction, user) => {
    if (reaction.message.partial) {
        try {
            await reaction.message.fetch();
        }
        catch (error) {
            console.error('Something went wrong when fetching the message: ', error);
        }
    }
    console.log(`${user.username} removed their "${reaction.emoji.name}" reaction.`);
});

client.login('my token');

标签: javascriptnode.jsdiscord.js

解决方案


除了检查反应消息是否是部分的,还要检查反应本身。

if (reaction.partial) {
 try {
  await reaction.fetch();
 } catch (error) {
  console.error('Something went wrong when fetching the reaction: ', error);
 }
}

推荐阅读