1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-23 06:43:17 +00:00

new event handling

This commit is contained in:
MatteZ02 2020-03-19 16:45:05 +02:00
parent 2ca0306928
commit 16a5e4ea6d
13 changed files with 78 additions and 46 deletions

View File

@ -42,6 +42,14 @@ module.exports = {
var video = await youtube.getVideoByID(videos[0].id); var video = await youtube.getVideoByID(videos[0].id);
} catch (err) { } catch (err) {
console.error(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); return msg.channel.send(client.messages.noResults);
} }
} }

13
commands/savedb.js Normal file
View File

@ -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);
}
};

View File

@ -38,21 +38,8 @@ module.exports = {
} }
console.log(`- Activated - Shard: ${client.shard.ids} -`); console.log(`- Activated - Shard: ${client.shard.ids} -`);
setInterval(async () => { setInterval(async () => {
if (client.config.saveDB && !client.config.devMode) { client.funcs.saveDB(client);
console.log('DB saved'); if (client.config.dblApi && !client.config.devMode) dbl.postStats(client.guilds.cache.size);
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);
}, 1800000); }, 1800000);
setInterval(() => { setInterval(() => {
client.funcs.ffmpeg(client, Discord); client.funcs.ffmpeg(client, Discord);

19
events/events.js Normal file
View File

@ -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}`);
});
}

View File

@ -1,10 +1,9 @@
const { Client, Collection } = require('discord.js'); const { Client, Collection } = require('discord.js');
const Discord = require('discord.js');
const admin = require('firebase-admin'); const admin = require('firebase-admin');
const serviceAccount = require('./config/serviceAccount.json'); const serviceAccount = require('./config/serviceAccount.json');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const events = '../events/'; const events = require('../events/events.js');
module.exports = class extends Client { module.exports = class extends Client {
constructor() { constructor() {
@ -12,6 +11,10 @@ module.exports = class extends Client {
disableEveryone: true, disableEveryone: true,
disabledEvents: ['TYPING_START'] disabledEvents: ['TYPING_START']
}); });
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
this.commands = new Collection(); this.commands = new Collection();
this.commandAliases = new Collection(); this.commandAliases = new Collection();
this.settingCmd = new Collection(); this.settingCmd = new Collection();
@ -20,8 +23,14 @@ module.exports = class extends Client {
this.dispatcher = {}; this.dispatcher = {};
this.config = require('./config/config.js'); this.config = require('./config/config.js');
this.messages = require('./config/messages.js'); this.messages = require('./config/messages.js');
this.dispatcher.finish = require('../events/dispatcher/finish.js'); this.db = admin.firestore();
this.dispatcher.error = require('../events/dispatcher/error.js'); 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 => { fs.readdirSync(path.join(__dirname, 'funcs')).forEach(filename => {
this.funcs[filename.slice(0, -3)] = require(`./funcs/${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; this.config.token = this.config.devToken;
} }
admin.initializeApp({ events(this);
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}`);
});
this.login(this.config.token).catch(err => console.log('Failed to login: ' + err)); this.login(this.config.token).catch(err => console.log('Failed to login: ' + err));
} }

View File

@ -5,14 +5,16 @@ module.exports = {
devToken: process.env.DEVTOKEN, devToken: process.env.DEVTOKEN,
dblKey: process.env.DBLKEY, dblKey: process.env.DBLKEY,
api_key: process.env.GOOGLE_API_KEY, api_key: process.env.GOOGLE_API_KEY,
api_key2: process.env.GOOGLE_API_KEY2,
api_key3: process.env.GOOGLE_API_KEY3,
testServer: "489111553321336832", testServer: "489111553321336832",
debug_channel: "634718645188034560", debug_channel: "634718645188034560",
primary_test_channel: "617633098296721409", primary_test_channel: "617633098296721409",
secondary_test_channel: "570531724002328577", secondary_test_channel: "570531724002328577",
devId: "360363051792203779", devId: "360363051792203779",
embedColor: "#b50002", 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", invite: "https://discordapp.com/oauth2/authorize?client_id=607266889537945605&permissions=3427328&scope=bot",
devMode: false, devMode: true,
dblApi: false, dblApi: false,
saveDB: true, saveDB: true,
shards: 10, shards: 10,

View File

@ -20,6 +20,7 @@ module.exports = {
currentDefaultVolume: ":speaker: Current default volume is:", currentDefaultVolume: ":speaker: Current default volume is:",
currentPrefix: "Current prefix:", currentPrefix: "Current prefix:",
currentVolume: ":loud_sound: The current volume is: ", 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.", 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__.", 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:", defaultVolumeSet: "<:green_check_mark:674265384777416705> Default volume set to:",
@ -84,6 +85,7 @@ module.exports = {
queueFooter: "songs in the queue!", queueFooter: "songs in the queue!",
queueLength: "<:redx:674263474704220182> There are only %SONGS% amount of songs in the queue!", queueLength: "<:redx:674263474704220182> There are only %SONGS% amount of songs in the queue!",
queueTitle: "__Song queue__", queueTitle: "__Song queue__",
quotaReached: "<:redx:674263474704220182> Could not search for results please try again!",
reloaded: "All files reloaded!", reloaded: "All files reloaded!",
removed: "🗑 removed `%SONG%` from the queue!", removed: "🗑 removed `%SONG%` from the queue!",
reset: "<:green_check_mark:674265384777416705> Reset __all__ guild settings!", reset: "<:green_check_mark:674265384777416705> Reset __all__ guild settings!",

16
struct/funcs/saveDB.js Normal file
View File

@ -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,
});
});
}
}