· 5 years ago · May 07, 2020, 03:46 PM
1const fs = require('fs');
2const path = require('path');
3
4const Discord = require('discord.js');
5
6class MusicBot {
7 /**
8 * Place Discord Token And Google API Key in musicbot.js {Mandatory}
9 */
10 constructor(options) {
11 if (!options || !options.discordToken || !options.googleKey) {
12 throw new Error('Either Token or Google Key is Missing');
13 }
14
15 this.discordToken = options.discordToken;
16 this.googleKey = options.googleKey;
17 this.prefix = options.prefix || 'KK!'; //Prefix setup
18
19 this.queue = new Map();
20
21 this.setup_();
22 }
23
24 start() {
25 this.client.login(this.discordToken);
26 }
27
28 setup_() {
29 this.client = new Discord.Client();
30 this.client.commands = new Discord.Collection(); ///Command Handler
31
32 const commandFiles = fs.readdirSync(path.join(__dirname, '/commands')).filter(file => file.endsWith('.js'));
33
34 for (const file of commandFiles) {
35 const command = require(`./commands/${file}`);
36 this.client.commands.set(command.name, command);
37 }
38
39 this.client.once('ready', () => { ///Status
40 console.log('Bot is ready!');
41 this.client.user.setActivity('For KK!', { type: 'Watching' });
42 });
43
44 this.client.on('message', message => { //Main Argument Call
45 if (message.author.bot || !message.content.startsWith(this.prefix)) {
46 return;
47 }
48
49 const args = message.content.slice(this.prefix.length).split(/ +/); //Slicing Prefix
50 const command = args.shift().toLowerCase();
51
52 const arg = args.join(' ');
53
54 if (!this.client.commands.has(command)) {
55 return;
56 }
57
58 try {
59 this.client.commands.get(command).execute(message, arg, this); //Help available for the commands goes here
60 } catch (error) {
61 console.error(error);
62 }
63 });
64 }
65}
66
67module.exports = MusicBot;