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

Commit

Permalink
Continue work on ES7 update.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Nov 20, 2018
1 parent da46536 commit e6b6474
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 109 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -41,6 +41,7 @@
{"properties": false} {"properties": false}
], ],
"newline-per-chained-call": "off", "newline-per-chained-call": "off",
"no-await-in-loop": "off",
"no-confusing-arrow": "off", "no-confusing-arrow": "off",
"no-console": "off", "no-console": "off",
"no-continue": "off", "no-continue": "off",
Expand Down
7 changes: 5 additions & 2 deletions commands.js
Expand Up @@ -546,8 +546,11 @@ class Commands {
Discord.createTextChannel(`twitch-${streamer}`).then((channel) => { Discord.createTextChannel(`twitch-${streamer}`).then((channel) => {
channel.setTopic(`This channel is for ${user}'s Twitch stream. Follow ${user} on Twitch at http://twitch.tv/${streamer}.`).then(() => { channel.setTopic(`This channel is for ${user}'s Twitch stream. Follow ${user} on Twitch at http://twitch.tv/${streamer}.`).then(() => {
channel.setPosition(9999).then(() => { channel.setPosition(9999).then(() => {
Discord.sortDiscordChannels(); Discord.sortDiscordChannels().then(() => {
resolve(true); resolve(true);
}).catch((err) => {
reject(new Exception("There was a Discord error while sorting channels.", err));
});
}).catch((err) => { }).catch((err) => {
reject(new Exception("There was a Discord error while setting the position of the channel.", err)); reject(new Exception("There was a Discord error while setting the position of the channel.", err));
}); });
Expand Down
130 changes: 75 additions & 55 deletions discord.js
Expand Up @@ -2,10 +2,12 @@ const DiscordJs = require("discord.js"),


Commands = require("./commands"), Commands = require("./commands"),
Db = require("./database"), Db = require("./database"),
Exception = require("./exception"),
Log = require("./log"), Log = require("./log"),
settings = require("./settings"), settings = require("./settings"),
Tmi = require("./tmi"), Tmi = require("./tmi"),
Twitch = require("./twitch"), Twitch = require("./twitch"),
Warning = require("./warning"),


channelDeletionTimeouts = {}, channelDeletionTimeouts = {},
discord = new DiscordJs.Client(settings.discord), discord = new DiscordJs.Client(settings.discord),
Expand Down Expand Up @@ -156,9 +158,9 @@ class Discord {
} }
}); });


discord.on("message", (message) => { discord.on("message", async (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); await Discord.message(message.author, message.content);
} }
}); });


Expand Down Expand Up @@ -262,25 +264,36 @@ class Discord {
/** /**
* Parses a message. * Parses a message.
* @param {User} user The user who sent the message. * @param {User} user The user who sent the message.
* @param {string} text The text of the message. * @param {string} message The text of the message.
* @returns {void} * @returns {Promise} A promise that resolves when the message is parsed.
*/ */
static message(user, text) { static async message(user, message) {
const matches = messageParse.exec(text); for (const text of message.split("\n")) {
if (messageParse.test(text)) {
const matches = messageParse.exec(text),
command = matches[1].toLocaleLowerCase(),
args = matches[2];

if (Object.getOwnPropertyNames(Commands.prototype).filter((p) => typeof Commands.prototype[p] === "function" && p !== "constructor").indexOf(command) !== -1) {
let success;
try {
await Discord.commands[command](user, args);
} catch (err) {
if (err instanceof Warning) {
Log.warning(`${user}: ${text}\n${err}`);
} else if (err instanceof Exception) {
Log.exception(err.message, err.innerError);
} else {
Log.exception("Unhandled error found.", err);
}

return;
}


if (matches) {
if (Object.getOwnPropertyNames(Commands.prototype).filter((p) => typeof Commands.prototype[p] === "function" && p !== "constructor").indexOf(matches[1]) !== -1) {
Discord.commands[matches[1]](user, matches[2]).then((success) => {
if (success) { if (success) {
Log.log(`${user}: ${text}`); Log.log(`${user}: ${text}`);
} }
}).catch((err) => { }
if (err.innerError) {
Log.exception(err.message, err.innerError);
} else {
Log.warning(err);
}
});
} }
} }
} }
Expand Down Expand Up @@ -711,8 +724,12 @@ class Discord {
* @returns {void} * @returns {void}
*/ */
static markEmptyVoiceChannel(channel) { static markEmptyVoiceChannel(channel) {
channelDeletionTimeouts[channel.id] = setTimeout(() => { channelDeletionTimeouts[channel.id] = setTimeout(async () => {
channel.delete(); try {
await channel.delete();
} catch (err) {
Log.exception(`Couldn't delete empty voice channel ${channel}.`, err);
}
delete channelDeletionTimeouts[channel.id]; delete channelDeletionTimeouts[channel.id];
}, 300000); }, 300000);
} }
Expand All @@ -725,24 +742,27 @@ class Discord {
// ### ## # ## ### ### ### ## ## # ### ## # # # # # # # # ## ### ### // ### ## # ## ### ### ### ## ## # ### ## # # # # # # # # ## ### ###
/** /**
* Sorts Discord channels. * Sorts Discord channels.
* @returns {void} * @returns {Promise} A promise that resolves when the Discord channels are sorted.
*/ */
static sortDiscordChannels() { static async sortDiscordChannels() {
const channels = Array.from(sixGuild.channels.filter((channel) => channel.name.startsWith("twitch-")).values()).sort((a, b) => a.name.localeCompare(b.name)), const channels = Array.from(sixGuild.channels.filter((channel) => channel.name.startsWith("twitch-")).values()).sort((a, b) => a.name.localeCompare(b.name)),
positionChannel = (index) => { positionChannel = async (index) => {
const channel = sixGuild.channels.get(channels[index].id); const channel = sixGuild.channels.get(channels[index].id);


index++; index++;
channel.edit({position: index}).then(() => { try {
if (index < channels.length) { await channel.edit({position: index});
positionChannel(index); } catch (err) {
}
}).catch((err) => {
Log.exception("Problem repositioning channels.", err); Log.exception("Problem repositioning channels.", err);
}); return;
}

if (index < channels.length) {
positionChannel(index);
}
}; };


positionChannel(0); await positionChannel(0);
} }


// # # # # # # ## # ## // # # # # # # ## # ##
Expand Down Expand Up @@ -821,7 +841,7 @@ class Discord {
* @returns {void} * @returns {void}
*/ */
static addStreamer(name) { static addStreamer(name) {
streamers.push(name.toLowerCase()); streamers.push(name.toLocaleLowerCase());
} }


// ## # // ## #
Expand Down Expand Up @@ -855,7 +875,7 @@ class Discord {
* @returns {void} * @returns {void}
*/ */
static addHost(name) { static addHost(name) {
hosts.push(name.toLowerCase()); hosts.push(name.toLocaleLowerCase());
} }


// # # # // # # #
Expand Down Expand Up @@ -888,8 +908,8 @@ class Discord {
* @param {User} user The user to add to the role. * @param {User} user The user to add to the role.
* @returns {Promise} A promise that resolves when the user has been added to the role. * @returns {Promise} A promise that resolves when the user has been added to the role.
*/ */
static addStreamersRole(user) { static async addStreamersRole(user) {
return sixGuild.member(user).addRole(streamersRole); await sixGuild.member(user).addRole(streamersRole);
} }


// ## # ### ## // ## # ### ##
Expand All @@ -903,8 +923,8 @@ class Discord {
* @param {User} user The user to remove from the role. * @param {User} user The user to remove from the role.
* @returns {Promise} A promise that resovles when the user has been removed from the role. * @returns {Promise} A promise that resovles when the user has been removed from the role.
*/ */
static removeStreamersRole(user) { static async removeStreamersRole(user) {
return sixGuild.member(user).removeRole(streamersRole); await sixGuild.member(user).removeRole(streamersRole);
} }


// # # ## # # # # # # ### ## // # # ## # # # # # # ### ##
Expand All @@ -919,8 +939,8 @@ class Discord {
* @param {User} user The user to add to the role. * @param {User} user The user to add to the role.
* @returns {Promise} A promise that resolves when the user has been added to the role. * @returns {Promise} A promise that resolves when the user has been added to the role.
*/ */
static addStreamNotifyRole(user) { static async addStreamNotifyRole(user) {
return sixGuild.member(user).addRole(streamNotifyRole); await sixGuild.member(user).addRole(streamNotifyRole);
} }


// ## # # # # # # ### ## // ## # # # # # # ### ##
Expand All @@ -935,8 +955,8 @@ class Discord {
* @param {User} user The user to remove from the role. * @param {User} user The user to remove from the role.
* @returns {Promise} A promise that resolves when the user has been removed from the role. * @returns {Promise} A promise that resolves when the user has been removed from the role.
*/ */
static removeStreamNotifyRole(user) { static async removeStreamNotifyRole(user) {
return sixGuild.member(user).removeRole(streamNotifyRole); await sixGuild.member(user).removeRole(streamNotifyRole);
} }


// # ### ## // # ### ##
Expand Down Expand Up @@ -966,8 +986,8 @@ class Discord {
* @param {Role} role The role to add the user to. * @param {Role} role The role to add the user to.
* @returns {Promise} A promise that resolves when the user has been added to the role. * @returns {Promise} A promise that resolves when the user has been added to the role.
*/ */
static addUserToRole(user, role) { static async addUserToRole(user, role) {
return sixGuild.member(user).addRole(role); await sixGuild.member(user).addRole(role);
} }


// # # #### ### ## // # # #### ### ##
Expand All @@ -982,8 +1002,8 @@ class Discord {
* @param {Role} role The role to remove the user to. * @param {Role} role The role to remove the user to.
* @returns {Promise} A promise that resolves when the user has been removed from the role. * @returns {Promise} A promise that resolves when the user has been removed from the role.
*/ */
static removeUserFromRole(user, role) { static async removeUserFromRole(user, role) {
return sixGuild.member(user).removeRole(role); await sixGuild.member(user).removeRole(role);
} }


// # ### # ## # ## // # ### # ## # ##
Expand All @@ -995,14 +1015,14 @@ class Discord {
/** /**
* Creates a text channel. * Creates a text channel.
* @param {string} name The name of the channel to create. * @param {string} name The name of the channel to create.
* @returns {Promise} A promise that resolves when the channel has been created. * @returns {Promise<TextChannel>} A promise that resolves with the created channel.
*/ */
static createTextChannel(name) { static async createTextChannel(name) {
return sixGuild.createChannel(name, "text").then((channel) => { const channel = await sixGuild.createChannel(name, "text");
channel.setParent(streamersCategory);


return channel; await channel.setParent(streamersCategory);
});
return channel;
} }


// # # # # ## # ## // # # # # ## # ##
Expand All @@ -1014,15 +1034,15 @@ class Discord {
/** /**
* Creates a voice channel. * Creates a voice channel.
* @param {string} name The name of the channel to create. * @param {string} name The name of the channel to create.
* @returns {Promise} A promise that resolves when the channel has been created. * @returns {Promise<VoiceChannel>} A promise that resolves with the created channel.
*/ */
static createVoiceChannel(name) { static async createVoiceChannel(name) {
return sixGuild.createChannel(name, "voice").then((channel) => { const channel = await sixGuild.createChannel(name, "voice");
channel.edit({bitrate: 64000});
channel.setParent(voiceCategory);


return channel; await channel.edit({bitrate: 64000});
}); await channel.setParent(voiceCategory);

return channel;
} }


// # ## # ## # # # // # ## # ## # # #
Expand Down
36 changes: 20 additions & 16 deletions index.js
Expand Up @@ -13,7 +13,7 @@ const Db = require("./database"),
/** /**
* Starts up the application. * Starts up the application.
*/ */
(function startup() { (async function startup() {
Log.log("Starting up..."); Log.log("Starting up...");


// Set the window title. // Set the window title.
Expand All @@ -24,24 +24,28 @@ const Db = require("./database"),
} }


// Get streamers and hosted channels. // Get streamers and hosted channels.
Db.getStreamersAndHosts().then((data) => { let data;
Log.log("Got streamer data."); try {
data = await Db.getStreamersAndHosts();
} catch (err) {
setTimeout(startup, 60000);
Log.exception("There was a database error getting streamers and hosted channels. Retrying in 60 seconds...", err);
return;
}


// Startup tmi Log.log("Got streamer data.");
Tmi.startup();
Tmi.connect();


// Startup Discord // Startup tmi
Discord.startup(); Tmi.startup();
Discord.connect(); await Tmi.connect();


// Add streamers and hosts. // Startup Discord
data.streamers.forEach((streamer) => Discord.addStreamer(streamer)); Discord.startup();
data.hosts.forEach((host) => Discord.addHost(host)); await Discord.connect();
}).catch((err) => {
setTimeout(startup, 60000); // Add streamers and hosts.
Log.exception("There was a database error getting streamers and hosted channels.", err); data.streamers.forEach((streamer) => Discord.addStreamer(streamer));
}); data.hosts.forEach((host) => Discord.addHost(host));
}()); }());


process.on("unhandledRejection", (err) => { process.on("unhandledRejection", (err) => {
Expand Down

0 comments on commit e6b6474

Please sign in to comment.