This repository was archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (56 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { Intents, Client, MessageAttachment, MessageEmbed } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const axios = require('axios');
const { createCanvas, loadImage } = require('canvas')
const url = (subreddit) => `https://reddit.com/r/${subreddit}.json?t=week`;
const fetchReddit = async (subreddit) => {
const { data } = (await axios.get(url(subreddit))).data;
const children = data.children;
if(!data || !children) throw new Error();
const main = (children[Math.floor(Math.random() * children.length)]).data;
const image = await loadImage(main.is_video ? main.thumbnail : main.url);
const canvas = createCanvas(image.width, image.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0);
return await canvas.createPNGStream();
}
client.once("ready", () => console.log(client.user.tag));
client.on("interactionCreate", async interaction => {
if(interaction.commandName != "reddit")return;
await interaction.deferReply({ ephemeral: true });
try {
const name = interaction.options.getString("subreddit");
if(!name)return;
const reddit = await fetchReddit(name);
const attachment = new MessageAttachment(reddit, 'img.png');
await interaction.editReply({ embeds: [new MessageEmbed().setImage("attachment://img.png")] , files: [attachment]})
}catch(e) {
console.log(e)
await interaction.editReply({ embeds: [new MessageEmbed().setDescription("Subreddit does not exist")] })
}
})
client.login(TOKEN)
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders')
function deploy(client) {
const rest = new REST({ version: '9' }).setToken(client.token);
const commands = [
new SlashCommandBuilder()
.setName('reddit')
.setDescription('Reddit Post')
.addStringOption(option => option.setName('subreddit').setDescription(`Subreddit name`).setRequired(true))
];
;(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationCommands(client.user.id),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
}