久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

DiscordJS 獲取超過 100 條消息

DiscordJS fetching more then 100 messages(DiscordJS 獲取超過 100 條消息)
本文介紹了DiscordJS 獲取超過 100 條消息的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試在 DiscordJS 中獲取超過 100 條消息.我在 here 找到了這段代碼,但它不起作用:

I'm trying to fetch more than 100 messages in DiscordJS. I've found this code here but it doesn't work:

async function lots_of_messages_getter(channel, limit = 500) {
    const sum_messages = [];
    let last_id;

    while (true) {
        const options = { limit: 100 };
        if (last_id) {
            options.before = last_id;
        }

        const messages = await channel.fetch(options);
        sum_messages.push(...messages.array());
        last_id = messages.last().id;

        if (messages.size != 100 || sum_messages >= limit) {
            break;
        }
    }

    return sum_messages;
}

client.on("message", async message => {
    const channel = client.channels.cache.get("12345");
    if (message.content.startsWith(prefix+"random")){
        console.log(lots_of_messages_getter());
    }
});

它給了我這個錯誤:

(node:6312) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetch' of undefined

如何解決這個問題?我對 Node.js 有點陌生.

How can this be fixed? I'm kinda new to Node.js.

推薦答案

以下適用于 discord.js v12 和 v13.與您從中獲取代碼的 舊答案 不同,它返回一個 collection,所以可以使用.first()等方法.last().find().get().filter()

The following works in discord.js v12 and v13. Unlike the older answers where you got your code from, it returns a collection, so you can use methods like .first(), .last(), .find(), .get(), .filter(), etc.

const { Collection } = require('discord.js');

async function fetchMore(channel, limit = 250) {
  if (!channel) {
    throw new Error(`Expected channel, got ${typeof channel}.`);
  }
  if (limit <= 100) {
    return channel.messages.fetch({ limit });
  }

  let collection = new Collection();
  let lastId = null;
  let options = {};
  let remaining = limit;

  while (remaining > 0) {
    options.limit = remaining > 100 ? 100 : remaining;
    remaining = remaining > 100 ? remaining - 100 : 0;

    if (lastId) {
      options.before = lastId;
    }

    let messages = await channel.messages.fetch(options);

    if (!messages.last()) {
      break;
    }

    collection = collection.concat(messages);
    lastId = messages.last().id;
  }

  return collection;
}

示例用法:

client.on('message', async (message) => {
  if (message.author.bot) return;

  try {
    const list = await fetchMore(message.channel, 120);

    console.log(
      list.size,
      list.filter((msg) => msg.content.includes('something')),
    );
  } catch (err) {
    console.log(err);
  }
});

這篇關于DiscordJS 獲取超過 100 條消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 日本在线观看视频 | 日韩av成人在线 | 国产成人亚洲精品 | 黄a免费看 | 99久久久久久久久 | 欧美在线亚洲 | 黑人久久| 天堂资源 | 亚洲三区在线观看 | 美女网站视频免费黄 | 麻豆国产精品777777在线 | 亚洲国产高清高潮精品美女 | 浴室洗澡偷拍一区二区 | 一区二区三区在线免费观看视频 | 精品国产乱码久久久久久久久 | 天堂在线1| 狠狠操狠狠干 | 视频一区二区中文字幕 | 中文字幕欧美一区二区 | 999精品网| 亚洲第一网站 | 凹凸日日摸日日碰夜夜 | 雨宫琴音一区二区在线 | 欧美激情久久久久久 | 91在线一区 | 91伊人 | 黄视频网址 | 日韩国产免费 | 亚洲草草视频 | 自拍偷拍中文字幕 | 亚洲人成网亚洲欧洲无码 | 久久久久久国产精品免费 | 欧美一区不卡 | 欧美色综合一区二区三区 | 午夜精品一区二区三区免费视频 | 久久一区二区三区四区五区 | 91精品国产综合久久久久久蜜臀 | 在线观看成年视频 | 91 在线| 亚洲一区二区视频 | 国产日韩欧美综合 |