Permalink
Browse files

Fix various bugs with auto hosting.

  • Loading branch information...
roncli committed Jan 22, 2018
1 parent 9b04f07 commit 1fcae1d6f86dd1fd08d595a3e22c4dc5aa7b7284
Showing with 46 additions and 18 deletions.
  1. +1 −0 .eslintrc.json
  2. +23 −4 commands.js
  3. +13 −11 discord.js
  4. +9 −3 tmi.js
@@ -35,6 +35,7 @@
"max-lines": "off",
"max-params": "off",
"max-statements": "off",
"multiline-comment-style": "off",
"multiline-ternary": "off",
"new-cap": [
"error",
@@ -401,16 +401,35 @@ class Commands {
return;
}
if (Discord.currentHost === message) {
commands.service.queue(`Sorry, ${user}, but I am already hosting ${message}.`);
reject(new Error("Cannot host the currently hosted channel."));
return;
}
Twitch.getChannelStream(message).then((results) => {
Discord.manualHosting = results && results.stream;
if (Discord.manualHosting) {
Discord.currentHost = message;
Tmi.host("sixgaminggg", Discord.currentHost).then(() => {
Tmi.host("sixgaminggg", message).then(() => {
Discord.currentHost = message;
Tmi.queue(`Now hosting ${Discord.currentHost}. Check out their stream at http://twitch.tv/${Discord.currentHost}!`);
Discord.announceStream(results.stream);
resolve(true);
}).catch((err) => {
if (err === "bad_host_hosting") {
commands.service.queue(`Sorry, ${user}, but I am already hosting ${message}.`);
reject(new Error("Cannot host the currently hosted channel."));
return;
}
if (err === "bad_host_error") {
commands.service.queue(`Sorry, ${user}, but Twitch is having issues. Try hosting again later.`);
reject(new Error("Twitch error while attempting to host."));
return;
}
commands.service.queue(`Sorry, ${user}, but the server is currently down. Try later, or get a hold of roncli for fixing.`);
reject(new Exception("There was a Twitch chat error while attempting to host a channel.", err));
});
@@ -459,7 +478,7 @@ class Commands {
return;
}
Tmi.unhost("sixgaminggg");
Tmi.unhost("sixgaminggg").catch(() => {});
commands.service.queue("Exiting host mode.");
Discord.manualHosting = false;
Discord.currentHost = "";
@@ -615,7 +634,7 @@ class Commands {
"insert into host (streamer) values (@streamer)",
{streamer: {type: Db.VARCHAR(50), value: message}}
).then(() => {
Discord.addHost(message.toLowerCase());
Discord.addHost(message);
commands.service.queue(`${user}, you have successfully added ${message} as a streamer to be hosted.`);
resolve(true);
}).catch((err) => {
@@ -43,6 +43,7 @@ class Discord {
static get discord() {
return discord;
}
// # # # #
// # # # #
// ## # # ### ### ## ### ### #### ## ### ###
@@ -196,8 +197,8 @@ class Discord {
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.toLowerCase());
Discord.removeHost(user.toLowerCase());
Discord.addStreamer(user);
Discord.removeHost(user);
}).catch((err) => {
Log.exception("There was a database error inserting into the streamer table.", err);
});
@@ -460,8 +461,9 @@ class Discord {
} else {
Discord.updateHosting(live);
}
}).catch((err, results) => {
Log.exception(`Error getting streams: ${results}`, err);
}).catch(() => {
// This is commonly not an error, just try again in a minute.
setTimeout(Discord.checkStreams, 60000);
});
}
@@ -518,7 +520,7 @@ class Discord {
currentHost = liveStreamers[0].toLowerCase();
Tmi.queue(`Now hosting Six Gamer ${currentHost}. Check out their stream at http://twitch.tv/${currentHost}!`);
Tmi.host("sixgaminggg", currentHost).catch((err) => {
if (err !== "bad_host_hosting") {
if (["bad_host_hosting", "bad_host_error"].indexOf(err) === -1) {
Log.exception("Problem hosting channel.", err);
}
});
@@ -528,7 +530,7 @@ class Discord {
hostingTimestamps.splice(0, 1);
}
streamers.splice(streamers.indexOf(currentHost), 1);
streamers.push(currentHost);
Discord.addHost(currentHost);
}
setTimeout(Discord.checkStreams, 60000);
return;
@@ -543,11 +545,11 @@ class Discord {
// Try to host a live host.
liveStreamers = hosts.filter((host) => live.indexOf(host.toLowerCase()) !== -1);
if (liveStreamers.length > 0) {
if (liveStreamers[0] !== currentHost) {
if (liveStreamers[0].toLowerCase() !== currentHost) {
currentHost = liveStreamers[0].toLowerCase();
Tmi.queue(`Now hosting ${currentHost}. Check out their stream at http://twitch.tv/${currentHost}!`);
Tmi.host("sixgaminggg", currentHost).catch((err) => {
if (err !== "bad_host_hosting") {
if (["bad_host_hosting", "bad_host_error"].indexOf(err) === -1) {
Log.exception("Problem hosting channel.", err);
}
});
@@ -557,7 +559,7 @@ class Discord {
hostingTimestamps.splice(0, 1);
}
hosts.splice(hosts.indexOf(currentHost), 1);
hosts.push(currentHost);
Discord.addHost(currentHost);
}
setTimeout(Discord.checkStreams, 60000);
return;
@@ -756,7 +758,7 @@ class Discord {
* @returns {void}
*/
static addStreamer(name) {
streamers.push(name);
streamers.push(name.toLowerCase());
}
// ## #
@@ -790,7 +792,7 @@ class Discord {
* @returns {void}
*/
static addHost(name) {
hosts.push(name);
hosts.push(name.toLowerCase());
}
// # # #
12 tmi.js
@@ -46,7 +46,9 @@ class Tmi {
tmi.on("connected", () => {
Log.log("Connected to tmi.");
tmi.raw("CAP REQ :twitch.tv/membership");
tmi.raw("CAP REQ :twitch.tv/membership").catch((err) => {
Log.exception("RAW command failed.", err);
});
});
tmi.on("disconnected", (ev) => {
@@ -101,7 +103,9 @@ class Tmi {
*/
static connect() {
Log.log("Connecting to tmi...");
tmi.connect();
tmi.connect().catch((err) => {
Log.exception("TMI connection failed.", err);
});
// Setup IRC command rotation.
clearTimeout(commandRotationTimeout);
@@ -152,7 +156,9 @@ class Tmi {
* @returns {Promise} A promise that resolves when the message is sent.
*/
static queue(message) {
return tmi.say("sixgaminggg", message);
return tmi.say("sixgaminggg", message).catch((err) => {
Log.exception("SAY command failed.", err);
});
}
// # #

0 comments on commit 1fcae1d

Please sign in to comment.