Permalink
Browse files

Remove streamers who have left the Discord server.

  • Loading branch information...
roncli committed Feb 14, 2018
1 parent 320ffc4 commit 6e9908ff57194818e96436f328314b476ef54082
Showing with 92 additions and 76 deletions.
  1. +1 −3 README.md
  2. +88 −70 discord.js
  3. +3 −3 index.js
@@ -34,9 +34,7 @@ The database is quite simple. Here is the script that creates it:
CREATE TABLE streamer(
id int IDENTITY(1,1) NOT NULL,
discord varchar(50) NOT NULL,
streamer varchar(50) NOT NULL,
code int NOT NULL,
validated bit NOT NULL CONSTRAINT DF_streamer_validated DEFAULT (0)
streamer varchar(50) NOT NULL
)
settings.js
@@ -185,7 +185,7 @@ class Discord {
).then((data) => {
if (data.recordsets[0][0].streamers === 0) {
Db.query(
"insert into streamer (streamer, discord, code, validated) values (@streamer, @discord, 0, 1)",
"insert into streamer (streamer, discord) values (@streamer, @discord)",
{
streamer: {type: Db.VARCHAR(50), value: user},
discord: {type: Db.VARCHAR(50), value: newMember.id}
@@ -379,89 +379,107 @@ class Discord {
* @returns {void}
*/
static checkStreams() {
Twitch.getStreams(["sixgaminggg"].concat(streamers, hosts).join(",")).then((streams) => {
const wentOffline = [],
wentLive = [];
let live = [];
// Get the list of live channels.
try {
live = streams.map((stream) => stream.channel.name.toLowerCase());
} catch (err) {
Log.exception("Error checking streams.", err);
setTimeout(() => Discord.checkStreams(), 60000);
return;
}
// Remove users who left the server.
Db.query("select streamer, discord from streamer").then((data) => {
data.recordsets[0].forEach((streamer) => {
const {discord: id, streamer: name} = streamer;
if (!Discord.findUserById(id)) {
Db.query("delete from streamer where discord = @id", {id: {type: Db.VARCHAR(50), value: id}}).then(() => {
Discord.removeStreamer(name);
}).catch((err) => {
Log.exception(`Database error removing streamer \`${name}\` \`${id}\`.`, err);
});
}
});
// Being empty once is usually a sign of an error. Will try again next time.
if (live.length === 0) {
if (!wasEmptyLast) {
Log.log("Live list was empty.");
wasEmptyLast = true;
setTimeout(Discord.checkStreams, 60000);
Twitch.getStreams(["sixgaminggg"].concat(streamers, hosts).join(",")).then((streams) => {
const wentOffline = [],
wentLive = [];
let live = [];
// Get the list of live channels.
try {
live = streams.map((stream) => stream.channel.name.toLowerCase());
} catch (err) {
Log.exception("Error checking streams.", err);
setTimeout(() => Discord.checkStreams(), 60000);
return;
}
} else {
wasEmptyLast = false;
}
// Detect which streams have gone offline.
for (const key in liveChannels) {
if (live.indexOf(key) === -1) {
wentOffline.push(key);
// Being empty once is usually a sign of an error. Will try again next time.
if (live.length === 0) {
if (!wasEmptyLast) {
Log.log("Live list was empty.");
wasEmptyLast = true;
setTimeout(Discord.checkStreams, 60000);
return;
}
} else {
wasEmptyLast = false;
}
}
// Remove live channel data from offline streams.
wentOffline.forEach((name) => {
if (name.toLowerCase() === "sixgaminggg") {
discord.user.setStatus("online");
discord.user.setActivity(null, {});
// Detect which streams have gone offline.
for (const key in liveChannels) {
if (live.indexOf(key) === -1) {
wentOffline.push(key);
}
}
delete liveChannels[name];
});
// Detect which streams have gone online.
live.forEach((name) => {
if (!liveChannels[name]) {
wentLive.push(name);
}
});
// Remove live channel data from offline streams.
wentOffline.forEach((name) => {
if (name.toLowerCase() === "sixgaminggg") {
discord.user.setStatus("online");
discord.user.setActivity(null, {});
}
delete liveChannels[name];
});
// Save channel data.
streams.forEach((stream) => {
liveChannels[stream.channel.name.toLowerCase()] = stream;
});
// Detect which streams have gone online.
live.forEach((name) => {
if (!liveChannels[name]) {
wentLive.push(name);
}
});
// Discord notifications for new live channels.
wentLive.forEach((stream) => {
Discord.announceStream(liveChannels[stream]);
});
// Save channel data.
streams.forEach((stream) => {
liveChannels[stream.channel.name.toLowerCase()] = stream;
});
// If manual hosting is active, check it. Afterwards, update hosting.
if (manualHosting) {
Twitch.getChannelStream(currentHost).then((results) => {
manualHosting = results && results.stream;
// Discord notifications for new live channels.
wentLive.forEach((stream) => {
Discord.announceStream(liveChannels[stream]);
});
if (manualHosting) {
({stream: liveChannels[currentHost]} = results);
} else {
delete liveChannels[currentHost];
currentHost = "";
}
// If manual hosting is active, check it. Afterwards, update hosting.
if (manualHosting) {
Twitch.getChannelStream(currentHost).then((results) => {
manualHosting = results && results.stream;
if (manualHosting) {
({stream: liveChannels[currentHost]} = results);
} else {
delete liveChannels[currentHost];
currentHost = "";
}
Discord.updateHosting(live);
}).catch((err) => {
// Skip the current round of checking the manual host, and just check again next time.
Discord.updateHosting(live);
Log.exception(`Error checking current host ${currentHost}.`, err);
});
} else {
Discord.updateHosting(live);
}).catch((err) => {
// Skip the current round of checking the manual host, and just check again next time.
Discord.updateHosting(live);
Log.exception(`Error checking current host ${currentHost}.`, err);
});
} else {
Discord.updateHosting(live);
}
}).catch(() => {
// This is commonly not an error, just try again in a minute.
setTimeout(Discord.checkStreams, 60000);
}
}).catch(() => {
// This is commonly not an error, just try again in a minute.
setTimeout(Discord.checkStreams, 60000);
});
}).catch((err) => {
Log.exception("Database error getting streamers.", err);
setTimeout(() => Discord.checkStreams(), 60000);
});
}
@@ -16,8 +16,8 @@ const Db = require("./database"),
(function startup() {
Log.log("Starting up...");
// Get validated streamers and hosted channels.
Db.query("select streamer from streamer where validated = 1; select streamer from host").then((data) => {
// Get streamers and hosted channels.
Db.query("select streamer from streamer; select streamer from host").then((data) => {
Log.log("Got streamer data.");
// Startup tmi
@@ -33,6 +33,6 @@ const Db = require("./database"),
data.recordsets[1].forEach((streamer) => Discord.addHost(streamer.streamer));
}).catch((err) => {
setTimeout(startup, 60000);
Log.exception("There was a database error getting validated streamers and hosted channels.", err);
Log.exception("There was a database error getting streamers and hosted channels.", err);
});
}());

0 comments on commit 6e9908f

Please sign in to comment.