簡體   English   中英

Discord.js 如何獲取 reddit 帖子的描述

[英]Discord.js How to get description of reddit post

我有這段代碼,它從 r/memes 獲得一個隨機帖子,它獲得了帶有圖像的帖子標題(如果有的話),我得到了很多幫助,我只是希望它能夠閱讀帖子的描述。 如果我想在新聞 subreddits 或其他東西上使用它,我希望能夠閱讀帖子中的文本。

if (msg.content == '-meme') {
 function loadMemes() {
  // Fetch JSON
  return (
   fetch('https://www.reddit.com/r/memes.json?limit=800&?sort=hot&t=all')
    .then((res) => res.json())
    // Return the actual posts
    .then((json) => json.data.children)
  );
 }

 function postRandomMeme(message) {
  return loadMemes().then((posts) => {
   // Get a random post's title and URL
   const { title, url } = posts[Math.floor(Math.random() * posts.length)].data;
   // Create the embed
   const embed = new Discord.RichEmbed({
    title,
    image: { url },
    footer: { text: 'Subreddit : r/memes' },
   });
   // Send the embed
   return message.channel.send(embed);
  });
 }

 // Usage:
 postRandomMeme(msg);
 // Log all errors
 //.catch(console.error);
}

有一個名為reddit-bot的 github 存儲庫是開源和在線的。 你可以從那里提供的代碼中得到一個想法。

https://github.com/lowJ/reddit-bot/blob/master/index.js

我還發現了這個 npm package 專門用於獲取模因,並且還有一個開源 github 存儲庫。

https://www.npmjs.com/package/@blad3mak3r/reddit-memes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM