【发布时间】:2020-12-24 08:09:09
【问题描述】:
我在这个社区的帮助下收集了一些代码,如果机器人因为不存在而无法从 subreddit 发送随机帖子,我不知道如何获取?这是代码:
if (msg.content.startsWith('-reddit ')) {
const subname = msg.content.slice(8);
const mxlenght = 30;
if (subname.length >= mxlenght) {
msg.reply('Ez a subreddit név meghaladja a 30 karakteres maximumot.');
} else {
function loadMemes() {
// Fetch JSON
return (
fetch(
'https://www.reddit.com/r/' + subname + '.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/' + subname },
});
// Send the embed
return message.channel.send(embed);
})
.catch(console.error);
}
// Usage:
postRandomMeme(msg).catch(console.error);
}
}
【问题讨论】:
标签: javascript node.js discord discord.js