javascript - 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);
}
解决方案
有一个名为的 github 存储库reddit-bot
,它是开源和在线的。你可以从那里提供的代码中得到一个想法。
https://github.com/lowJ/reddit-bot/blob/master/index.js
我还发现了这个专门用于获取 memes 的 npm 包,并且还有一个开源 github 存储库。
https://www.npmjs.com/package/@blad3mak3r/reddit-memes
推荐阅读
- node.js - 将 csv 转换为 JSON,并在文档顶部添加注释 (Node.js)
- excel - 使用命名范围使用 vba 创建 excel 数据验证
- java - 将两个 ArrayList 并行合并为一个 ArrayList?
- android - 将选中的单选按钮的值作为字符串并将其存储在 Firebase
- c# - 使用 Asp.NET Core 3.1 框架将文件上传到服务器时如何使用 IFormFile 作为属性?
- vb.net - 获取所有 Outlook 电子邮件(包括服务器上的电子邮件)
- ruby-on-rails - 如何创建(我认为)需要 PostgreSQL 子查询的 Rails 查询?
- kubernetes - Prometheus 仪表板暴露在入口控制器上
- .net-core - 我可以使用什么命令来删除 .NET Core 中的项目?
- c# - SignalR 请求用户为空