add commands to add/delete/list redirect links

merge-requests/11/merge
Matthew 2020-05-05 19:13:39 -04:00
parent 171da19be8
commit f94bda7c4f
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
3 changed files with 117 additions and 0 deletions

View File

@ -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 <redirect to url> <key>';
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);
}
}
}

View File

@ -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 <key>';
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);
}
}
}

View File

@ -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);
}
}
}