Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Improve error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Nov 5, 2018
1 parent 1de9f2a commit dd95d92
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 24 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -30,6 +30,7 @@
"max-depth": "off", "max-depth": "off",
"max-len": "off", "max-len": "off",
"max-lines": "off", "max-lines": "off",
"max-lines-per-function": "off",
"max-params": "off", "max-params": "off",
"max-statements": "off", "max-statements": "off",
"multiline-comment-style": "off", "multiline-comment-style": "off",
Expand Down
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -93,10 +93,13 @@ Coming Soon


1.4.1 1.4.1
----- -----
* Remove streamers that are no longer on the Discord server.
* Fixed multiple Twitch live announcements.
* Discord.js update to get channels inside categories working as expected. * Discord.js update to get channels inside categories working as expected.
* Added Brigitte to randomonium. * Added Brigitte to randomonium.
* Remove streamers that are no longer on the Discord server.
* Use database library for improved error handling. * Use database library for improved error handling.
* Improved error logging.
* Added Brigitte and Wrecking Ball to randomonium.
* Get out. 👉 * Get out. 👉


Version History Version History
Expand Down
10 changes: 9 additions & 1 deletion commands.js
Expand Up @@ -7,7 +7,15 @@ const Db = require("./database"),
addGameParse = /^([a-zA-Z0-9]{2,50}) +(.{2,255})$/, addGameParse = /^([a-zA-Z0-9]{2,50}) +(.{2,255})$/,
userCreatedChannels = {}; userCreatedChannels = {};


let Discord, Tmi; /**
* @type {typeof import("./discord")}
*/
let Discord;

/**
* @type {typeof import("./tmi")}
*/
let Tmi;


