Permalink
Browse files

Version 0.3. Fix bugs, get the bot to work for qualifiers.

  • Loading branch information...
roncli committed Nov 10, 2018
1 parent 0bf1ee0 commit 947526823fc86dfd04bb3560fa90dd4715257d03
Showing with 225 additions and 47 deletions.
  1. +8 −17 commands.js
  2. +37 −3 database.js
  3. +84 −2 discord.js
  4. +94 −23 event.js
  5. +1 −1 index.js
  6. +1 −1 package.json
@@ -85,7 +85,7 @@ class Commands {
* @returns {Promise} A promise that resolves when the command completes.
*/
async simulate(user, message, channel) {
Commands.adminCheck(this, user);
Commands.adminCheck(user);
if (!idMessageParse.test(message)) {
return false;
@@ -593,6 +593,9 @@ class Commands {
let {1: score1, 2: score2} = reportParse.exec(message);
score1 = +score1;
score2 = +score2;
if (score1 < score2) {
[score1, score2] = [score2, score1];
}
@@ -743,7 +746,7 @@ class Commands {
let eventDate;
try {
eventDate = new Date(tz.Date(date, "America/Los_Angeles"));
eventDate = new Date(new tz.Date(date, "America/Los_Angeles"));
} catch (err) {
await Discord.queue(`Sorry, ${user}, but that is an invalid date and time.`, channel);
return new Error("Invalid date and time.");
@@ -754,20 +757,14 @@ class Commands {
return new Error("Date is in the past.");
}
// TODO - Remove Saturday requirement, open home changes.
if (eventDate.getDay() !== 6) {
await Discord.queue(`Sorry, ${user}, but that day is not a Saturday.`, channel);
return new Error("Day is not a Saturday.");
}
try {
await Event.openEvent(+season, event, eventDate);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem matching opening a new event.`, channel);
throw err;
}
await Discord.queue(`Hey @everyone, Season ${season} ${event} will begin on ${date.toLocaleString("en-us", {timeZone: "America/Los_Angeles", year: "numeric", month: "long", day: "numeric", hour12: true, hour: "numeric", minute: "2-digit", timeZoneName: "short"})}. If you'd like to play be sure you have set your home maps for the season by using the \`!home\` command, setting one map at a time, for example, \`!home Logic x2\`. Then \`!join\` the tournament!`);
await Discord.queue(`Hey @everyone, ${event} will begin on ${date.toLocaleString("en-us", {timeZone: "America/Los_Angeles", year: "numeric", month: "long", day: "numeric", hour12: true, hour: "numeric", minute: "2-digit", timeZoneName: "short"})}. If you'd like to play be sure you have set your home maps for the season by using the \`!home\` command, setting one map at a time, for example, \`!home Logic x2\`. Then \`!join\` the tournament!`);
return true;
}
@@ -1303,7 +1300,7 @@ class Commands {
let eventDate;
try {
eventDate = new Date(tz.Date(date, "America/Los_Angeles"));
eventDate = new Date(new tz.Date(date, "America/Los_Angeles"));
} catch (err) {
await Discord.queue(`Sorry, ${user}, but that is an invalid date and time.`, channel);
return new Error("Invalid date and time.");
@@ -1314,12 +1311,6 @@ class Commands {
return new Error("Date is in the past.");
}
// TODO - Remove Saturday requirement, adjusting warning accordingly, open home changes.
if (eventDate.getDay() !== 6) {
await Discord.queue(`Sorry, ${user}, but that day is not a Saturday.`, channel);
return new Error("Day is not a Saturday.");
}
let players;
try {
players = await Event.openFinals(+season, event, eventDate);
@@ -1328,7 +1319,7 @@ class Commands {
throw err;
}
await Discord.queue(`The Season ${season} ${event} will begin on ${date.toLocaleString("en-us", {timeZone: "America/Los_Angeles", year: "numeric", month: "long", day: "numeric", hour12: true, hour: "numeric", minute: "2-digit", timeZoneName: "short"})}. You will be notified if you have qualified for this event!`);
await Discord.queue(`${event} will begin on ${date.toLocaleString("en-us", {timeZone: "America/Los_Angeles", year: "numeric", month: "long", day: "numeric", hour12: true, hour: "numeric", minute: "2-digit", timeZoneName: "short"})}. You will be notified if you have qualified for this event!`);
for (const player of players) {
const playerUser = Discord.getGuildUser(player.discordId);
@@ -86,21 +86,22 @@ class Database {
* @param {object[]} matches The matches.
* @param {object[]} players The players.
* @param {boolean} finals Whether the event is a Finals Tournament.
* @param {boolean} warningSent Whether a warning was sent.
* @param {number} round The current round number.
* @param {string} eventName The name of the event.
* @param {Date} eventDate The date of the event.
* @param {number} eventId The ID of the event.
* @param {number} season The season of the event.
* @returns {Promise} A promise that resolves when the backup is complete.
*/
static async backup(matches, players, finals, round, eventName, eventDate, eventId, season) {
static async backup(matches, players, finals, warningSent, round, eventName, eventDate, eventId, season) {
await db.query(`
DELETE FROM tblBackup
INSERT INTO tblBackup (Code) VALUES (@code)
`, {
code: {
type: Db.TEXT,
value: JSON.stringify({matches, players, finals, round, eventName, eventDate, eventId, season}, (key, value) => {
value: JSON.stringify({matches, players, finals, warningSent, round, eventName, eventDate, eventId, season}, (key, value) => {
if (["channel", "voice", "results"].indexOf(key) !== -1) {
return value.id;
}
@@ -176,7 +177,7 @@ class Database {
// ### #
/**
* Gets the current backup.
* @returns {Promise<{matches: object[], players: object[], finals: boolean, round: number, eventName: string, eventDate: date, eventId: number, season: number}>} A promise that resolves with the current backup.
* @returns {Promise<{matches: object[], players: object[], finals: boolean, warningSent: boolean, round: number, eventName: string, eventDate: date, eventId: number, season: number}>} A promise that resolves with the current backup.
*/
static async getBackup() {
const data = await db.query("SELECT Code FROM tblBackup");
@@ -384,6 +385,39 @@ class Database {
}, {}));
}
// # ## # # #
// # # # # #
// ### ## ### # ## ### ### ## ### # # ## ### ### ## ### ###
// ## # ## # # # ## # # ## # # # # #### # # # # # # ## # # ##
// ## ## # # # ## # ## ## # # # # #### # # # # # ## # ##
// ### ## ## ## ## # # ### ## # # # # ### # # # # ## # ###
/**
* Sets the winner and runner up for the season.
* @param {number} season The season number.
* @param {string} winnerDiscordId The Discord ID of the season winner.
* @param {string} runnerUpDiscordId The Discord ID of the season runner-up.
* @returns {Promise} A promise that resolves when the season winners have been set.
*/
static async setSeasonWinners(season, winnerDiscordId, runnerUpDiscordId) {
await db.query("INSERT INTO tblSeason (Season, ChampionPlayerID, RunnerUpPlayerID) SELECT @season, w.PlayerID, r.PlayerID FROM tblPlayer w CROSS JOIN tblPlayer r WHERE w.DiscordID = @winnerDiscordId AND r.DiscordID = @runnerUpDiscordId", {season: {type: Db.INT, value: season}, winnerDiscordId: {type: Db.VARCHAR(50), value: winnerDiscordId}, runnerUpDiscordId: {type: Db.VARCHAR(50), value: runnerUpDiscordId}});
}
// # # #### # ### # #
// # # # # # # #
// # # ### ### ### ### ## ### # # ## ### ### # # ### ### ## ### ### ###
// # # # # # # # # # # ## # # # # ## # # # ### # # # # # # # # ##
// # # # # # # # ## # ## # # # ## # # # # # # ## # # # # ## ##
// ### ### ### # # ## ## #### # ## # # ## # # # # ## ### # # # ###
// # ###
/**
* Updates the ratings upon conclusion of an event.
* @param {number} eventId The event ID.
* @returns {Promise} A promise that resolves when the ratings are updated.
*/
static async updateEventRatings(eventId) {
await db.query("INSERT INTO tblRating (PlayerID, Rating, RatingDeviation, Volatility, EventID) SELECT PlayerID, Rating, RatingDeviation, Volatility, @eventId FROM tblPlayer", {eventId: {type: Db.INT, value: eventId}});
}
// # # ### ## ### # #
// # # # # # # # #
// # # ### ### ### ### ## # # # ### # # ## ### # # ### ### ## ### ###
@@ -5,6 +5,7 @@ const DiscordJs = require("discord.js"),
settings = require("./settings"),
discord = new DiscordJs.Client(settings.discord),
guildName = "The Observatory",
messageParse = /^!([^ ]+)(?: +(.*[^ ]))? *$/,
noPermissions = {
CREATE_INSTANT_INVITE: false,
@@ -38,6 +39,8 @@ const DiscordJs = require("discord.js"),
};
let alertsChannel,
archiveCategory,
chatCategory,
eventRole,
generalChannel,
obsGuild,
@@ -146,6 +149,36 @@ class Discord {
return obsGuild.defaultRole;
}
// # # ## #
// # # # #
// ### ### ## ### ## # # ## # ### ### ## ### ## ### # #
// # # # # # # # # # # # ## # # # # # ## # # # # # # # #
// # ## # # # # # # # ## # # # ## # ## ## # # # # #
// # # # ## # # ### # ## ## # # ## ## # ## # #
// ### #
/**
* Gets the archive category.
* @returns {TextChannel} The archive category.
*/
static get archiveCategory() {
return archiveCategory;
}
// # # ## #
// # # # # #
// ## ### ### ### # ### ### ## ### ## ### # #
// # # # # # # # # # # # ## # # # # # # # #
// # # # # ## # # # # ## # ## ## # # # # #
// ## # # # # ## ## # # ## ## # ## # #
// ### #
/**
* Gets the chat category.
* @returns {TextChannel} The chat category.
*/
static get chatCategory() {
return chatCategory;
}
// # ## # ## # # ## #
// # # # # # # # # #
// ### ## # ## ### ### # ### ### ### # ### ### ## ### ## ### # #
@@ -176,6 +209,20 @@ class Discord {
return pilotsVoiceChatCategory;
}
// ### ##
// # # #
// ### ## ### ### ## ### # # ## # ##
// ## # ## # # ## # # # # ### # # # # ##
// ## ## # ## ## # # # # # # # # # ##
// ### ## # # ### ## # # # # ## ### ##
/**
* Gets the current season participant role.
* @returns {Role} The season role.
*/
static get seasonRole() {
return seasonRole;
}
// # #
// # #
// ### ### ### ### ### # # ###
@@ -192,7 +239,7 @@ class Discord {
discord.addListener("ready", () => {
Log.log("Connected to Discord.");
obsGuild = discord.guilds.find((g) => g.name === "The Observatory");
obsGuild = discord.guilds.find((g) => g.name === guildName);
alertsChannel = obsGuild.channels.find((c) => c.name === "fusionbot-alerts");
generalChannel = obsGuild.channels.find((c) => c.name === "general");
@@ -201,6 +248,8 @@ class Discord {
eventRole = obsGuild.roles.find((r) => r.name === "In Current Event");
seasonRole = obsGuild.roles.find((r) => r.name === `Season ${season} Participant`);
archiveCategory = obsGuild.channels.find((c) => c.name === "Archive");
chatCategory = obsGuild.channels.find((c) => c.name === "Chat");
pilotsChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Chat");
pilotsVoiceChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Voice Chat");
@@ -218,7 +267,7 @@ class Discord {
});
discord.addListener("message", (message) => {
if (message.guild && message.guild.name === "The Observatory" && message.channel.type === "text") {
if (!message.guild || message.guild.name === guildName && message.channel.type === "text") {
Discord.message(message.author, message.content, message.channel);
}
});
@@ -349,6 +398,7 @@ class Discord {
} catch (err) {
console.log("Could not send queue.");
console.log(message);
console.log(err);
return void 0;
}
}
@@ -376,6 +426,7 @@ class Discord {
} catch (err) {
console.log("Could not send rich queue.");
console.log(message);
console.log(err);
return void 0;
}
}
@@ -551,6 +602,22 @@ class Discord {
return obsGuild.createRole(data);
}
// # ### ## ### # # # ## # #
// # # # # # # # # # # # #
// ### ## ### # # ## # ## # # ## ### ## ### ## ## ### # # # ### ## ###
// ## # ## # ### # # # # ## ### # # ## # # # # # # # #### ### # # ## # #
// ## ## # # # # # # ## # # # ## # # # # # # # # # # # ## #
// ### ## ## # # ## ### ## # ## ### ### ## ### ## # # # # # ## ## #
/**
* Sets the position of role1 to be after role 2.
* @param {Role} role1 The role to put after role2.
* @param {Role} role2 The role to put role1 after.
* @returns {Promise} A promise that resolves when the role has been placed.
*/
static setRolePositionAfter(role1, role2) {
role1.setPosition(role2.calculatedPosition - 1);
}
// # # # # ### ### ##
// # # # # # # # #
// ### ### ### # # ### ## ### # ## # # ## # ##
@@ -685,6 +752,21 @@ class Discord {
static removeChannel(channel) {
return channel.delete();
}
// # ## ### ##
// # # # # # #
// ### ## ### # ## ### ### ## ### # # ## # ##
// ## # ## # # # ## # # ## # # # # ### # # # # ##
// ## ## # # # ## # ## ## # # # # # # # # # ##
// ### ## ## ## ## # # ### ## # # # # ## ### ##
/**
* Sets the season role.
* @param {Role} role The role to set as the season role.
* @returns {void}
*/
static setSeasonRole(role) {
seasonRole = role;
}
}
module.exports = Discord;
Oops, something went wrong.

0 comments on commit 9475268

Please sign in to comment.