diff --git a/commands/random-res/shower-thought.js b/commands/random-res/shower-thought.js new file mode 100644 index 00000000..b2e4c1ff --- /dev/null +++ b/commands/random-res/shower-thought.js @@ -0,0 +1,27 @@ +const { Command } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); + +module.exports = class ShowerThoughtCommand extends Command { + constructor(client) { + super(client, { + name: 'shower-thought', + aliases: ['shower-thoughts'], + group: 'random-res', + memberName: 'shower-thought', + description: 'Responds with a random shower thought, directly from r/Showerthoughts.' + }); + } + + async run(msg) { + try { + const { body } = await snekfetch + .get('https://www.reddit.com/r/Showerthoughts.json') + .query({ limit: 1000 }); + const allowed = msg.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18); + if (!allowed.length) return msg.say('Hmm... It seems the thoughts are all gone right now. Try again later!'); + return msg.say(allowed[Math.floor(Math.random() * allowed.length)].data.title); + } catch (err) { + return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + } + } +}; diff --git a/commands/search/reddit.js b/commands/search/reddit.js index 988afe9a..42af636b 100644 --- a/commands/search/reddit.js +++ b/commands/search/reddit.js @@ -28,14 +28,14 @@ module.exports = class RedditCommand extends Command { .get(`https://www.reddit.com/r/${subreddit}/new.json`) .query({ sort: 'new' }); if (!body.data.children.length) return msg.say('Could not find any results.'); - const post = body.data.children[Math.floor(Math.random() * body.data.children.length)].data; - if (!msg.channel.nsfw && post.over_18) return msg.say('This post is only viewable in NSFW channels.'); + const allowed = msg.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18); + if (!allowed.length) return msg.say('Could not find any results.'); + const post = allowed[Math.floor(Math.random() * allowed.length)].data; const embed = new MessageEmbed() .setColor(0xFF4500) .setAuthor('Reddit', 'https://i.imgur.com/DSBOK0P.png') .setURL(`https://www.reddit.com${post.permalink}`) .setTitle(post.title) - .setDescription(`[View URL Here](${post.url})`) .addField('❯ Upvotes', post.ups, true) .addField('❯ Downvotes', diff --git a/package.json b/package.json index 03e63b77..de15dbc0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "48.0.0", + "version": "48.1.0", "description": "Your personal server companion.", "main": "Shard.js", "scripts": {