【问题标题】:Cache message discord.js缓存消息 discord.js
【发布时间】:2020-08-24 22:20:33
【问题描述】:

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

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    你要做的是首先获取频道所在的公会,然后从公会获取频道(因此它返回一个 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
    
    });
      
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这不起作用,因为消息没有被缓存。知道如何缓存消息吗?
      • 还是不行。这是我的code
      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2015-08-03
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 2017-01-21
        • 2021-04-18
        • 2020-12-30
        • 2021-12-24
        相关资源
        最近更新 更多