首页 > 解决方案 > Discord.js 如何获取 reddit 帖子的描述

问题描述

我有这段代码,它从 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);
}

标签: javascriptjsondiscord.jsreddit

解决方案


有一个名为的 github 存储库reddit-bot,它是开源和在线的。你可以从那里提供的代码中得到一个想法。

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

我还发现了这个专门用于获取 memes 的 npm 包,并且还有一个开源 github 存储库。

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


推荐阅读