From 2d7752d2cf0bdbf45bea961e178b91dd09062864 Mon Sep 17 00:00:00 2001 From: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Mon, 22 Jun 2020 00:41:30 +0300 Subject: [PATCH] additional error logging and debugging. --- index.js | 10 ++++++- src/bot.js | 6 ++++ src/commands/join.js | 2 +- src/commands/settings.js | 11 ++------ src/commands/settings/setDj.js | 8 ++++-- src/events/clientEvents/handler.js | 33 ++++++++++++++++++++++ src/events/connectionEvents/handler.js | 21 ++++++++++++++ src/events/dispatcherEvents/error.js | 3 +- src/events/dispatcherEvents/finish.js | 38 ++++++++++++++------------ src/events/dispatcherEvents/handler.js | 16 +++++++++++ src/events/events.js | 19 ------------- src/struct/client.js | 5 +--- src/struct/config/config.js | 4 +-- src/struct/funcs/exe.js | 12 ++------ src/struct/funcs/ffmpeg.js | 2 +- src/struct/funcs/handleVideo.js | 5 ++-- src/struct/funcs/play.js | 16 ++--------- src/struct/funcs/shuffle.js | 2 +- 18 files changed, 126 insertions(+), 87 deletions(-) create mode 100644 src/events/clientEvents/handler.js create mode 100644 src/events/connectionEvents/handler.js create mode 100644 src/events/dispatcherEvents/handler.js delete mode 100644 src/events/events.js diff --git a/index.js b/index.js index b5c1ebb3..e1cb4b40 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,9 @@ if (config.devMode) { config.shards = 1; } -const { ShardingManager } = require("discord.js"); +const { + ShardingManager +} = require("discord.js"); const manager = new ShardingManager("./src/bot.js", { token: config.token, respawn: config.respawn, @@ -28,3 +30,9 @@ console.log = function (arg) { oldConsole.log(arg); if (!config.devMode) webhookClient.send(arg); }; + +oldConsole.error = console.error; +console.error = function (arg) { + oldConsole.error(arg); + if (!client.config.devMode) webhookClient.send(arg); +}; \ No newline at end of file diff --git a/src/bot.js b/src/bot.js index 58208e1b..e78200d3 100644 --- a/src/bot.js +++ b/src/bot.js @@ -9,3 +9,9 @@ console.log = function (arg) { oldConsole.log(arg); if (!client.config.devMode) webhookClient.send(arg); }; + +oldConsole.error = console.error; +console.error = function (arg) { + oldConsole.error(arg); + if (!client.config.devMode) webhookClient.send(arg); +}; \ No newline at end of file diff --git a/src/commands/join.js b/src/commands/join.js index 14f69506..1cff58f5 100644 --- a/src/commands/join.js +++ b/src/commands/join.js @@ -17,7 +17,7 @@ module.exports = { msg.channel.send(`${client.messages.joined} ${voiceChannel.name}!`); } catch (error) { client.queue.delete(msg.guild.id); - client.users.cache.get(client.config.devId).send(client.messages.errorConnecting + error); + console.log(error); return msg.channel.send(client.messages.error); } } diff --git a/src/commands/settings.js b/src/commands/settings.js index ff47b36a..2e71e095 100644 --- a/src/commands/settings.js +++ b/src/commands/settings.js @@ -68,15 +68,8 @@ module.exports = { option.execute(msg, args, client); } catch (error) { msg.reply(client.messages.errorExeOpt); - const embed = new Discord.MessageEmbed() - .setTitle(`Musix ${error.toString()}`) - .setDescription(error.stack.replace(/at /g, "**at **")) - .setColor(client.config.embedColor); - client - .fetchUser(client.config.devId) - .then((user) => user.send(embed)) - .catch(console.error); - client.users.cache.get(client.config.devId).send(embed); + console.log(error.toString()); + console.log(error.stack.replace(/at /g, "**at **")); } } else { return msg.channel.send(embed); diff --git a/src/commands/settings/setDj.js b/src/commands/settings/setDj.js index 106ce187..d502df1e 100644 --- a/src/commands/settings/setDj.js +++ b/src/commands/settings/setDj.js @@ -13,10 +13,12 @@ module.exports = { const permissions = msg.channel.permissionsFor(msg.client.user); if (!permissions.has('MANAGE_ROLES')) return msg.channel.send(client.messages.noPermsManageRoles); msg.guild.createRole({ - name: 'DJ', - }) + name: 'DJ', + }) .then(role => client.global.db.guilds[msg.guild.id].djrole = role.id) - .catch(console.error) + .catch((error) => { + console.log(error); + }) client.global.db.guilds[msg.guild.id].dj = true; msg.channel.send(client.messages.djRoleCreated); } diff --git a/src/events/clientEvents/handler.js b/src/events/clientEvents/handler.js new file mode 100644 index 00000000..43fe765d --- /dev/null +++ b/src/events/clientEvents/handler.js @@ -0,0 +1,33 @@ +module.exports = function (client) { + const Discord = require('discord.js'); + client.on('ready', () => { + require(`./ready.js`).execute(client, Discord); + }).on('message', (msg) => { + require(`./msg.js`).execute(client, msg, Discord); + }).on('guildCreate', (guild) => { + require(`./guildCreate.js`).execute(client, guild); + }).on('voiceStateUpdate', (oldState, newState) => { + require(`./voiceStateUpdate.js`).execute(client, oldState, newState); + }).on('error', (error) => { + console.log(error); + }).on('debug', (info) => { + if (client.config.devMode) console.log(info); + }).on('invalidated', () => { + console.log("Client session invalidated! Exiting the process!") + process.exit(1); + }).on('rateLimit', (rateLimitInfo) => { + + }).on('shardDisconnect', (event, id) => { + console.log(`Shard ${id} disconnected event ${event}`); + }).on('shardError', (error, shardId) => { + console.log(`Shard ${shardId} error ${error}`); + }).on('shardReady', (id, unavailableGuilds) => { + console.log(`Shard ${id} ready. Unavailable guilds: ${unavailableGuilds || 0}`); + }).on('shardReconnecting', (id) => { + console.log(`shard ${id} reconnecting.`); + }).on('shardResume', (id, replayedEvents) => { + console.log(`shard ${id} resume events ${replayedEvents}`); + }).on("warn", (info) => { + console.log(`Warn! info: ${info}`); + }); +} \ No newline at end of file diff --git a/src/events/connectionEvents/handler.js b/src/events/connectionEvents/handler.js new file mode 100644 index 00000000..5b5b68e4 --- /dev/null +++ b/src/events/connectionEvents/handler.js @@ -0,0 +1,21 @@ +module.exports = function (client, connection) { + connection.on("authenticated", () => { + if (client.config.devMode) console.log("Voice connection initiated."); + }).on("debug", (message) => { + if (client.config.devMode) console.log(message); + }).on("disconnect", () => { + if (client.config.devMode) console.log("Voice connection disconnected."); + }).on("error", (error) => { + console.log(error); + }).on("failed", (error) => { + if (client.config.devMode) console.log(error); + }).on("newSession", () => { + if (client.config.devMode) console.log("New voice session id received!"); + }).on("ready", () => { + if (client.config.devMode) console.log("Voice connection ready."); + }).on("reconnecting", () => { + if (client.config.devMode) console.log("Voice connection reconnecting."); + }).on("warn", (warning) => { + console.log(`Voice connection warning: ${warning}`); + }) +} \ No newline at end of file diff --git a/src/events/dispatcherEvents/error.js b/src/events/dispatcherEvents/error.js index 6521812a..f8ae59fd 100644 --- a/src/events/dispatcherEvents/error.js +++ b/src/events/dispatcherEvents/error.js @@ -1,12 +1,11 @@ module.exports = async function (client, error, guild) { const queue = client.queue.get(guild.id); - console.error(error); + console.log(error); /*if (error = "Error: input stream: This video contains content from WMG, who has blocked it on copyright grounds.") { queue.endReason = "skip"; queue.connection.dispatcher.end(); return queue.textChannel.send(client.messages.songBlockedWMG); }*/ - client.users.cache.get(client.config.devId).send(client.messages.dispatcherError + error); queue.voiceChannel.leave(); client.queue.delete(guild.id); return queue.textChannel.send(client.messages.errorDispatcher + `\`${error}\``); diff --git a/src/events/dispatcherEvents/finish.js b/src/events/dispatcherEvents/finish.js index daa747be..cfe8896c 100644 --- a/src/events/dispatcherEvents/finish.js +++ b/src/events/dispatcherEvents/finish.js @@ -1,22 +1,24 @@ -module.exports = async function (client, reason, guild) { - const queue = client.queue.get(guild.id); - queue.playing = false; - if (reason === "seek") { - return (queue.playing = true); - } - - if (!queue.songLooping) { - if (queue.looping) { - queue.songs.push(queue.songs[0]); +module.exports = { + async execute(client, guild) { + const queue = client.queue.get(guild.id); + queue.playing = false; + if (queue.endReason === "seek") { + return (queue.playing = true); } - queue.time = 0; - queue.votes = 0; - queue.voters = []; - if (reason !== "replay") { - if (reason === "previous") queue.songs.unshift(queue.prevSongs.pop()) - if (reason !== "previous") queue.prevSongs.push(queue.songs.shift()); + if (!queue.songLooping) { + if (queue.looping) { + queue.songs.push(queue.songs[0]); + } + + queue.time = 0; + queue.votes = 0; + queue.voters = []; + if (queue.endReason !== "replay") { + if (queue.endReason === "previous") queue.songs.unshift(queue.prevSongs.pop()) + if (queue.endReason !== "previous") queue.prevSongs.push(queue.songs.shift()); + } } - } - client.funcs.play(guild, queue.songs[0], client, 0, true); + client.funcs.play(guild, queue.songs[0], client, 0, true); + }, }; \ No newline at end of file diff --git a/src/events/dispatcherEvents/handler.js b/src/events/dispatcherEvents/handler.js new file mode 100644 index 00000000..42ba68c7 --- /dev/null +++ b/src/events/dispatcherEvents/handler.js @@ -0,0 +1,16 @@ +module.exports = function (client, dispatcher, queue, guild) { + dispatcher.on("finish", () => { + if (client.config.devMode) console.log("Dispatcher finish."); + require("./finish").execute(client, guild); + }) + .on("start", () => { + if (client.config.devMode) console.log("Dispatcher start."); + queue.endReason = null; + dispatcher.player.streamingData.pausedTime = 0; + }) + .on("error", (error) => { + require("./error").execute(client, error, guild); + }).on("debug", (info) => { + if (client.config.devMode) console.log(info); + }) +} \ No newline at end of file diff --git a/src/events/events.js b/src/events/events.js deleted file mode 100644 index 5c0ae4cb..00000000 --- a/src/events/events.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = function (client) { - const Discord = require('discord.js'); - const events = './clientEvents/'; - client.on('ready', () => { - require(`${events}ready.js`).execute(client, Discord); - }); - client.on('message', (msg) => { - require(`${events}msg.js`).execute(client, msg, Discord); - }); - client.on('guildCreate', (guild) => { - require(`${events}guildCreate.js`).execute(client, guild); - }); - client.on('voiceStateUpdate', (oldState, newState) => { - require(`${events}voiceStateUpdate.js`).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/src/struct/client.js b/src/struct/client.js index 3725690f..153c5c31 100644 --- a/src/struct/client.js +++ b/src/struct/client.js @@ -7,7 +7,6 @@ const admin = require("firebase-admin"); const serviceAccount = require("./config/serviceAccount.json"); const fs = require("fs"); const path = require("path"); -const events = require("../events/events.js"); const myIntents = new Intents(); myIntents.add( @@ -39,8 +38,6 @@ module.exports = class extends Client { this.messages = require("./config/messages.js"); this.db = admin.firestore(); this.db.FieldValue = require("firebase-admin").firestore.FieldValue; - this.dispatcher.finish = require("../events/dispatcherEvents/finish.js"); - this.dispatcher.error = require("../events/dispatcherEvents/error.js"); this.global = { db: { guilds: {}, @@ -71,7 +68,7 @@ module.exports = class extends Client { this.config.token = this.config.devToken; } - events(this); + require("../events/clientEvents/handler.js")(this); this.login(this.config.token).catch((err) => console.log("Failed to login: " + err) diff --git a/src/struct/config/config.js b/src/struct/config/config.js index b3d8bd4d..7eeb6bfc 100644 --- a/src/struct/config/config.js +++ b/src/struct/config/config.js @@ -24,7 +24,7 @@ module.exports = { embedColor: "#b50002", invite: "https://discordapp.com/oauth2/authorize?client_id=607266889537945605&permissions=3427328&scope=bot", supportServer: "https://discord.gg/rvHuJtB", - devMode: false, + devMode: true, api: false, saveDB: true, respawn: true, @@ -98,4 +98,4 @@ module.exports.emojis = { stop: "<:stop:674685626108477519> ", stopWatch: ":stopwatch: ", volumeHigh: "<:volumehigh:674685637626167307> ", -} +} \ No newline at end of file diff --git a/src/struct/funcs/exe.js b/src/struct/funcs/exe.js index c773b9fa..c20931e4 100644 --- a/src/struct/funcs/exe.js +++ b/src/struct/funcs/exe.js @@ -13,16 +13,8 @@ module.exports = function (msg, args, client, Discord, command) { command.uses++; command.execute(msg, args, client, Discord, command); } catch (error) { - const date = new Date(); msg.reply(client.messages.errorExe); - const embed = new Discord.MessageEmbed() - .setTitle(`Musix ${error.toString()}`) - .setDescription(error.stack.replace(/at /g, "**at **")) - .setFooter( - `guild: ${msg.guild.id} (${msg.guild.name}), user: ${msg.member.id} (${msg.member.displayName}), channel: ${msg.channel.id} (${msg.channel.name}), date: ${date}, Shard: ${client.shard.ids}` - ) - .setColor("#b50002"); - client.users.cache.get(client.config.devId).send(embed); - console.error(error); + console.log(error.toString()); + console.log(error.stack.replace(/at /g, "**at **")); } }; \ No newline at end of file diff --git a/src/struct/funcs/ffmpeg.js b/src/struct/funcs/ffmpeg.js index 3997c378..4182d65f 100644 --- a/src/struct/funcs/ffmpeg.js +++ b/src/struct/funcs/ffmpeg.js @@ -4,6 +4,6 @@ module.exports = async function (client) { await client.channels.fetch(client.config.secondary_test_channel) .then(x => x.join()); } catch (error) { - client.users.cache.get(client.config.devId).send(client.messages.errorDetected + error); + console.log(error); } }; \ No newline at end of file diff --git a/src/struct/funcs/handleVideo.js b/src/struct/funcs/handleVideo.js index ba6e3f0a..35f07f30 100644 --- a/src/struct/funcs/handleVideo.js +++ b/src/struct/funcs/handleVideo.js @@ -57,12 +57,11 @@ module.exports = async function ( try { const connection = await voiceChannel.join(); construct.connection = connection; + require("../../events/connectionEvents/handler")(client, connection); client.funcs.play(msg.guild, construct.songs[0], client, 0, true); } catch (error) { client.queue.delete(msg.guild.id); - client.users.cache - .get(client.config.devId) - .send(client.messages.errorConnecting + error); + console.log(error); return msg.channel.send(client.messages.error + error); } return; diff --git a/src/struct/funcs/play.js b/src/struct/funcs/play.js index e89ab75f..af21cf6a 100644 --- a/src/struct/funcs/play.js +++ b/src/struct/funcs/play.js @@ -30,7 +30,7 @@ module.exports = async function (guild, song, client, seek, play) { if (song.type === "ytdl") input = ytdl(song.url, streamConfig.ytdlOptions) //.on('info', (info, format) => console.log(format)) - .on("error", (err) => console.log(err)); + .on("error", (error) => console.log(error)); const ffmpegArgs = [ "-analyzeduration", @@ -65,19 +65,9 @@ module.exports = async function (guild, song, client, seek, play) { console.log(error); }); - const dispatcher = queue.connection - .play(stream, streamConfig.options) - .on("finish", () => { - client.dispatcher.finish(client, queue.endReason, guild); - }) - .on("start", () => { - queue.endReason = null; - dispatcher.player.streamingData.pausedTime = 0; - }) - .on("error", (error) => { - client.dispatcher.error(client, error, guild); - }); + const dispatcher = queue.connection.play(stream, streamConfig.options) dispatcher.setVolume(queue.volume / 100); + require("../../events/dispatcherEvents/handler")(client, dispatcher, queue, guild); if ((client.global.db.guilds[guild.id].startPlaying && play) || play) { if (song.type !== "ytdl") return; const embed = new Discord.MessageEmbed() diff --git a/src/struct/funcs/shuffle.js b/src/struct/funcs/shuffle.js index 23def67e..c61c360e 100644 --- a/src/struct/funcs/shuffle.js +++ b/src/struct/funcs/shuffle.js @@ -2,7 +2,7 @@ module.exports = function (a) { for (let i = a.length - 1; i > 1; i--) { const j = Math.floor(Math.random() * (i + 1)); if (i === 0 || j === 0) { - console.log(`J or I is 0. I: ${i} J: ${j}`); + // J or I is 0. It works like this so hands off! } else { [a[i], a[j]] = [a[j], a[i]]; }