diff --git a/src/commands/addredirect.ts b/src/commands/addredirect.ts new file mode 100644 index 0000000..79aa97f --- /dev/null +++ b/src/commands/addredirect.ts @@ -0,0 +1,38 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class AddRedirect extends Command { + constructor(client: Client) { + super(client); + this.name = 'addredirect'; + this.description = 'Adds a redirect link for \'loc.sh\''; + this.usage = 'addredirect '; + this.aliases = ['ar']; + this.permissions = 6; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + const check = await this.client.db.redirect.findOne({ key: args[1].toLowerCase() }); + if (check) return this.error(message.channel, `Redirect key ${args[1].toLowerCase()} already exists. Linked to: ${check.to}`); + try { + const test = new URL(args[0].toLowerCase()); + if (test.protocol !== 'https:') return this.error(message.channel, 'Protocol must be HTTPS.'); + } catch { + return this.error(message.channel, 'This doesn\'t appear to be a valid URL.'); + } + if ((/^[a-zA-Z0-9]+$/gi.test(args[1].toLowerCase()) === false) || args[1].toLowerCase().length > 15) return this.error(message.channel, 'Invalid key. The key must be alphanumeric and less than 16 characters.'); + // eslint-disable-next-line new-cap + const redirect = new this.client.db.redirect({ + key: args[1].toLowerCase(), + to: args[0].toLowerCase(), + }); + await redirect.save(); + return this.success(message.channel, `Redirect https://loc.sh/${args[1].toLowerCase()} -> ${args[0].toLowerCase()} is now active.`); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/delredirect.ts b/src/commands/delredirect.ts new file mode 100644 index 0000000..7c864f7 --- /dev/null +++ b/src/commands/delredirect.ts @@ -0,0 +1,26 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class DelRedirect extends Command { + constructor(client: Client) { + super(client); + this.name = 'delredirect'; + this.description = 'Delete a redirect link for \'loc.sh\''; + this.usage = 'delredirect '; + this.aliases = ['dr']; + this.permissions = 6; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, [this.name]); + const check = await this.client.db.redirect.findOne({ key: args[0].toLowerCase() }); + if (!check) return this.error(message.channel, `Redirect key ${args[0].toLowerCase()} doesn't exist.`); + await this.client.db.redirect.deleteOne({ key: args[0].toLowerCase() }); + return this.success(message.channel, `Deleted redirect https://loc.sh/${args[0].toLowerCase()}.`); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/listredirects.ts b/src/commands/listredirects.ts new file mode 100644 index 0000000..59efaa7 --- /dev/null +++ b/src/commands/listredirects.ts @@ -0,0 +1,53 @@ +import { Message } from 'eris'; +import { createPaginationEmbed } from 'eris-pagination'; +import { Client, Command, RichEmbed } from '../class'; + +export default class DelRedirect extends Command { + constructor(client: Client) { + super(client); + this.name = 'listredirects'; + this.description = 'Delete a redirect link for \'loc.sh\''; + this.usage = 'listredirects [key || redirect to]'; + this.aliases = ['getredirect', 'lsredirects', 'listlinks', 'lsr', 'gr']; + this.permissions = 6; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (args[0]) { + const redirects = await this.client.db.redirect.find({ $or: [{ key: args[0].toLowerCase() }, { to: args[0].toLowerCase() }] }); + if (!redirects) return this.error(message.channel, 'Could not find an entry matching that query.'); + const embed = new RichEmbed(); + embed.setTitle('Redirect Information'); + for (const redirect of redirects) { + embed.addField(redirect.key, redirect.to); + } + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setTimestamp(); + return message.channel.createMessage({ embed }); + } + const redirects = await this.client.db.redirect.find(); + if (!redirects) return this.error(message.channel, 'No redirect links found.'); + const redirectArray: [{ name: string, value: string }?] = []; + for (const redirect of redirects) { + redirectArray.push({ name: redirect.key, value: redirect.to }); + } + const splitRedirects = this.client.util.splitFields(redirectArray); + const cmdPages: RichEmbed[] = []; + splitRedirects.forEach((split) => { + const embed = new RichEmbed(); + embed.setTitle('Redirect Information'); + embed.setTimestamp(); + embed.setFooter(this.client.user.username, this.client.user.avatarURL); + embed.setDescription(`Command list for ${this.client.user.username}`); + split.forEach((c) => embed.addField(c.name, c.value)); + return cmdPages.push(embed); + }); + if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] }); + return createPaginationEmbed(message, cmdPages); + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +}