我目前在discord.js中有一个使用reddit的api的命令。我只是想知道,我将如何获得帖子所发表的评论和评论数量?我到处都看了,找不到答案。非常感谢您的支持。谢谢您的时间!
const { MessageEmbed, DiscordAPIError } = require('discord.js')
const randomPuppy = require('random-puppy')
const Discord = require('discord.js')
module.exports = {
name: "meme",
description: "Returns a random meme from reddit.",
aliases: [],
run: async (bot, message, args, prefix) => {
const subReddits = ["memes"]
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const img = await randomPuppy(random);
const embed = new Discord.MessageEmbed()
.setColor('PURPLE')
.setImage(img)
.setTitle('Meme from r/memes')
.setURL('https://www.reddit.com/r/memes');
message.channel.send(embed);
}
}
答案 0 :(得分:0)
您将需要创建自己的抓取方式,而不是使用randomPuppy,因为它只是抓取而不会返回网址
const fetch = require("node-fetch");
在run函数中:(使用imgur,因为那是randomPuppy从中提取的,而imgur从reddit中提取)
run() {
const subReddits = ["memes"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const url = `https://www.imgur.com/r/${random}/hot.json`;
const res = await fetch(url);
const json = await res.json();
const posts = json.data
.filter(f => msg.channel.nsfw || !f.nsfw);
if (!posts.length) return message.channel.send("No posts, maybe try in a nsfw channel");
const post = posts[Math.floor(Math.random() * posts.length)];
const redditUrl = `https://www.reddit.com${post.reddit}`;
const embed = new MessageEmbed()
.setColor('PURPLE')
.setImage(`https://imgur.com/${imageData.hash}${imageData.ext.replace(/\?.*/, '')}`);
.setTitle("Post from r/" + random)
.setURL(redditUrl)
//this might be imgur upvotes or reddit upvotes idk
.addField("Votes", post.score);
message.channel.send(embed);
}
从https://github.com/dylang/random-puppy/blob/master/index.js
获得了一些代码无论如何,这可能会出现一些错误(例如用户输入的子reddit不存在),因此由您自己进行错误检查