【问题标题】:Get old messages after bot reboot (Discord bot, NodeJS)机器人重启后获取旧消息(Discord 机器人,NodeJS)
【发布时间】:2018-06-18 23:41:22
【问题描述】:

我正在尝试制作 Discord Bot,但遇到了一些问题。 我想要一种方法来获取频道的所有消息,但是在机器人重新启动后,他不会“看到”旧消息。 这是一个例子:

const commando = require('discord.js-commando');

module.exports = class nbMess extends commando.Command{
    constructor(client) {
        super(client, {
            name: 'nbmsg',
            group: 'admin',
            memberName: 'nbmsg',
            description: 'Return number of messages in a channel'
        });
    }
    async run(msg, args){
        msg.channel.send(`Number of messages : ${msg.channel.messages.size}`);
    }
}

所以,我发送 3 条随机消息,然后启动机器人并启动命令。 结果是 1,对于调用命令的消息。

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

我也遇到过discord.js的问题,发现discord.js-light不依赖缓存,它的事件可以在未缓存的对象上触发。

https://www.npmjs.com/package/discord.js-light

存在一些差异,您可能需要获取一些数据,否则这些数据会自动填充到 discord.js 中。

【讨论】:

    【解决方案2】:

    使用.fetchMessages() 方法获取过去发送的消息。

    .fetchMessages 上的文档中的一个示例:

    // Get messages
    channel.fetchMessages()
      .then(messages => console.log(`Received ${messages.size} messages`))
      .catch(console.error);
    

    所以你的例子看起来像这样:

    module.exports = class nbMess extends commando.Command{
        ...
        async run(msg, args){
            msg.channel.fetchMessages()
            .then(messages => {
                msg.channel.send(`Number of messages : ${messages.size}`);
            });
        }
    }
    

    您可以传递可选设置,例如 limit/要返回的最大消息数 - 请在此处查看这些设置:ChannelLogsQueryOptions

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2018-06-05
      • 2020-08-24
      • 2020-07-04
      • 2021-08-19
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2019-04-24
      相关资源
      最近更新 更多