首页 文章

重启bot后获取旧消息(Discord bot,NodeJS)

提问于
浏览
-2

我正在尝试制作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 .

1 回答

  • 1

    使用 .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 / max数量的消息 - 请在此处查看这些设置:ChannelLogsQueryOptions

相关问题