// ### # // ### #
// # # # // # # #
Expand Down
79 changes: 58 additions & 21 deletions discord.js
Expand Up @@ -40,6 +40,15 @@ let currentHost = "",
* A static class that handles all Discord.js interctions. * A static class that handles all Discord.js interctions.
*/ */
class Discord { class Discord {
// # # #
// # #
// ### ## ### ## ## ### ###
// # # # ## # # # # # # #
// # # # ## # # # # # #
// ### ### ### ## ## # ###
/**
* @returns {Client} The Discord object.
*/
static get discord() { static get discord() {
return discord; return discord;
} }
Expand Down Expand Up @@ -121,7 +130,7 @@ class Discord {
static startup() { static startup() {
Discord.commands = new Commands(Discord); Discord.commands = new Commands(Discord);


discord.addListener("ready", () => { discord.on("ready", () => {
Log.log("Connected to Discord."); Log.log("Connected to Discord.");


sixGuild = discord.guilds.find("name", "Six Gaming"); sixGuild = discord.guilds.find("name", "Six Gaming");
Expand All @@ -147,17 +156,13 @@ class Discord {
} }
}); });


discord.on("disconnect", (ev) => { discord.on("message", (message) => {
Log.exception("Disconnected from Discord.", ev);
});

discord.addListener("message", (message) => {
if (message.guild && message.guild.name === "Six Gaming" && message.channel.name === "sixbotgg" && message.channel.type === "text") { if (message.guild && message.guild.name === "Six Gaming" && message.channel.name === "sixbotgg" && message.channel.type === "text") {
Discord.message(message.author, message.content); Discord.message(message.author, message.content);
} }
}); });


discord.addListener("voiceStateUpdate", (oldMember, newMember) => { discord.on("voiceStateUpdate", (oldMember, newMember) => {
if (oldMember.voiceChannel) { if (oldMember.voiceChannel) {
if (oldMember.voiceChannel.name !== "\u{1F4AC} General" && oldMember.voiceChannel.members.size === 0) { if (oldMember.voiceChannel.name !== "\u{1F4AC} General" && oldMember.voiceChannel.members.size === 0) {
Discord.markEmptyVoiceChannel(oldMember.voiceChannel); Discord.markEmptyVoiceChannel(oldMember.voiceChannel);
Expand All @@ -172,7 +177,7 @@ class Discord {
} }
}); });


discord.addListener("presenceUpdate", (oldMember, newMember) => { discord.on("presenceUpdate", (oldMember, newMember) => {
if (newMember.presence && newMember.presence.game && newMember.presence.game.streaming && newMember.presence.game.url && newMember.presence.game.url.includes("twitch.tv")) { if (newMember.presence && newMember.presence.game && newMember.presence.game.streaming && newMember.presence.game.url && newMember.presence.game.url.includes("twitch.tv")) {
const matches = urlParse.exec(newMember.presence.game.url); const matches = urlParse.exec(newMember.presence.game.url);


Expand Down Expand Up @@ -210,20 +215,29 @@ class Discord {
// # # # # # # # ## # # // # # # # # # # ## # #
// ## ## # # # # ## ## ## // ## ## # # # # ## ## ##
/** /**
* Connects to Discord. * Connects to Discord. Should only ever be called once.
* @returns {void} * @returns {void}
*/ */
static connect() { static connect() {
discord.on("error", (err) => {
if (err.code === "ECONNRESET") {
Log.warning("Connection reset from Discord.");
} else {
Log.exception("Discord error.", err.error || err);
}
});

discord.on("disconnect", (err) => {
Log.exception("Disconnected from Discord.", err);
});

Log.log("Connecting to Discord..."); Log.log("Connecting to Discord...");

discord.login(settings.discord.token).then(() => { discord.login(settings.discord.token).then(() => {
Log.log("Connected."); Log.log("Connected.");
}).catch((err) => { }).catch((err) => {
Log.exception("Error connecting to Discord, will automatically retry.", err); Log.exception("Error connecting to Discord, will automatically retry.", err);
}); });

discord.on("error", (err) => {
Log.exception("Discord error.", err.error || err);
});
} }


// # ## # # // # ## # #
Expand All @@ -237,8 +251,7 @@ class Discord {
* @returns {boolean} Whether the bot is connected to Discord. * @returns {boolean} Whether the bot is connected to Discord.
*/ */
static isConnected() { static isConnected() {
// Note: " && discord.ws && discord.ws.connection" will be unneeded in Discord.js v11.3.0. return discord && sixGuild ? discord.status === 0 : false;
return discord && sixGuild && discord.ws && discord.ws.connection ? discord.status === 0 : false;
} }


// # # ## ### ### ### ### ## // # # ## ### ### ### ### ##
Expand Down Expand Up @@ -428,13 +441,25 @@ class Discord {
} }


// Remove live channel data from offline streams. // Remove live channel data from offline streams.
wentOffline.forEach((name) => { for (const name of wentOffline) {
if (name.toLowerCase() === "sixgaminggg") { if (name.toLowerCase() === "sixgaminggg") {
discord.user.setStatus("online"); discord.user.setStatus("online").catch((err) => {
discord.user.setActivity(null, {}); if (err.code === "ECONNRESET") {
Log.warning("Connection reset while setting status to online.");
} else {
Log.exception("Error setting status to online.", err);
}
});
discord.user.setActivity(null, {}).catch((err) => {
if (err.code === "ECONNRESET") {
Log.warning("Connection reset while removing activity.");
} else {
Log.exception("Error removing activity.", err);
}
});
} }
delete liveChannels[name]; delete liveChannels[name];
}); }


// Detect which streams have gone online. // Detect which streams have gone online.
live.forEach((name) => { live.forEach((name) => {
Expand Down Expand Up @@ -643,8 +668,20 @@ class Discord {
manualHosting = false; manualHosting = false;
Tmi.unhost("sixgaminggg").catch(() => {}); Tmi.unhost("sixgaminggg").catch(() => {});
Tmi.queue("What's going on everyone? Six Gaming is live!"); Tmi.queue("What's going on everyone? Six Gaming is live!");
discord.user.setStatus("online"); discord.user.setStatus("online").catch((err) => {
discord.user.setActivity(stream.channel.status, {url: "http://twitch.tv/SixGamingGG", type: "STREAMING"}); if (err.code === "ECONNRESET") {
Log.warning("Connection reset while setting status to online.");
} else {
Log.exception("Error setting status to online.", err);
}
});
discord.user.setActivity(stream.channel.status, {url: "http://twitch.tv/SixGamingGG", type: "STREAMING"}).catch((err) => {
if (err.code === "ECONNRESET") {
Log.warning("Connection reset while setting activity.");
} else {
Log.exception("Error setting activity.", err);
}
});
} else if (streamers.indexOf(stream.channel.display_name.toLowerCase()) !== -1) { } else if (streamers.indexOf(stream.channel.display_name.toLowerCase()) !== -1) {
message.embed.description = `${streamNotifyRole} - Six Gamer ${stream.channel.display_name} just went live on Twitch! Watch at ${stream.channel.url}`; message.embed.description = `${streamNotifyRole} - Six Gamer ${stream.channel.display_name} just went live on Twitch! Watch at ${stream.channel.url}`;
} else if (hosts.indexOf(stream.channel.display_name.toLowerCase()) !== -1) { // eslint-disable-line no-negated-condition } else if (hosts.indexOf(stream.channel.display_name.toLowerCase()) !== -1) { // eslint-disable-line no-negated-condition
Expand Down
3 changes: 3 additions & 0 deletions log.js
Expand Up @@ -2,6 +2,9 @@ const util = require("util"),


queue = []; queue = [];


/**
* @type {typeof import("./discord")}
*/
let Discord; let Discord;


// # // #
Expand Down
3 changes: 2 additions & 1 deletion randomonium.js
Expand Up @@ -25,7 +25,8 @@ const owHeroes = [
"Orisa", "Orisa",
"Doomfist", "Doomfist",
"Moira", "Moira",
"Brigitte" "Brigitte",
"Wrecking Ball"
]; ];


// #### # # // #### # #
Expand Down

0 comments on commit dd95d92

Please sign in to comment.