Permalink
Browse files

Update to auto-make users streamers.

  • Loading branch information...
roncli committed Nov 30, 2017
1 parent c4328cc commit e5ac7b1173d304750d881b428a44848f30c2f889
Showing with 54 additions and 220 deletions.
  1. +0 −215 commands.js
  2. +54 −5 discord.js
View
@@ -467,221 +467,6 @@ class Commands {
});
}
// # #
// # #
// ## ## ### # ## ### # #
// # # # # # ### # # # ####
// # # # # # # # # # #
// ## ## # # # ### # # #
/**
* Confirms the link between a Discord user and a Twitch user. Tmi-only.
* @param {string|User} user The user initiating the command.
* @param {string} message The text of the command.
* @returns {Promise} A promise that resolves when the command completes.
*/
confirm(user, message) {
const commands = this;
return Commands.tmiPromise(commands, (resolve, reject) => {
if (!message || !codeParse.test(message)) {
resolve(false);
return;
}
const code = +message;
Db.query(
"select discord from streamer where streamer = @streamer and code = @code",
{
streamer: {type: Db.VARCHAR(50), value: user},
code: {type: Db.INT, value: code}
}
).then((data) => {
if (data.recordsets[0].length === 0) {
reject(new Error("The streamer's code did not match what was expected."));
return;
}
const {discord: id} = data.recordsets[0][0];
Db.query(
"update streamer set code = 0, validated = 1 where streamer = @streamer;delete from host where streamer = @streamer",
{streamer: {type: Db.VARCHAR(50), value: user}}
).then(() => {
const discordUsers = Discord.findUserById(id);
if (discordUsers.length === 0) {
commands.service.queue(`Sorry, ${user}, but you appear to have left the Discord server. Please rejoin at http://ronc.li/six-discord and then try the !confirm command again.`);
reject(new Error("Cannot find user on the Discord server."));
return;
}
const discordUser = discordUsers[0];
Discord.addStreamersRole(discordUser);
Tmi.queue(`You're all set, ${user}. You are now a Six Gaming streamer!`);
Discord.queue(`${discordUser}, 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());
resolve(true);
}).catch((err) => {
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 database error while setting a streamer to be validated.", err));
});
}).catch((err) => {
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 database error while checking for a streamer's validation code.", err));
});
});
}
// # # ### # # #
// # # # # #
// ### ### ### # # # ## ### ## ###
// # # # # # # # # # # # # # #
// # ## # # # # # #### # # # # #
// # # ### ### # #### ### ## ## # #
/**
* Begins the process of linking a Discord user and a Twitch user. Discord-only.
* @param {string|User} user The user initiating the command.
* @param {string} message The text of the command.
* @returns {Promise} A promise that resolves when the command completes.
*/
addtwitch(user, message) {
const commands = this;
return Commands.discordPromise(commands, (resolve, reject) => {
if (!message) {
resolve(false);
return;
}
Twitch.getChannelStream(message).then((results) => {
if (!results) {
commands.service.queue(`Sorry, ${user}, but ${message} is not a valid Twitch streamer.`);
reject(new Error("Twitch streamer does not exist."));
return;
}
Db.query(
"select discord, code, validated from streamer where streamer = @streamer",
{streamer: {type: Db.VARCHAR(50), value: message}}
).then((data) => {
const {id} = user;
if (data && data.recordsets[0] && data.recordsets[0][0]) {
if (data.recordsets[0][0].discord !== id) {
const users = Discord.findUserById(data.recordsets[0][0].discord);
if (users.length === 0) {
commands.service.queue(`Sorry, ${user}, but ${message} has already been registered. If this is in error, get a hold of roncli for fixing.`);
reject(new Error("Twitch stream has already been registered by another user, and the user is no longer on the server."));
} else {
commands.service.queue(`Sorry, ${user}, but ${message} has already been registered by ${users[0]}. If this is in error, get a hold of roncli for fixing.`);
reject(new Error("Twitch stream has already been registered by another user."));
}
return;
}
if (data.recordsets[0][0].validated) {
commands.service.queue(`Sorry, ${user}, but you're already validated!`);
reject(new Error("Twitch stream has already been added."));
return;
}
commands.service.queue(`${user}, to validate that this is you, please log in to Twitch as ${message}, visit http://twitch.tv/SixGamingGG, and enter the command \`!confirm ${data.recordsets[0][0].code}\` into chat.`);
resolve(true);
return;
}
const code = Math.floor(Math.random() * 900 + 100);
Db.query(
"insert into streamer (streamer, discord, code) values (@streamer, @discord, @code)",
{
streamer: {type: Db.VARCHAR(50), value: message},
discord: {type: Db.VARCHAR(50), value: id},
code: {type: Db.INT, value: code}
}
).then(() => {
commands.service.queue(`${user}, to validate that this is you, please log in to Twitch as ${message}, visit http://twitch.tv/SixGamingGG, and enter the command \`!confirm ${code}\` into chat.`);
resolve(true);
}).catch((err) => {
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 database error inserting into the streamer table.", err));
});
}).catch((err) => {
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 database error while checking to see if the user is a streamer.", err));
});
}).catch((err) => {
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 API error while checking to see if the user is a streamer.", err));
});
});
}
// # # # #
// # # #
// ### ## # # ## # # ## ### # # ## ### ## ###
// # # # ## #### # # # # # ## # # # # # # # #
// # ## # # # # # # ## # #### # # # # #
// # ## # # ## # ## ## #### ### ## ## # #
/**
* Removes the link between a Discord user and a Twitch user. Discord-only.
* @param {string|User} user The user initiating the command.
* @param {string} message The text of the command.
* @returns {Promise} A promise that resolves when the command completes.
*/
removetwitch(user, message) {
const commands = this;
return Commands.discordPromise(commands, (resolve, reject) => {
if (message) {
resolve(false);
return;
}
Db.query(
"select id, streamer from streamer where discord = @discord",
{discord: {type: Db.VARCHAR(50), value: user.id}}
).then((data) => {
if (!data || !data.recordsets[0] || !data.recordsets[0][0]) {
commands.service.queue(`${user}, you are not currently registered as a streamer.`);
reject(new Error("User is not a streamer."));
return;
}
const {id, streamer} = data.recordsets[0][0];
Db.query(
"delete from streamer where id = @id",
{id: {type: Db.INT, value: id}}
).then(() => {
const channel = Discord.findChannelByName(`twitch-${streamer}`);
if (channel) {
channel.delete();
}
Discord.removeStreamersRole(user);
Discord.removeStreamer(streamer);
commands.service.queue(`${user}, you have been removed as a streamer.`);
resolve(true);
}).catch((err) => {
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 database error while removing a streamer.", err));
});
}).catch((err) => {
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 database error while checking to see if the user is a streamer.", err));
});
});
}
// # # # ##
// # # # #
// ### ### ### # # # # ## ### ### ### ### ## #
View
@@ -1,6 +1,7 @@
const DiscordJs = require("discord.js"),
Commands = require("./commands"),
Db = require("./database"),
Log = require("./log"),
settings = require("./settings"),
Tmi = require("./tmi"),
@@ -12,7 +13,8 @@ const DiscordJs = require("discord.js"),
hosts = [],
liveChannels = {},
messageParse = /^!([^ ]+)(?: +(.+[^ ]))? *$/,
streamers = [];
streamers = [],
urlParse = /^https:\/\/www.twitch.tv\/(.+)$/;
let currentHost = "",
lastHost = 0,
@@ -22,7 +24,9 @@ let currentHost = "",
sixBotGGChannel,
sixGuild,
streamNotifyRole,
streamersCategory,
streamersRole,
voiceCategory,
wasEmptyLast = false;
// #### # #
@@ -36,6 +40,10 @@ let currentHost = "",
* A static class that handles all Discord.js interctions.
*/
class Discord {
static get discord() {
return discord;
}
// # # # #
// # # # #
// ## # # ### ### ## ### ### #### ## ### ###
@@ -119,6 +127,8 @@ class Discord {
sixGuild = discord.guilds.find("name", "Six Gaming");
liveStreamAnnouncementsChannel = sixGuild.channels.find("name", "live-stream-announcements");
streamersCategory = sixGuild.channels.find("name", "Streamers");
voiceCategory = sixGuild.channels.find("name", "Voice");
sixBotGGChannel = sixGuild.channels.find("name", "sixbotgg");
streamersRole = sixGuild.roles.find("name", "Streamers");
@@ -161,6 +171,41 @@ class Discord {
}
}
});
discord.addListener("presenceUpdate", (oldMember, newMember) => {
if (newMember.presence && newMember.presence.game && newMember.presence.game.streaming && newMember.presence.game.url && newMember.presence.game.url.contains("twitch.tv")) {
const matches = urlParse.exec(newMember.presence.game.url);
if (matches) {
const user = matches[1];
Db.query(
"select count(id) streamers from streamer where discord = @id",
{id: {type: Db.INT, value: newMember.id}}
).then((data) => {
if (data.recordsets[0][0].streamers === 0) {
Db.query(
"insert into streamer (streamer, discord, code, validated) values (@streamer, @discord, 0, 1)",
{
streamer: {type: Db.VARCHAR(50), value: user},
discord: {type: Db.VARCHAR(50), value: 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.toLowerCase());
Discord.removeHost(user.toLowerCase());
}).catch((err) => {
Log.exception("There was a database error inserting into the streamer table.", err);
});
}
}).catch((err) => {
Log.exception(`Error while checking if ${newMember} is a streamer.`, err);
});
}
}
});
}
// #
@@ -559,7 +604,7 @@ class Discord {
manualHosting = false;
Tmi.unhost("sixgaminggg");
Tmi.queue("What's going on everyone? Six Gaming is live!");
discord.user.setStatus("online", status, "http://twitch.tv/SixGamingGG");
discord.user.setStatus("online", stream.channel.status, "http://twitch.tv/SixGamingGG");
} else if (streamers.indexOf(stream.toLowerCase()) !== -1) {
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.toLowerCase()) !== -1) { // eslint-disable-line no-negated-condition
@@ -610,7 +655,7 @@ class Discord {
positionChannel = (index) => {
const channel = sixGuild.channels.get(channels[index].id);
channel.setPosition(100 + index).then(() => {
channel.edit({position: index}).then(() => {
index++;
if (index < channels.length) {
positionChannel(index);
@@ -876,7 +921,9 @@ class Discord {
* @returns {Promise} A promise that resolves when the channel has been created.
*/
static createTextChannel(name) {
return sixGuild.createChannel(name, "text");
return sixGuild.createChannel(name, "text").then((channel) => {
channel.edit({parent_id: streamersCategory.id}); // eslint-disable-line camelcase
});
}
// # # # # ## # ##
@@ -891,7 +938,9 @@ class Discord {
* @returns {Promise} A promise that resolves when the channel has been created.
*/
static createVoiceChannel(name) {
return sixGuild.createChannel(name, "voice");
return sixGuild.createChannel(name, "voice").then((channel) => {
channel.edit({parent_id: voiceCategory.id}); // eslint-disable-line camelcase
});
}
// # ## # ## # # #

0 comments on commit e5ac7b1

Please sign in to comment.