-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (68 loc) · 2.99 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits, Message } = require('discord.js');
const { token, prefix, subRedditList, otherSubRedditList, thirdSubRedditList, uid1 } = require('./config.json');
const { EmbedBuilder } = require('discord.js');
const request = require('request');
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages] });
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
client.on('messageCreate', async(message) => {
if (!message.content.startsWith(prefix)) return
switch (true) {
case message.content.includes("meme"):
const meme = await getPost(subRedditList);
const memeEmbed = constructMeme(meme);
message.channel.send({ embeds: [memeEmbed] });
break;
case message.content.includes("help"):
message.channel.send("literally just type #meme no other commands :wink: ")
break;
case message.content.includes("post"):
const params = {
subredditList: otherSubRedditList,
specific: false // possibly use this for custom message
}
if (message.author.id == uid1) {
params.subredditList = thirdSubRedditList;
params.specific = true;
}
getPost(otherSubRedditList)
.then(post => {
if (!message.channel.nsfw) return;
const memeEmbed = constructMeme(post);
message.channel.send({ embeds: [memeEmbed] });
})
.catch(err => {
// top-tier error handling in Javascript
console.log(err);
message.channel.send('Someting went terribly wrong while trying to fetch posts from reddit D: ')
})
break;
}
});
const getPost = (list) => {
return new Promise((resolve, reject) => {
const url = list[Math.floor(Math.random() * list.length)];
request({
uri: url,
method: 'GET'
}, (err, res) => {
console.log(`Fetching from ${url}...`)
if (err) return reject(err);
if (res.statusCode != 200) return reject(err);
const json = JSON.parse(res.body);
const children = json.data.children;
return resolve(children[Math.floor(Math.random() * children.length)]);
});
});
}
const constructMeme = (post) => new EmbedBuilder()
.setTitle(post.data.title)
.setDescription("subreddit: " + post.data.subreddit_name_prefixed)
.setImage(post.data.url)
// Log in to Discord with the client's token
client.login(token);