Permalink
Browse files

Fix bug with streamers/hosts being listed multiple times in stream an…

…nouncements. Fix bug with hosts not being removed after becoming streamers. Fix error handling. Log unhandled promise rejections.
  • Loading branch information...
roncli committed Jun 21, 2018
1 parent a022683 commit 1de9f2a276719072aa173445c7a715f26ab3f2bc
Showing with 49 additions and 57 deletions.
  1. +16 −0 database.js
  2. +10 −6 discord.js
  3. +4 −0 index.js
  4. +7 −49 log.js
  5. +12 −2 tmi.js
@@ -92,6 +92,22 @@ class Database {
return db.query("delete from host where id = @id", {id: {type: Db.INT, value: id}});
}
// # ## # # # # ### # #
// # # # # # # # # # #
// ### ## # ## ### ## #### ## ### ### ### # # # # ### ## ###
// # # # ## # # ## # # ## # # # # ## # # # # # # # ## # ## # #
// # # ## # ## # ## # # # # ## # # # # # # # ## ## #
// ### ## ### ## ## ## # # ## ### ## ### # ## ### ## #
// #
/**
* Deletes a host by their stream name.
* @param {string} name The name of the host to delete.
* @returns {Promise} A promise that resolves when the host has been deleted.
*/
static deleteHostByUser(name) {
return db.query("delete from host where streamer = @user", {user: {type: Db.VARCHAR(50), value: name}});
}
// # ## # ## # ### ### # #
// # # # # # # # # # # #
// ### ## # ## ### ## # ### ### ## ### # # ## ### ### # # # # ## ### ## ## ### ###
@@ -182,11 +182,15 @@ class Discord {
Db.streamerExistsByDiscord(newMember.id).then((exists) => {
if (!exists) {
Db.addStreamer(user, newMember.id).then(() => {
Discord.addStreamersRole(newMember);
Discord.queue(`${newMember}, you are now setup as a Six Gaming streamer at http://twitch.tv/${user}. If you would like a text channel on Discord for your Twitch community, you can use \`!addmychannel\`.`);
Discord.addStreamer(user);
Discord.removeHost(user);
Db.deleteHostByUser(user).then(() => {
Discord.addStreamersRole(newMember);
Discord.queue(`${newMember}, you are now setup as a Six Gaming streamer at http://twitch.tv/${user}. If you would like a text channel on Discord for your Twitch community, you can use \`!addmychannel\`.`);
Discord.addStreamer(user);
Discord.removeHost(user);
}).catch((err) => {
Log.exception("There was a database error while removing a hosted streamer after becoming a Six Gaming streamer.", err);
});
}).catch((err) => {
Log.exception("There was a database error inserting into the streamer table.", err);
});
@@ -434,7 +438,7 @@ class Discord {
// Detect which streams have gone online.
live.forEach((name) => {
if (!liveChannels[name]) {
if (!liveChannels[name] && wentLive.indexOf(name) === -1) {
wentLive.push(name);
}
});
@@ -36,3 +36,7 @@ const Db = require("./database"),
Log.exception("There was a database error getting streamers and hosted channels.", err);
});
}());
process.on("unhandledRejection", (err) => {
Log.exception("Unhandled promise rejection caught.", err);
});
56 log.js
@@ -1,4 +1,6 @@
const queue = [];
const util = require("util"),
queue = [];
let Discord;
@@ -15,33 +17,6 @@ let Discord;
* A class that handles logging.
*/
class Log {
// ## ####
// # #
// ### ## ### # ### ## ## ### ### ### ## ### ###
// # # # ## # # # # # # # ## # # # # # # # # # ##
// # ## # # # # ## # ## # # # # # # ##
// # ## ### ### # # ## ## #### # # ## # ###
// #
/**
* A JSON.stringify helper function that turns errors into normal objects prior to stringifying them.
* @param {*} _ Unused.
* @param {*} value The object to be translated.
* @returns {*} The original object if not an error, or the error as an object.
*/
static replaceErrors(_, value) {
if (value instanceof Error) {
const error = {};
Object.getOwnPropertyNames(value).forEach((key) => {
({[key]: error[key]} = value);
});
return error;
}
return value;
}
// ##
// #
// # ## ###
@@ -142,27 +117,10 @@ class Log {
}
if (log.obj) {
switch (typeof log.obj) {
case "string":
message.embed.fields.push({
name: "Message",
value: log.obj
});
break;
default:
if (log.obj instanceof Error) {
message.embed.fields.push({
name: "Stack Trace",
value: `\`\`\`${JSON.stringify(log.obj, Log.replaceErrors)}\`\`\``
});
} else {
message.embed.fields.push({
name: "Data",
value: `\`\`\`${JSON.stringify(log.obj, Log.replaceErrors)}\`\`\``
});
}
break;
}
message.embed.fields.push({
name: "Message",
value: util.inspect(log.obj)
});
}
Discord.richQueue(message, log.type === "exception" ? errorChannel : logChannel);
14 tmi.js
@@ -174,7 +174,13 @@ class Tmi {
* @return {Promise} A promise that resolves when hosting completes.
*/
static host(hostingChannel, hostedChannel) {
return tmi.host(hostingChannel, hostedChannel);
return tmi.host(hostingChannel, hostedChannel).catch((err) => {
if (err === "No response from Twitch.") {
Log.log(`Host command from ${hostingChannel} to ${hostedChannel} failed due to no response from Twitch.`);
} else {
Log.exception(`Host command from ${hostingChannel} to ${hostedChannel} failed.`, err);
}
});
}
// # #
@@ -190,7 +196,11 @@ class Tmi {
*/
static unhost(hostingChannel) {
return tmi.unhost(hostingChannel).catch((err) => {
Log.exception("Unhost command failed.", err);
if (err === "No response from Twitch.") {
Log.log(`Unhost command from ${hostingChannel} failed due to no response from Twitch.`);
} else {
Log.exception(`Unhost command from ${hostingChannel} failed.`, err);
}
});
}

0 comments on commit 1de9f2a

Please sign in to comment.