繁体   English   中英

如何让我的机器人获取已经从频道中的用户发送的随机消息,并在触发自动响应时发送该随机消息

[英]How to get my bot to get random messages that are already sent from users in a channel and send that random message when an autoresponse is triggered

正如标题所说,我试图使用 fetchMessages 来检查某个频道发送的消息,但是当我试图搞砸这个时,它只会在控制台中给我提供无穷无尽的信息块。

我尝试阅读它,但其中似乎没有实际消息,只是给了我有关服务器的信息,例如频道 ID、会员 ID、他们的权限等。

起初我认为使用这种方法会给我随机消息,所以我可以让机器人直接将消息发送到一个频道,但它给了我一个错误,所以我只是使用 console.log 来获取更多信息。 但事实并非如此,或者我只是用错了......

这是我正在尝试的代码:

    bot.on('message', msg =>{

    if(msg.author.bot) return;
    lowerCase = msg.content.toLowerCase();

    if(lowerCase.includes ("fetch")){ 
        msg.channel.fetchMessages({limit: 1}).then(console.log);
    }
})

同样显然它无法跟踪旧消息,因为当我尝试使用用户 ID 过滤掉消息时,它显示他们发送了 0 条消息,即使他们以前一直在发送消息? 我正在尝试实现它仍然可以读取非常旧的消息,并且在我使用自动回复消息时仍然可以发送这些消息。

TextChannel.fetchMessages() 一次最多只能获取 100 条消息

简短的回答:

虽然它没有显示在docs中,但如果您获取超过 100 个,您将收到 API 错误:

 DiscordAPIError: Invalid Form Body limit: int value should be less than or equal to 100.

控制台记录Message object 确实会为您提供有关Guild及其中所有其他内容的大量信息。 您可能只想控制台记录消息的内容和作者 ID/用户标签

因此,如果您想搜索超过过去的 100 条消息,则必须在每个连续的TextChannel.fetchMessages()调用中使用ChannelLogsQueryOptionsbefore参数中最旧消息的 ID 连续获取 100 条消息,然后连接每个Collection一起使用Collection.concat()


长答案:

例如,如果你想获取过去的 200 条消息,你可以这样写:

// using .then()
await channel.fetchMessages({limit: 100})
.then(first100 => {
  let next100 = await channel.fetchMessages({limit: 100, before: first100.lastKey()})
  .then(next100 => {
    let all200 = first100.concat(next100);
  });
});
// in async function
let first100 = await channel.fetchMessages({limit: 100});
let next100 = await channel.fetchMessages({limit: 100, before: first100.last().id});
let all200 = first100.concat(next100);

看看这个答案,以了解将过去消息存储在数组中的异步 function。

我制作了自己的版本,它将为您提供一个Collection并将其作为正确的Promise返回:

const fetchManyMessages = (channel, limit = 200) => {
  return new Promise((resolve, reject) => {
    channel.fetchMessages({limit: limit < 100 ? limit : 100})
    .then(collection => {
      const nextBatch = () => {
        console.log(collection.size);
        let remaining = limit - collection.size;

        channel.fetchMessages({limit: remaining<100 ? remaining : 100, before: collection.lastKey()})
        .then(next => {
          let concatenated = collection.concat(next);

          // resolve when limit is met or when no new msgs were added (reached beginning of channel)
          if (collection.size >= limit || collection.size == concatenated.size) return resolve(concatenated);

          collection = concatenated;
          nextBatch();
        })
        .catch(error => reject(error));
      }

      nextBatch();
    })
    .catch(error => reject(error));
  });
}

示例用法:

fetchManyMessages(message.channel, 1500)
.then(msgs => console.log(msgs.size))
.catch(err => console.log(err));

在这里,我还制作了一个prototype版本,我认为它非常适合一些语法上的精彩:

Discord.TextChannel.prototype.fetchManyMessages = function (limit = 200) {
  return new Promise((resolve, reject) => {
    this.fetchMessages({limit: limit < 100 ? limit : 100})
    .then(collection => {
      const nextBatch = () => {
        console.log(collection.size);
        let remaining = limit - collection.size;

        this.fetchMessages({limit: remaining<100 ? remaining : 100, before: collection.lastKey()})
        .then(next => {
          let concatenated = collection.concat(next);

          // resolve when limit is met or when no new msgs were added (reached beginning of channel)
          if (collection.size >= limit || collection.size == concatenated.size) return resolve(concatenated);

          collection = concatenated;
          nextBatch();
        })
        .catch(error => reject(error));
      }

      nextBatch();
    })
    .catch(error => reject(error));
  });
}

示例用法:

message.channel.fetchManyMessages(1500)
.then(msgs => console.log(msgs.size))
.catch(err => console.log(err));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM