muse/src/commands/shortcuts.ts

102 lines
3 KiB
TypeScript
Raw Normal View History

2020-03-17 01:37:54 +01:00
import {Message} from 'discord.js';
import {injectable} from 'inversify';
import {Shortcut, Settings} from '../models';
2020-03-17 23:59:26 +01:00
import errorMsg from '../utils/error-msg';
2020-03-17 01:37:54 +01:00
import Command from '.';
@injectable()
export default class implements Command {
public name = 'shortcuts';
2020-03-19 00:57:21 +01:00
public aliases = [];
2020-03-17 04:12:02 +01:00
public examples = [
2020-03-17 23:59:26 +01:00
['shortcuts', 'show all shortcuts'],
2020-03-17 04:12:02 +01:00
['shortcuts set s skip', 'aliases `s` to `skip`'],
['shortcuts set party play https://www.youtube.com/watch?v=zK6oOJ1wz8k', 'aliases `party` to a specific play command'],
['shortcuts delete party', 'removes the `party` shortcut']
];
2020-03-17 01:37:54 +01:00
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
2020-03-17 04:12:02 +01:00
const settings = await Settings.findOne({where: {guildId: msg.guild!.id}});
if (!settings) {
return;
}
const {prefix} = settings;
2020-03-17 01:37:54 +01:00
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) {
2020-03-17 23:59:26 +01:00
await msg.channel.send(errorMsg('you do\'nt have permission to do that'));
2020-03-17 01:37:54 +01:00
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) {
2020-03-17 23:59:26 +01:00
await msg.channel.send(errorMsg('shortcut doesn\'t exist'));
2020-03-17 01:37:54 +01:00
return;
}
// Check permissions
if (shortcut.authorId !== msg.author.id && msg.author.id !== msg.guild!.owner!.id) {
2020-03-17 23:59:26 +01:00
await msg.channel.send(errorMsg('you don\'t have permission to do that'));
2020-03-17 01:37:54 +01:00
return;
}
await shortcut.destroy();
await msg.channel.send('shortcut deleted');
break;
}
default: {
2020-03-17 23:59:26 +01:00
await msg.channel.send(errorMsg('unknown command'));
2020-03-17 01:37:54 +01:00
}
}
}
}
}