This repository has been archived by the owner on Feb 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
64 lines (55 loc) · 1.86 KB
/
bot.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
// Discord join link
// https://discordapp.com/oauth2/authorize?&client_id=610577812260257914&scope=bot&permissions=51200
const Discord = require('discord.js');
const fetch = require('node-fetch');
require('dotenv').config();
(async () => {
try {
const fetched = await fetch('https://mtgjson.com/json/CompiledList.json');
const { files } = await fetched.json();
const bot = new Discord.Client();
const send = (channel, message, formatted = true) => {
let newMessage = formatted
? `Here's your file: https://mtgjson.com/json/${message}.\n\nCheck out the website for the documentation!`
: message;
channel.reply(newMessage);
};
bot.on('ready', () => {
console.log('MTGJSON Fetcher is connected!');
});
bot.on('message', message => {
let [trigger, cmd] = message.content.split(' ');
try {
switch (trigger) {
case '!f':
case '!get':
case '!fetch':
const desire = cmd.split('.')[0];
if (files.includes(desire)) {
if (!cmd.includes('.')) {
cmd = cmd + '.json';
}
send(message, cmd);
} else {
possibleFiles = files.filter(file => file.indexOf(desire) > -1);
send(
message,
`Sorry, I can't find that file. ${
possibleFiles.length > 0
? 'Did you mean any of these? **' + possibleFiles.join(', ') + '**'
: ''
}`,
false
);
}
break;
}
} catch (err) {
console.error('An error occured when trying to parse the Discord message.');
}
});
bot.login(process.env.BOT_TOKEN);
} catch (err) {
console.error('An error occured while trying to fetch the file list.');
}
})();