Permalink
Browse files

Remove TMI, fix bugs.

  • Loading branch information...
roncli committed Sep 10, 2018
1 parent c6d131e commit a07cee4d8336b00ddef6c867ef7a624b7aef587c
Showing with 192 additions and 442 deletions.
  1. +139 −247 commands.js
  2. +17 −20 discord.js
  3. +17 −1 event.js
  4. +1 −5 index.js
  5. +18 −52 log.js
  6. +0 −1 package.json
  7. +0 −116 tmi.js

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -127,18 +127,18 @@ class Discord {
* @returns {void}
*/
static startup() {
Discord.commands = new Commands(Discord);
Discord.commands = new Commands();
discord.addListener("ready", () => {
Log.log("Connected to Discord.");
obsGuild = discord.guilds.find("name", "The Observatory");
obsGuild = discord.guilds.find((g) => g.name === "The Observatory");
generalChannel = obsGuild.channels.find("name", "general");
resultsChannel = obsGuild.channels.find("name", "match-results");
generalChannel = obsGuild.channels.find((c) => c.name === "general");
resultsChannel = obsGuild.channels.find((c) => c.name === "match-results");
eventRole = obsGuild.roles.find("name", "In Current Event");
seasonRole = obsGuild.roles.find("name", "Season 11 Participant");
eventRole = obsGuild.roles.find((r) => r.name === "In Current Event");
seasonRole = obsGuild.roles.find((r) => r.name === "Season 11 Participant");
});
discord.on("disconnect", (ev) => {
@@ -186,7 +186,7 @@ class Discord {
* @returns {boolean} Whether the bot is connected to Discord.
*/
static isConnected() {
return discord ? discord.status === 0 : false;
return discord && obsGuild ? discord.status === 0 : false;
}
// # # ## ### ### ### ### ##
@@ -198,16 +198,17 @@ class Discord {
* Parses a message.
* @param {User} user The user who sent the message.
* @param {string} text The text of the message.
* @param {TextChannel} channel The channel the message was sent on.
* @returns {Promise} A promise that resolves when the message is parsed.
*/
static async message(user, text) {
static async message(user, text, channel) {
const matches = messageParse.exec(text);
if (matches) {
if (Object.getOwnPropertyNames(Commands.prototype).filter((p) => typeof Commands.prototype[p] === "function" && p !== "constructor").indexOf(matches[1]) !== -1) {
let success;
try {
success = await Discord.commands[matches[1]](user, matches[2]);
success = await Discord.commands[matches[1]](user, matches[2], channel);
} catch (err) {
if (err.innerError) {
Log.exception(err.message, err.innerError);
@@ -247,7 +248,7 @@ class Discord {
description: message,
timestamp: new Date(),
color: 0x263686,
footer: {icon_url: Discord.icon}
footer: {icon_url: Discord.icon, text: "DescentBot"}
}
}
);
@@ -302,7 +303,7 @@ class Discord {
* @returns {User} The Discord user.
*/
static findUserById(id) {
return discord.users.find("id", id);
return discord.users.find((u) => u.id === id);
}
// # ## # ## # # #
@@ -314,15 +315,11 @@ class Discord {
// ###
/**
* Returns the guild member by their user ID or their Discord user.
* @param {User|number|string} user The Discord user.
* @param {User|string} user The Discord user.
* @returns {GuildMember} The guild member.
*/
static getGuildUser(user) {
if (["number", "string"].indexOf(typeof user) !== -1) {
return obsGuild.member(discord.users.find("id", user));
}
return obsGuild.member(user);
return obsGuild.members.find((u) => u.id === user || u.id === user.id);
}
// # # # ## # ## ### # #
@@ -338,7 +335,7 @@ class Discord {
* @returns {Channel} The Discord channel.
*/
static findChannelByName(name) {
return obsGuild.channels.find("name", name);
return obsGuild.channels.find((c) => c.name === name);
}
// # # # ### ## ### # #
@@ -354,7 +351,7 @@ class Discord {
* @returns {Role} The Discord Role.
*/
static findRoleByName(name) {
return obsGuild.roles.find("name", name);
return obsGuild.roles.find((r) => r.name === name);
}
// # # # ### ## ### ### #
@@ -370,7 +367,7 @@ class Discord {
* @returns {Role} The Discord Role.
*/
static findRoleById(id) {
return obsGuild.roles.find("id", id);
return obsGuild.roles.find((r) => r.id === id);
}
// # # #### # ### ##
@@ -237,6 +237,21 @@ class Event {
return matches.filter((m) => !m.cancelled && m.players.indexOf(userId) !== -1 && m.winner);
}
// # ## ## ## # # # #
// # # # # # #### # #
// ### ## ### # # # # #### ### ### ## ### ## ###
// # # # ## # #### # # # # # # # # # # # ## ##
// ## ## # # # # # # # # ## # # # # ## ##
// # ## ## # # ### ### # # # # ## ## # # ## ###
// ###
/**
* Gets all non-cancelled matches.
* @returns {object[]} An array of match objects.
*/
static getAllMatches() {
return matches.filter((m) => !m.cancelled);
}
// # ### ## # #### # #
// # # # # # # # #
// ### ## ### # # ## ### # # # ### ### # # ### ## ###
@@ -561,7 +576,8 @@ class Event {
players: [player1.id, player2.id],
channel: textChannel,
voice: voiceChannel,
home: player1.id
home: player1.id,
round: round === 0 ? void 0 : round
};
matches.push(match);
@@ -1,6 +1,5 @@
const Discord = require("./discord"),
Log = require("./log"),
Tmi = require("./tmi");
Log = require("./log");
// # #
// # #
@@ -15,9 +14,6 @@ const Discord = require("./discord"),
(function startup() {
Log.log("Starting up...");
Tmi.startup();
Tmi.connect();
Discord.startup();
Discord.connect();
}());
70 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;
}
// ##
// #
// # ## ###
@@ -72,14 +47,14 @@ class Log {
// ###
/**
* Logs a warning.
* @param {*} obj The object to log.
* @param {string} message The string to log.
* @returns {void}
*/
static warning(obj) {
static warning(message) {
queue.push({
type: "warning",
date: new Date(),
obj
message
});
Log.output();
}
@@ -123,15 +98,15 @@ class Log {
Discord = require("./discord");
}
const logChannel = Discord.findChannelByName("fusionbot-log"),
errorChannel = Discord.findChannelByName("fusionbot-errors");
if (Discord.isConnected()) {
queue.forEach(async (log) => {
const logChannel = Discord.findChannelByName("fusionbot-log"),
errorChannel = Discord.findChannelByName("fusionbot-errors");
queue.forEach((log) => {
const message = {
embed: {
color: log.type === "log" ? 0x80FF80 : log.type === "warning" ? 0xFFFF00 : log.type === "exception" ? 0xFF0000 : 0x16F6F8,
footer: {icon_url: Discord.icon},
footer: {"icon_url": Discord.icon},
fields: [],
timestamp: log.date
}
@@ -142,27 +117,18 @@ class Log {
}
if (log.obj) {
switch (typeof log.obj) {
case "string":
message.embed.fields.push({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({value: `\`\`\`${JSON.stringify(log.obj, Log.replaceErrors)}\`\`\``});
}
break;
}
message.embed.fields.push({
name: "Message",
value: util.inspect(log.obj)
});
}
await Discord.richQueue(message, log.type === "exception" ? errorChannel : logChannel);
Discord.richQueue(message, log.type === "exception" ? errorChannel : logChannel);
});
queue.splice(0, queue.length);
} else {
console.log(queue[queue.length - 1]);
}
}
}
@@ -21,7 +21,6 @@
"discord.js": "11.4.2",
"glicko2": "0.8.4",
"node-database": "roncli/node-database#v1.0.2",
"tmi.js": "1.2.1",
"ws": "6.0.0"
},
"engines": {
Oops, something went wrong.

0 comments on commit a07cee4

Please sign in to comment.