aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2020-03-16 19:37:54 -0500
committerMax Isom <[email protected]>2020-03-16 19:37:54 -0500
commit32cb3ca4ae6a419f64e413ba5c8c543593a927b1 (patch)
tree18d4a04c579e2dcd2cade011badc2f48e3efcebf /src/commands
parent5eb1389a6f8121c5ad31d4c06b0c673998447ba0 (diff)
downloadmuse-32cb3ca4ae6a419f64e413ba5c8c543593a927b1.tar.xz
muse-32cb3ca4ae6a419f64e413ba5c8c543593a927b1.zip
Add custom shortcut support
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/shortcuts.ts88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/commands/shortcuts.ts b/src/commands/shortcuts.ts
new file mode 100644
index 0000000..2d62370
--- /dev/null
+++ b/src/commands/shortcuts.ts
@@ -0,0 +1,88 @@
+import {Message} from 'discord.js';
+import {injectable} from 'inversify';
+import {Shortcut, Settings} from '../models';
+import Command from '.';
+
+@injectable()
+export default class implements Command {
+ public name = 'shortcuts';
+ public description = 'edit shortcuts';
+
+ public async execute(msg: Message, args: string []): Promise<void> {
+ if (args.length === 0) {
+ // Get shortcuts for guild
+ const shortcuts = await Shortcut.findAll({where: {guildId: msg.guild!.id}});
+
+ if (shortcuts.length === 0) {
+ await msg.channel.send('no shortcuts exist');
+ return;
+ }
+
+ // Get prefix for guild
+ const {prefix} = await Settings.findOne({where: {guildId: msg.guild!.id}}) as Settings;
+
+ const res = shortcuts.reduce((accum, shortcut) => {
+ accum += `${prefix}${shortcut.shortcut}: ${shortcut.command}\n`;
+
+ return accum;
+ }, '');
+
+ await msg.channel.send(res);
+ } else {
+ const action = args[0];
+
+ const shortcutName = args[1];
+
+ switch (action) {
+ case 'set': {
+ const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}});
+
+ const command = args.slice(2).join(' ');
+
+ const newShortcut = {shortcut: shortcutName, command, guildId: msg.guild!.id, authorId: msg.author.id};
+
+ if (shortcut) {
+ if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.owner!.id) {
+ await msg.channel.send('error: you do not have permission to do that');
+ return;
+ }
+
+ await shortcut.update(newShortcut);
+ await msg.channel.send('shortcut updated');
+ } else {
+ await Shortcut.create(newShortcut);
+ await msg.channel.send('shortcut created');
+ }
+
+ break;
+ }
+
+ case 'delete': {
+ // Check if shortcut exists
+ const shortcut = await Shortcut.findOne({where: {guildId: msg.guild!.id, shortcut: shortcutName}});
+
+ if (!shortcut) {
+ await msg.channel.send('error: shortcut does not exist');
+ return;
+ }
+
+ // Check permissions
+ if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.owner!.id) {
+ await msg.channel.send('error: you do not have permission to do that');
+ return;
+ }
+
+ await shortcut.destroy();
+
+ await msg.channel.send('shortcut deleted');
+
+ break;
+ }
+
+ default: {
+ await msg.channel.send('error: unknown command');
+ }
+ }
+ }
+ }
+}