From 16a5e4ea6d77f471570f2a0e1a0cda99a5dc3060 Mon Sep 17 00:00:00 2001 From: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Thu, 19 Mar 2020 16:45:05 +0200 Subject: [PATCH] new event handling --- commands/play.js | 8 ++++ commands/savedb.js | 13 ++++++ events/{ => clientEvents}/guildCreate.js | 0 events/{ => clientEvents}/msg.js | 0 events/{ => clientEvents}/ready.js | 17 +------- events/{ => clientEvents}/voiceStateUpdate.js | 0 .../{dispatcher => dispatcherEvents}/error.js | 0 .../finish.js | 0 events/events.js | 19 ++++++++ struct/client.js | 43 ++++++------------- struct/config/config.js | 6 ++- struct/config/messages.js | 2 + struct/funcs/saveDB.js | 16 +++++++ 13 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 commands/savedb.js rename events/{ => clientEvents}/guildCreate.js (100%) rename events/{ => clientEvents}/msg.js (100%) rename events/{ => clientEvents}/ready.js (69%) rename events/{ => clientEvents}/voiceStateUpdate.js (100%) rename events/{dispatcher => dispatcherEvents}/error.js (100%) rename events/{dispatcher => dispatcherEvents}/finish.js (100%) create mode 100644 events/events.js create mode 100644 struct/funcs/saveDB.js diff --git a/commands/play.js b/commands/play.js index aa01a23a..a74019dc 100644 --- a/commands/play.js +++ b/commands/play.js @@ -42,6 +42,14 @@ module.exports = { var video = await youtube.getVideoByID(videos[0].id); } catch (err) { console.error(err); + if (err.code === 403) { + if (client.config.api_key === client.config.api_key2) { + client.config.api_key = client.config.api_key3; + } else { + client.config.api_key = client.config.api_key2; + } + return msg.channel.send(client.messages.quotaReached); + } return msg.channel.send(client.messages.noResults); } } diff --git a/commands/savedb.js b/commands/savedb.js new file mode 100644 index 00000000..c0e70a6d --- /dev/null +++ b/commands/savedb.js @@ -0,0 +1,13 @@ +module.exports = { + name: 'savedb', + alias: 'save', + usage: '', + description: 'save the database', + onlyDev: true, + permission: 'dev', + category: 'util', + async execute(msg, args, client, Discord, prefix, command) { + client.funcs.saveDB(client); + msg.channel.send(client.messages.dbSaved); + } +}; diff --git a/events/guildCreate.js b/events/clientEvents/guildCreate.js similarity index 100% rename from events/guildCreate.js rename to events/clientEvents/guildCreate.js diff --git a/events/msg.js b/events/clientEvents/msg.js similarity index 100% rename from events/msg.js rename to events/clientEvents/msg.js diff --git a/events/ready.js b/events/clientEvents/ready.js similarity index 69% rename from events/ready.js rename to events/clientEvents/ready.js index acede04f..6d1698da 100644 --- a/events/ready.js +++ b/events/clientEvents/ready.js @@ -38,21 +38,8 @@ module.exports = { } console.log(`- Activated - Shard: ${client.shard.ids} -`); setInterval(async () => { - if (client.config.saveDB && !client.config.devMode) { - console.log('DB saved'); - client.guilds.cache.forEach(guild => { - client.db.collection('guilds').doc(guild.id).set({ - prefix: client.global.db.guilds[guild.id].prefix, - defaultVolume: client.global.db.guilds[guild.id].defaultVolume, - permissions: client.global.db.guilds[guild.id].permissions, - dj: client.global.db.guilds[guild.id].dj, - djrole: client.global.db.guilds[guild.id].djrole, - startPlaying: client.global.db.guilds[guild.id].startPlaying, - //bass: client.global.db.guilds[guild.id].bass, - }); - }); - } - if (client.config.dblApi && !client.config.devMode) dbl.postStats(client.guilds.size); + client.funcs.saveDB(client); + if (client.config.dblApi && !client.config.devMode) dbl.postStats(client.guilds.cache.size); }, 1800000); setInterval(() => { client.funcs.ffmpeg(client, Discord); diff --git a/events/voiceStateUpdate.js b/events/clientEvents/voiceStateUpdate.js similarity index 100% rename from events/voiceStateUpdate.js rename to events/clientEvents/voiceStateUpdate.js diff --git a/events/dispatcher/error.js b/events/dispatcherEvents/error.js similarity index 100% rename from events/dispatcher/error.js rename to events/dispatcherEvents/error.js diff --git a/events/dispatcher/finish.js b/events/dispatcherEvents/finish.js similarity index 100% rename from events/dispatcher/finish.js rename to events/dispatcherEvents/finish.js diff --git a/events/events.js b/events/events.js new file mode 100644 index 00000000..53472a4d --- /dev/null +++ b/events/events.js @@ -0,0 +1,19 @@ +module.exports = function (client) { + const Discord = require('discord.js'); + const events = './clientEvents/'; + client.on('ready', () => { + require(`${events}ready`).execute(client, Discord); + }); + client.on('message', (msg) => { + require(`${events}msg`).execute(client, msg, Discord); + }); + client.on('guildCreate', (guild) => { + require(`${events}guildCreate`).execute(client, guild); + }); + client.on('voiceStateUpdate', (oldState, newState) => { + require(`${events}voiceStateUpdate`).execute(client, oldState, newState); + }); + client.on('error', (error) => { + client.channels.fetch(client.config.debug_channel).send(`Error: ${error} on shard: ${client.shard}`); + }); +} \ No newline at end of file diff --git a/struct/client.js b/struct/client.js index de2a4a96..a2e464dd 100644 --- a/struct/client.js +++ b/struct/client.js @@ -1,10 +1,9 @@ const { Client, Collection } = require('discord.js'); -const Discord = require('discord.js'); const admin = require('firebase-admin'); const serviceAccount = require('./config/serviceAccount.json'); const fs = require('fs'); const path = require('path'); -const events = '../events/'; +const events = require('../events/events.js'); module.exports = class extends Client { constructor() { @@ -12,6 +11,10 @@ module.exports = class extends Client { disableEveryone: true, disabledEvents: ['TYPING_START'] }); + + admin.initializeApp({ + credential: admin.credential.cert(serviceAccount), + }); this.commands = new Collection(); this.commandAliases = new Collection(); this.settingCmd = new Collection(); @@ -20,8 +23,14 @@ module.exports = class extends Client { this.dispatcher = {}; this.config = require('./config/config.js'); this.messages = require('./config/messages.js'); - this.dispatcher.finish = require('../events/dispatcher/finish.js'); - this.dispatcher.error = require('../events/dispatcher/error.js'); + this.db = admin.firestore(); + this.db.FieldValue = require('firebase-admin').firestore.FieldValue; + this.dispatcher.finish = require('../events/dispatcherEvents/finish.js'); + this.global = { + db: { + guilds: {}, + }, + }; fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => { this.funcs[filename.slice(0, -3)] = require(`./funcs/${filename}`); @@ -43,31 +52,7 @@ module.exports = class extends Client { this.config.token = this.config.devToken; } - admin.initializeApp({ - credential: admin.credential.cert(serviceAccount), - }); - - this.db = admin.firestore(); - - this.global = { - db: { - guilds: {}, - }, - }; - - this.db.FieldValue = require('firebase-admin').firestore.FieldValue; - - this.on('ready', () => { - require(`${events}ready`).execute(this, Discord); - }).on('message', (msg) => { - require(`${events}msg`).execute(this, msg, Discord); - }).on('guildCreate', (guild) => { - require(`${events}guildCreate`).execute(this, guild); - }).on('voiceStateUpdate', (oldState, newState) => { - require(`${events}voiceStateUpdate`).execute(this, oldState, newState); - }).on('error', (error) => { - client.channels.fetch(client.config.debug_channel).send(`Error: ${error} on shard: ${this.shard}`); - }); + events(this); this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); } diff --git a/struct/config/config.js b/struct/config/config.js index 68ffe577..db1addf6 100644 --- a/struct/config/config.js +++ b/struct/config/config.js @@ -5,14 +5,16 @@ module.exports = { devToken: process.env.DEVTOKEN, dblKey: process.env.DBLKEY, api_key: process.env.GOOGLE_API_KEY, + api_key2: process.env.GOOGLE_API_KEY2, + api_key3: process.env.GOOGLE_API_KEY3, testServer: "489111553321336832", debug_channel: "634718645188034560", primary_test_channel: "617633098296721409", secondary_test_channel: "570531724002328577", devId: "360363051792203779", embedColor: "#b50002", - invite: "https://discordapp.com/api/oauth2/authorize?client_id=607266889537945605&permissions=3427328&redirect_uri=https%3A%2F%2Fdiscordapp.com%2Foauth2%2Fauthorize%3Fclient_id%3D607266889537945605%26%3Bscope%3Dbot%26%3Bpermissions%3D0&scope=bot", - devMode: false, + invite: "https://discordapp.com/oauth2/authorize?client_id=607266889537945605&permissions=3427328&scope=bot", + devMode: true, dblApi: false, saveDB: true, shards: 10, diff --git a/struct/config/messages.js b/struct/config/messages.js index 01c99b3f..25668006 100644 --- a/struct/config/messages.js +++ b/struct/config/messages.js @@ -20,6 +20,7 @@ module.exports = { currentDefaultVolume: ":speaker: Current default volume is:", currentPrefix: "Current prefix:", currentVolume: ":loud_sound: The current volume is: ", + dbSaved: "<:green_check_mark:674265384777416705> DB Saved!", defaultVolumeMax: "<:redx:674263474704220182> The default volume must be below `100` for quality and safety resons.", defaultVolumeNumber: "<:redx:674263474704220182> I'm sorry, But the default volume needs to be a valid __number__.", defaultVolumeSet: "<:green_check_mark:674265384777416705> Default volume set to:", @@ -84,6 +85,7 @@ module.exports = { queueFooter: "songs in the queue!", queueLength: "<:redx:674263474704220182> There are only %SONGS% amount of songs in the queue!", queueTitle: "__Song queue__", + quotaReached: "<:redx:674263474704220182> Could not search for results please try again!", reloaded: "All files reloaded!", removed: "🗑️ removed `%SONG%` from the queue!", reset: "<:green_check_mark:674265384777416705> Reset __all__ guild settings!", diff --git a/struct/funcs/saveDB.js b/struct/funcs/saveDB.js new file mode 100644 index 00000000..2027fe3f --- /dev/null +++ b/struct/funcs/saveDB.js @@ -0,0 +1,16 @@ +module.exports = async function (client) { + if (client.config.saveDB && !client.config.devMode) { + console.log('DB saved'); + client.guilds.cache.forEach(guild => { + client.db.collection('guilds').doc(guild.id).set({ + prefix: client.global.db.guilds[guild.id].prefix, + defaultVolume: client.global.db.guilds[guild.id].defaultVolume, + permissions: client.global.db.guilds[guild.id].permissions, + dj: client.global.db.guilds[guild.id].dj, + djrole: client.global.db.guilds[guild.id].djrole, + startPlaying: client.global.db.guilds[guild.id].startPlaying, + //bass: client.global.db.guilds[guild.id].bass, + }); + }); + } +}