缓存消息discord.js

时间:2020-05-09 09:49:38

标签: node.js discord discord.js

我想扮演一些角色。但是为此,我必须缓存在机器人启动之前发送的消息。我用channel.messages.fetch尝试过,但是到目前为止还没有奏效。

我当前的代码:

client.on('messageReactionAdd', async(reaction, user) => {
    client.channels.cache.get("689034237672030230");
    channel.messages.fetch('708428887612194979');
    // 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 message.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.id}" 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!`);
});

3 个答案:

答案 0 :(得分:0)

channel.messages.fetch()不起作用,因为未定义channel。您需要将前两行定义为变量:

const channel = client.channels.cache.get("689034237672030230");
const msg = channel.messages.cache.get('708428887612194979');

答案 1 :(得分:0)

当client#ready触发时,缓存消息:

client.on('ready', () => {
    client.channels.cache.get(CHANNEL_ID).message.cache.get(MSG_ID)
})

答案 2 :(得分:0)

你需要做的是首先获取频道所在的公会,然后从公会中获取频道(所以它返回一个GuildChannel)。然后从那里获取消息。

完整代码为:

client.on('ready', async () => {
  const guild = await client.guilds.fetch('guild-id-here');
  const channel = guild.channels.cache.get('channel-id-here');
  const message = await channel.messages.fetch('message-id-here');
});

client.on('message', message => {
  //rest of your code

});