Skip to content

Commit

Permalink
Various bug fixes, the new FusionBot is now usable.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Sep 11, 2018
1 parent 2e6fc59 commit 021b245
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 80 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -38,6 +38,7 @@
"multiline-ternary": "off",
"new-cap": ["error", {"properties": false}],
"newline-per-chained-call": "off",
"no-await-in-loop": "off",
"no-confusing-arrow": "off",
"no-console": "off",
"no-continue": "off",
Expand Down
42 changes: 29 additions & 13 deletions commands.js
Expand Up @@ -421,16 +421,15 @@ class Commands {
throw new Error("No event currently running.");
}

const standings = Event.getStandings();
let str = "Standings:";

standings.forEach((index) => {
const player = standings[index];

str += `\n${index + 1}) ${player.name} - ${player.score} (${player.wins}-${player.losses})`;
});
let standings;
try {
standings = Event.getStandingsText();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was an error getting the standings.", err);
}

await Discord.queue(str, user);
await Discord.queue(standings, user);

return true;
}
Expand Down Expand Up @@ -632,7 +631,7 @@ class Commands {
throw new Error("Match is not yet reported.");
}

if (!match.reported.winner === user.id) {
if (match.reported.winner !== user.id) {
await Discord.queue(`Sorry, ${user}, but you can't confirm your own reports!`, channel);
throw new Error("Player tried to confirm their own report.");
}
Expand Down Expand Up @@ -690,7 +689,7 @@ class Commands {

Event.updateResult(match);

await Discord.queue(`${user}, your match comment has been successfully updated.`);
await Discord.queue(`${user}, your match comment has been successfully updated.`, channel);

return true;
}
Expand Down Expand Up @@ -940,9 +939,14 @@ class Commands {
await Discord.queue(`Round ${Event.round} starts now!`);

try {
matches.forEach(async (match) => {
let str = "Matches:";

for (const match of matches) {
await Event.createMatch(match[0], match[1]);
});
str += `\n**${Discord.getGuildUser(match[0]).displayName}** vs **${Discord.getGuildUser(match[1]).displayName}**`;
}

await Discord.queue(str);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem creating matches for the next round.`, channel);
throw err;
Expand Down Expand Up @@ -1060,6 +1064,8 @@ class Commands {
throw err;
}

await Discord.queue(`Additional match:\n**${player1.displayName}** vs **${player2.displayName}**`);

return true;
}

Expand Down Expand Up @@ -1228,6 +1234,16 @@ class Commands {
throw new Error("Event is not currently running.");
}

let standings;
try {
standings = Event.getStandingsText();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there is was an error ending the event.`, channel);
throw new Exception("There was an error getting the standings.", err);
}

await Discord.queue(standings, Discord.resultsChannel);

try {
await Event.endEvent();
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions database.js
Expand Up @@ -145,9 +145,9 @@ class Database {
* @return {Promise} A promise that resolves when the players' home levels have been locked.
*/
static async lockHomeLevelsForDiscordIds(discordIds) {
const players = discordIds.map((discordId, index) => ({index: `player${index}`, discordId}));
const players = discordIds.map((discordId, index) => ({index: `player${index}`, atIndex: `@player${index}`, discordId}));

await db.query(`UPDATE tblHome SET Locked = 1 WHERE DiscordID IN (${players.map((p) => p.index).join(", ")})`, players.reduce((accumulator, player) => {
await db.query(`UPDATE tblHome SET Locked = 1 WHERE DiscordID IN (${players.map((p) => p.atIndex).join(", ")})`, players.reduce((accumulator, player) => {
accumulator[player.index] = {type: Db.VARCHAR(50), value: player.discordId};
return accumulator;
}, {}));
Expand Down
121 changes: 100 additions & 21 deletions discord.js
Expand Up @@ -40,6 +40,8 @@ const DiscordJs = require("discord.js"),
let eventRole,
generalChannel,
obsGuild,
pilotsChatCategory,
pilotsVoiceChatCategory,
resultsChannel,
seasonRole;

Expand Down Expand Up @@ -94,7 +96,7 @@ class Discord {
// # ## ### ### ### ## ### ## # # # # # # # # ## ###
/**
* The results channel.
* @returns {Channel} The results channel.
* @returns {TextChannel} The results channel.
*/
static get resultsChannel() {
return resultsChannel;
Expand All @@ -115,6 +117,50 @@ class Discord {
return discord.id;
}

// # # ## # ### ##
// # # # # # # # #
// ### ## # ### # # # ### # # ## # ##
// # # # ## ### # # # # # # ### # # # # ##
// # # ## # # ## # # # # # # # # # ##
// ### ## # # # ### ### ## # # ## ### ##
/**
* The default role for the server.
* @returns {Role} The server's default role.
*/
static get defaultRole() {
return obsGuild.defaultRole;
}

// # ## # ## # # ## #
// # # # # # # # # #
// ### ## # ## ### ### # ### ### ### # ### ### ## ### ## ### # #
// # # # # # # # ## # # # # # # # # # # # ## # # # # # # # #
// # # # # # # # ## # # # # # ## # # # # ## # ## ## # # # # #
// ### ### ### ## ## ### ## # # # # ## ## # # ## ## # ## # #
// # ### #
/**
* Gets the pilots chat category.
* @returns {TextChannel} The pilots chat category.
*/
static get pilotsChatCategory() {
return pilotsChatCategory;
}

// # ## # # # # ## # # ## #
// # # # # # # # # # # #
// ### ## # ## ### ### # # ## ## ## ## # ### ### ### # ### ### ## ### ## ### # #
// # # # # # # # ## # # # # # # # ## # # # # # # # # # # # ## # # # # # # # #
// # # # # # # # ## ## # # # # ## # # # # # ## # # # # ## # ## ## # # # # #
// ### ### ### ## ## ### ## ## ### ## ## ## # # # # ## ## # # ## ## # ## # #
// # ### #
/**
* Gets the pilots voice chat category.
* @returns {VoiceChannel} The pilots voice chat category.
*/
static get pilotsVoiceChatCategory() {
return pilotsVoiceChatCategory;
}

// # #
// # #
// ### ### ### ### ### # # ###
Expand All @@ -141,12 +187,19 @@ class Discord {

eventRole = obsGuild.roles.find((r) => r.name === "In Current Event");
seasonRole = obsGuild.roles.find((r) => r.name === "Season 11 Participant");

pilotsChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Chat");
pilotsVoiceChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Voice Chat");
});

discord.on("disconnect", (ev) => {
Log.exception("Disconnected from Discord.", ev);
});

discord.on("error", (ev) => {
Log.exception("Unhandled error.", ev);
});

discord.addListener("message", (message) => {
if (message.guild && message.guild.name === "The Observatory" && message.channel.type === "text") {
Discord.message(message.author, message.content, message.channel);
Expand Down Expand Up @@ -238,7 +291,7 @@ class Discord {
* @param {Channel} [channel] The channel to send the message to.
* @returns {Promise} A promise that resolves when the message is sent.
*/
static queue(message, channel) {
static async queue(message, channel) {
if (!channel) {
channel = generalChannel;
}
Expand All @@ -252,21 +305,35 @@ class Discord {
}
};

if (JSON.stringify(msg).length > 1024) {
return channel.send(message);
}

return channel.send(
"",
{
embed: {
description: message,
timestamp: new Date(),
color: 0x263686,
footer: {icon_url: Discord.icon, text: "DescentBot"}
try {
if (JSON.stringify(msg).length > 1024) {
while (message.length > 0) {
await channel.send(message.substr(0, 2000));
if (message.length > 2000) {
message = message.substr(2000, message.length - 2000);
} else {
message = "";
}
}
return void 0;
}
);

return await channel.send(
"",
{
embed: {
description: message,
timestamp: new Date(),
color: 0x263686,
footer: {icon_url: Discord.icon, text: "DescentBot"}
}
}
);
} catch (err) {
console.log("Could not send queue.");
console.log(message);
return void 0;
}
}

// # # ##
Expand All @@ -282,12 +349,18 @@ class Discord {
* @param {Channel} [channel] The channel to send the message to.
* @returns {Promise} A promise that resolves when the message is sent.
*/
static richQueue(message, channel) {
static async richQueue(message, channel) {
if (!channel) {
channel = generalChannel;
}

return channel.send("", message);
try {
return await channel.send("", message);
} catch (err) {
console.log("Could not send rich queue.");
console.log(message);
return void 0;
}
}

// # ##
Expand Down Expand Up @@ -535,11 +608,14 @@ class Discord {
* Creates a text channel.
* @param {string} name The name of the channel to create.
* @param {Channel} category The category to assign the channel to.
* @returns {Promise} A promise that resolves when the channel has been created.
* @returns {Promise<TextChannel>} A promise that resolves with the text channel created.
*/
static async createTextChannel(name, category) {
const channel = await obsGuild.createChannel(name, "text");
return channel.edit({parent_id: category && category.id});
if (category) {
await channel.setParent(category);
}
return channel;
}

// # # # # ## # ##
Expand All @@ -552,11 +628,14 @@ class Discord {
* Creates a voice channel.
* @param {string} name The name of the channel to create.
* @param {Channel} category The category to assign the channel to.
* @returns {Promise} A promise that resolves when the channel has been created.
* @returns {Promise<VoiceChannel>} A promise that resolves with the voice channel created.
*/
static async createVoiceChannel(name, category) {
const channel = await obsGuild.createChannel(name, "voice");
channel.edit({parent_id: category && category.id});
if (category) {
await channel.setParent(category);
}
return channel;
}

// ## # ##
Expand Down

0 comments on commit 021b245

Please sign in to comment.