Rename SpamLogs to Archives. Tweak spam archive format.

This commit is contained in:
Dragory 2018-08-18 19:51:28 +03:00
parent c9db802638
commit e3ff4cef45
5 changed files with 116 additions and 92 deletions

View file

@ -1,10 +1,10 @@
import http, { ServerResponse } from "http";
import { GlobalPlugin } from "knub";
import { GuildSpamLogs } from "../data/GuildSpamLogs";
import { GuildArchives } from "../data/GuildArchives";
import { sleep } from "../utils";
const DEFAULT_PORT = 9920;
const logUrlRegex = /^\/spam-logs\/([a-z0-9\-]+)\/?$/i;
const archivesRegex = /^\/(spam-logs|archives)\/([a-z0-9\-]+)\/?$/i;
function notFound(res: ServerResponse) {
res.statusCode = 404;
@ -15,18 +15,26 @@ function notFound(res: ServerResponse) {
* A global plugin that allows bot owners to control the bot
*/
export class LogServerPlugin extends GlobalPlugin {
protected spamLogs: GuildSpamLogs;
protected archives: GuildArchives;
protected server: http.Server;
async onLoad() {
this.spamLogs = new GuildSpamLogs(null);
this.archives = new GuildArchives(null);
this.server = http.createServer(async (req, res) => {
const logId = req.url.match(logUrlRegex);
if (!logId) return notFound(res);
const pathMatch = req.url.match(archivesRegex);
if (!pathMatch) return notFound(res);
if (logId) {
const log = await this.spamLogs.find(logId[1]);
const logId = pathMatch[2];
if (pathMatch[1] === "spam-logs") {
res.statusCode = 301;
res.setHeader("Location", `/archives/${logId}`);
return;
}
if (pathMatch) {
const log = await this.archives.find(logId);
if (!log) return notFound(res);
res.setHeader("Content-Type", "text/plain; charset=UTF-8");