Haha-Yes/commands/fun/reddit.js

48 lines
1.8 KiB
JavaScript
Raw Normal View History

import { SlashCommandBuilder } from 'discord.js';
import { EmbedBuilder } from 'discord.js';
2022-06-17 02:14:14 +02:00
import fetch from 'node-fetch';
2022-06-17 01:25:35 +02:00
export default {
data: new SlashCommandBuilder()
.setName('reddit')
.setDescription('Send random images from the subreddit you choose')
.addStringOption(option =>
option.setName('subreddit')
.setDescription('The subreddit you wish to see')
.setRequired(true)),
category: 'fun',
async execute(interaction, args) {
2022-06-17 01:25:35 +02:00
await interaction.deferReply({ ephemeral: false });
2022-09-01 01:43:59 +02:00
const subreddit = args.subreddit;
fetch('https://www.reddit.com/r/' + subreddit + '.json?limit=100').then((response) => {
2022-06-17 01:25:35 +02:00
return response.json();
}).then((response) => {
if (response.error == 404) {
return interaction.editReply('Not a valid subreddit');
}
if (response.data.dist == 0) {
return interaction.editReply('Not a valid subreddit');
}
const i = Math.floor((Math.random() * response.data.children.length));
if (response.data.children[i].data.over_18 == true && !interaction.channel.nsfw) {
2022-10-31 13:13:09 +01:00
return interaction.editReply('Uh oh! This is a NSFW subreddit! Try again in a NSFW channel!');
2022-06-17 01:25:35 +02:00
}
2022-09-21 20:13:20 +02:00
let description = response.data.children[i].data.selftext;
if (description === '') {
description = 'No description.';
}
const redditEmbed = new EmbedBuilder()
2022-09-08 16:56:15 +02:00
.setColor(interaction.member ? interaction.member.displayHexColor : 'Navy')
2022-06-17 01:25:35 +02:00
.setTitle(response.data.children[i].data.title)
2022-09-21 20:13:20 +02:00
.setDescription(description)
2022-06-17 01:25:35 +02:00
.setURL('https://reddit.com' + response.data.children[i].data.permalink)
2022-06-17 12:30:20 +02:00
.setFooter({ text: `/r/${response.data.children[i].data.subreddit} | ⬆ ${response.data.children[i].data.ups} 🗨 ${response.data.children[i].data.num_comments}` });
2022-06-17 01:25:35 +02:00
2022-07-03 01:00:17 +02:00
interaction.followUp({ embeds: [redditEmbed] });
2022-06-17 01:25:35 +02:00
interaction.followUp(response.data.children[i].data.url);
});
},
};