缓存消息 Discord.js

5

我想创建一些反应角色,但为此必须缓存在机器人启动之前发送的消息。我尝试过使用 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!`);
});
2个回答

3
你需要做的是先获取频道所在的公会,然后从公会中获取频道(这将返回一个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

});
  

-1

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

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

这不起作用,因为消息没有被缓存。有什么办法可以缓存消息吗? - Mr Brickstar
还是不行。这是我的代码 - Mr Brickstar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接