From 7c861539085e4c540938ad023631d024bb786d14 Mon Sep 17 00:00:00 2001 From: Sayak Mukhopadhyay Date: Sat, 6 Feb 2021 20:47:01 +0530 Subject: [PATCH] Added command aliasing and data dm commands --- src/modules/discord/client.ts | 53 +++++++++++++++---- src/modules/discord/commands/adminRoles.ts | 12 +++++ src/modules/discord/commands/bgsChannel.ts | 12 +++++ src/modules/discord/commands/bgsReport.ts | 25 ++++++++- src/modules/discord/commands/bgsRole.ts | 12 +++++ src/modules/discord/commands/chart.ts | 11 +++- src/modules/discord/commands/factionStatus.ts | 19 ++++++- .../discord/commands/forbiddenRoles.ts | 12 +++++ .../discord/commands/monitorFactions.ts | 14 +++++ .../discord/commands/monitorSystems.ts | 14 +++++ src/modules/discord/commands/myGuild.ts | 10 ++++ src/modules/discord/commands/systemStatus.ts | 19 ++++++- src/modules/discord/commands/tick.ts | 11 +++- 13 files changed, 204 insertions(+), 20 deletions(-) diff --git a/src/modules/discord/client.ts b/src/modules/discord/client.ts index e89ca53..e7958c3 100644 --- a/src/modules/discord/client.ts +++ b/src/modules/discord/client.ts @@ -120,22 +120,55 @@ export class DiscordClient { } private initiateCommands(): void { + let myGuild = new MyGuild(); + let bgsRole = new BGSRole(); + let adminRoles = new AdminRoles(); + let forbiddenRoles = new ForbiddenRoles(); + let bgaChannel = new BGSChannel(); + let monitorSystems = new MonitorSystems(); + let monitorFactions = new MonitorFactions(); + let systemStatus = new SystemStatus(); + let systemStatusDm = new SystemStatus(true); + let factionStatus = new FactionStatus(); + let factionStatusDm = new FactionStatus(true); + let bgsReport = new BGSReport(); + let bgsReportDm = new BGSReport(true); + this.commandsMap.set("hi", new Hi()); this.commandsMap.set("help", new Help()); - this.commandsMap.set("myguild", new MyGuild()); - this.commandsMap.set("bgsrole", new BGSRole()); - this.commandsMap.set("adminroles", new AdminRoles()); - this.commandsMap.set("forbiddenroles", new ForbiddenRoles()); - this.commandsMap.set("bgschannel", new BGSChannel()); - this.commandsMap.set("monitorsystems", new MonitorSystems()); - this.commandsMap.set("monitorfactions", new MonitorFactions()); - this.commandsMap.set("systemstatus", new SystemStatus()); - this.commandsMap.set("factionstatus", new FactionStatus()); - this.commandsMap.set("bgsreport", new BGSReport()); + this.commandsMap.set("myguild", myGuild); + this.commandsMap.set("mgd", myGuild); + this.commandsMap.set("bgsrole", bgsRole); + this.commandsMap.set("brl", bgsRole); + this.commandsMap.set("adminroles", adminRoles); + this.commandsMap.set("arl", adminRoles); + this.commandsMap.set("forbiddenroles", forbiddenRoles); + this.commandsMap.set("frl", forbiddenRoles); + this.commandsMap.set("bgschannel", bgaChannel); + this.commandsMap.set("bcl", bgaChannel); + this.commandsMap.set("monitorsystems", monitorSystems); + this.commandsMap.set("ms", monitorSystems); + this.commandsMap.set("monitorfactions", monitorFactions); + this.commandsMap.set("mf", monitorFactions); + this.commandsMap.set("systemstatus", systemStatus); + this.commandsMap.set("ss", systemStatus); + this.commandsMap.set("systemstatusdm", systemStatusDm); + this.commandsMap.set("ssdm", systemStatusDm); + this.commandsMap.set("factionstatus", factionStatus); + this.commandsMap.set("fs", factionStatus); + this.commandsMap.set("factionstatusdm", factionStatusDm); + this.commandsMap.set("fsdm", factionStatusDm); + this.commandsMap.set("bgsreport", bgsReport); + this.commandsMap.set("bgsreport", bgsReport); + this.commandsMap.set("brt", bgsReport); + this.commandsMap.set("bgsreportdm", bgsReportDm); + this.commandsMap.set("brtdm", bgsReportDm); this.commandsMap.set("sort", new Sort()); this.commandsMap.set("chart", new Chart()); + this.commandsMap.set("chartdm", new Chart(true)); this.commandsMap.set("theme", new Theme()); this.commandsMap.set("tick", new Tick()); + this.commandsMap.set("tickdm", new Tick(true)); } private initiateCustom(): void { diff --git a/src/modules/discord/commands/adminRoles.ts b/src/modules/discord/commands/adminRoles.ts index 68e5ad5..7ecbe90 100644 --- a/src/modules/discord/commands/adminRoles.ts +++ b/src/modules/discord/commands/adminRoles.ts @@ -35,6 +35,7 @@ export class AdminRoles { try { if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -48,6 +49,17 @@ export class AdminRoles { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'a': + return 'add'; + case 'r': + return 'remove'; + case 'l': + return 'list'; + } + } + async add(message: Message, argsArray: string[]) { // Only the server admins can set the admin roles let member = message.guild.member(message.author); diff --git a/src/modules/discord/commands/bgsChannel.ts b/src/modules/discord/commands/bgsChannel.ts index 43be3ab..1ddf82f 100644 --- a/src/modules/discord/commands/bgsChannel.ts +++ b/src/modules/discord/commands/bgsChannel.ts @@ -35,6 +35,7 @@ export class BGSChannel { try { if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -48,6 +49,17 @@ export class BGSChannel { } } + checkAndMapAlias(command: string) { + switch (command) { + case 's': + return 'set'; + case 'r': + return 'remove'; + case 'sh': + return 'show'; + } + } + async set(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.FORBIDDEN]); diff --git a/src/modules/discord/commands/bgsReport.ts b/src/modules/discord/commands/bgsReport.ts index eb9dc18..b592dc7 100644 --- a/src/modules/discord/commands/bgsReport.ts +++ b/src/modules/discord/commands/bgsReport.ts @@ -30,10 +30,12 @@ import { Tick } from './tick'; export class BGSReport { db: DB; tickTime: string; + dm: boolean; - constructor() { + constructor(dm = false) { this.db = App.db; this.tickTime = ""; + this.dm = dm; } exec(message: Message, commandArguments: string): void { @@ -43,6 +45,7 @@ export class BGSReport { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -53,6 +56,19 @@ export class BGSReport { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'g': + return 'get'; + case 'st': + return 'settime'; + case 'sh': + return 'showtime'; + case 'u': + return 'unsettime'; + } + } + async get(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.BGS, Access.FORBIDDEN]); @@ -63,7 +79,12 @@ export class BGSReport { if (message.guild.me.permissionsIn(message.channel).has([flags.EMBED_LINKS])) { let embedArray = await this.getBGSReportEmbed(guildId, message.channel as TextChannel); for (let index = 0; index < embedArray.length; index++) { - await message.channel.send(embedArray[index]); + if (this.dm) { + message.channel.send("I have DM'd the result to you"); + message.member.send(embedArray[index]); + } else { + message.channel.send(embedArray[index]); + } } } else { try { diff --git a/src/modules/discord/commands/bgsRole.ts b/src/modules/discord/commands/bgsRole.ts index d3cb410..7e49ce4 100644 --- a/src/modules/discord/commands/bgsRole.ts +++ b/src/modules/discord/commands/bgsRole.ts @@ -34,6 +34,7 @@ export class BGSRole { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -44,6 +45,17 @@ export class BGSRole { } } + checkAndMapAlias(command: string) { + switch (command) { + case 's': + return 'set'; + case 'r': + return 'remove'; + case 'sh': + return 'show'; + } + } + async set(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.FORBIDDEN]); diff --git a/src/modules/discord/commands/chart.ts b/src/modules/discord/commands/chart.ts index aa207d9..7e5328d 100644 --- a/src/modules/discord/commands/chart.ts +++ b/src/modules/discord/commands/chart.ts @@ -25,9 +25,11 @@ import { Access } from '../access'; export class Chart { db: DB; + dm: boolean; - constructor() { + constructor(dm = false) { this.db = App.db; + this.dm = dm; } exec(message: Message, commandArguments: string): void { @@ -86,7 +88,12 @@ export class Chart { let response: FullResponse = await request.get(requestOptions); if (response.statusCode === 200) { let attachment = new MessageAttachment(response.body as Buffer, contentDisposition.parse(response.headers['content-disposition']).parameters.filename); - message.channel.send(attachment); + if (this.dm) { + message.channel.send("I have DM'd the result to you"); + message.member.send(attachment); + } else { + message.channel.send(attachment); + } } else { App.bugsnagClient.call(response.statusMessage, { metaData: { diff --git a/src/modules/discord/commands/factionStatus.ts b/src/modules/discord/commands/factionStatus.ts index bab8406..473e42d 100644 --- a/src/modules/discord/commands/factionStatus.ts +++ b/src/modules/discord/commands/factionStatus.ts @@ -30,10 +30,12 @@ import { Tick } from './tick'; export class FactionStatus { db: DB; tickTime: string; + dm: boolean; - constructor() { + constructor(dm = false) { this.db = App.db; this.tickTime = ""; + this.dm = dm; } exec(message: Message, commandArguments: string): void { @@ -43,6 +45,7 @@ export class FactionStatus { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -53,6 +56,13 @@ export class FactionStatus { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'g': + return 'get'; + } + } + async get(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.BGS, Access.FORBIDDEN]); @@ -224,7 +234,12 @@ export class FactionStatus { embed.addField(fieldRecord[recordIndex].fieldTitle, fieldRecord[recordIndex].fieldDescription); } try { - message.channel.send(embed); + if (this.dm) { + message.channel.send("I have DM'd the result to you"); + message.member.send(embed); + } else { + message.channel.send(embed); + } } catch (err) { App.bugsnagClient.call(err, { metaData: { diff --git a/src/modules/discord/commands/forbiddenRoles.ts b/src/modules/discord/commands/forbiddenRoles.ts index 5a2a305..71a6464 100644 --- a/src/modules/discord/commands/forbiddenRoles.ts +++ b/src/modules/discord/commands/forbiddenRoles.ts @@ -34,6 +34,7 @@ export class ForbiddenRoles { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -44,6 +45,17 @@ export class ForbiddenRoles { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'a': + return 'add'; + case 'r': + return 'remove'; + case 'l': + return 'list'; + } + } + async add(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.FORBIDDEN], true); diff --git a/src/modules/discord/commands/monitorFactions.ts b/src/modules/discord/commands/monitorFactions.ts index da58313..a24e6a5 100644 --- a/src/modules/discord/commands/monitorFactions.ts +++ b/src/modules/discord/commands/monitorFactions.ts @@ -37,6 +37,7 @@ export class MonitorFactions { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -47,6 +48,19 @@ export class MonitorFactions { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'a': + return 'add'; + case 'ap': + return 'addprimary'; + case 'r': + return 'remove'; + case 'l': + return 'list'; + } + } + async add(message: Message, argsArray: string[], primary: boolean = false) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.BGS, Access.FORBIDDEN]); diff --git a/src/modules/discord/commands/monitorSystems.ts b/src/modules/discord/commands/monitorSystems.ts index cae7bfd..4c9c53f 100644 --- a/src/modules/discord/commands/monitorSystems.ts +++ b/src/modules/discord/commands/monitorSystems.ts @@ -37,6 +37,7 @@ export class MonitorSystems { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -47,6 +48,19 @@ export class MonitorSystems { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'a': + return 'add'; + case 'ap': + return 'addprimary'; + case 'r': + return 'remove'; + case 'l': + return 'list'; + } + } + async add(message: Message, argsArray: string[], primary: boolean = false) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.BGS, Access.FORBIDDEN]); diff --git a/src/modules/discord/commands/myGuild.ts b/src/modules/discord/commands/myGuild.ts index 28f7f50..58aebf4 100644 --- a/src/modules/discord/commands/myGuild.ts +++ b/src/modules/discord/commands/myGuild.ts @@ -34,6 +34,7 @@ export class MyGuild { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -44,6 +45,15 @@ export class MyGuild { } } + checkAndMapAlias(command: string) { + switch (command) { + case 's': + return 'set'; + case 'r': + return 'remove'; + } + } + async set(message: Message, argsArray: string[]) { // Only the server admins can set the guild let member = await message.guild.member(message.author) diff --git a/src/modules/discord/commands/systemStatus.ts b/src/modules/discord/commands/systemStatus.ts index cfe123a..dd469fb 100644 --- a/src/modules/discord/commands/systemStatus.ts +++ b/src/modules/discord/commands/systemStatus.ts @@ -29,10 +29,12 @@ import { Tick } from './tick'; export class SystemStatus { db: DB; tickTime: string; + dm: boolean; - constructor() { + constructor(dm = false) { this.db = App.db; this.tickTime = ""; + this.dm = dm; } exec(message: Message, commandArguments: string): void { @@ -42,6 +44,7 @@ export class SystemStatus { } if (argsArray.length > 0) { let command = argsArray[0].toLowerCase(); + command = this.checkAndMapAlias(command); if (this[command]) { this[command](message, argsArray); } else { @@ -52,6 +55,13 @@ export class SystemStatus { } } + checkAndMapAlias(command: string) { + switch (command) { + case 'g': + return 'get'; + } + } + async get(message: Message, argsArray: string[]) { try { await Access.has(message.author, message.guild, [Access.ADMIN, Access.BGS, Access.FORBIDDEN]); @@ -233,7 +243,12 @@ export class SystemStatus { embed.addField(fieldRecord[recordIndex].fieldTitle, fieldRecord[recordIndex].fieldDescription); } try { - message.channel.send(embed); + if (this.dm) { + message.channel.send("I have DM'd the result to you"); + message.member.send(embed); + } else { + message.channel.send(embed); + } } catch (err) { App.bugsnagClient.call(err, { metaData: { diff --git a/src/modules/discord/commands/tick.ts b/src/modules/discord/commands/tick.ts index 8e26efe..b3955f2 100644 --- a/src/modules/discord/commands/tick.ts +++ b/src/modules/discord/commands/tick.ts @@ -26,9 +26,11 @@ import { TickSchema, TickType } from '../../../interfaces/typings'; export class Tick { db: DB; + dm: boolean; - constructor() { + constructor(dm = false) { this.db = App.db; + this.dm = dm; } exec(message: Message, commandArguments: string): void { @@ -64,7 +66,12 @@ export class Tick { embed.addField("Last Tick", lastTickFormattedTime + ' UTC - ' + lastTickFormattedDate); embed.setTimestamp(new Date(lastTick.time)); try { - message.channel.send(embed); + if (this.dm) { + message.channel.send("I have DM'd the result to you"); + message.member.send(embed); + } else { + message.channel.send(embed); + } } catch (err) { App.bugsnagClient.call(err); }