diff --git a/src/api/loc.sh/routes/root.ts b/src/api/loc.sh/routes/root.ts index 406a0d6..c7ce6e9 100644 --- a/src/api/loc.sh/routes/root.ts +++ b/src/api/loc.sh/routes/root.ts @@ -10,11 +10,14 @@ export default class Root extends Route { } public bind() { + this.router.get('/', (_req, res) => res.redirect('https://www.libraryofcode.org/')); + this.router.get('/:key', async (req, res) => { try { const link: RedirectRaw = await this.server.client.db.Redirect.findOne({ key: req.params.key }).lean().exec(); if (!link) return res.status(404).json({ code: this.constants.codes.NOT_FOUND, message: this.constants.messages.NOT_FOUND }); - return res.redirect(link.to); + res.redirect(link.to); + return await this.server.client.db.Redirect.updateOne({ key: req.params.key }, { $inc: { visitedCount: 1 } }); } catch (err) { this.server.client.util.handleError(err); return res.status(500).json({ code: this.constants.codes.SERVER_ERROR, message: this.constants.messages.SERVER_ERROR }); diff --git a/src/commands/addredirect.ts b/src/commands/addredirect.ts index 709389c..ad5956e 100644 --- a/src/commands/addredirect.ts +++ b/src/commands/addredirect.ts @@ -27,6 +27,7 @@ export default class AddRedirect extends Command { const redirect = new this.client.db.Redirect({ key: args[1].toLowerCase(), to: args[0], + visitedCount: 0, }); await redirect.save(); return this.success(message.channel, `Redirect https://loc.sh/${args[1].toLowerCase()} -> ${args[0]} is now active.`); diff --git a/src/commands/listredirects.ts b/src/commands/listredirects.ts index 9df7801..ae9a11e 100644 --- a/src/commands/listredirects.ts +++ b/src/commands/listredirects.ts @@ -21,7 +21,7 @@ export default class DelRedirect extends Command { const embed = new RichEmbed(); embed.setTitle('Redirect Information'); for (const redirect of redirects) { - embed.addField(redirect.key, redirect.to); + embed.addField(`${redirect.key} | visited ${redirect.visitedCount} times`, redirect.to); } embed.setFooter(this.client.user.username, this.client.user.avatarURL); embed.setTimestamp(); @@ -31,7 +31,7 @@ export default class DelRedirect extends Command { 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 }); + redirectArray.push({ name: `${redirect.key} | visited ${redirect.visitedCount} times`, value: redirect.to }); } const splitRedirects = this.client.util.splitFields(redirectArray); const cmdPages: RichEmbed[] = []; diff --git a/src/models/Redirect.ts b/src/models/Redirect.ts index 5376451..f17b77f 100644 --- a/src/models/Redirect.ts +++ b/src/models/Redirect.ts @@ -3,16 +3,19 @@ import { Document, Schema, model } from 'mongoose'; export interface RedirectInterface extends Document { key: string, to: string, + visitedCount: number, } export interface RedirectRaw { key: string, to: string, + visitedCount: number, } const Redirect: Schema = new Schema({ key: String, to: String, + visitedCount: Number, }); export default model('Redirect', Redirect);