Rename SpamLogs to Archives. Tweak spam archive format.
This commit is contained in:
parent
c9db802638
commit
e3ff4cef45
5 changed files with 116 additions and 92 deletions
|
@ -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");
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { decorators as d, Plugin } from "knub";
|
||||
import { Message, TextChannel } from "eris";
|
||||
import { Channel, Message, TextChannel, User } from "eris";
|
||||
import {
|
||||
formatTemplateString,
|
||||
getEmojiInString,
|
||||
getRoleMentions,
|
||||
getUrlsInString,
|
||||
|
@ -13,7 +14,8 @@ import { LogType } from "../data/LogType";
|
|||
import { GuildLogs } from "../data/GuildLogs";
|
||||
import { ModActionsPlugin } from "./ModActions";
|
||||
import { CaseType } from "../data/CaseType";
|
||||
import { GuildSpamLogs } from "../data/GuildSpamLogs";
|
||||
import { GuildArchives } from "../data/GuildArchives";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
enum RecentActionType {
|
||||
Message = 1,
|
||||
|
@ -35,9 +37,21 @@ interface IRecentAction {
|
|||
|
||||
const MAX_INTERVAL = 300;
|
||||
|
||||
const ARCHIVE_EXPIRY_DAYS = 90;
|
||||
const ARCHIVE_HEADER_FORMAT = trimLines(`
|
||||
Server: {guild.name} ({guild.id})
|
||||
Channel: #{channel.name} ({channel.id})
|
||||
User: {user.username}#{user.discriminator} ({user.id})
|
||||
`);
|
||||
const ARCHIVE_MESSAGE_FORMAT = "[MSG ID {message.id}] [{timestamp}] {user.username}: {message.content}{attachments}";
|
||||
const ARCHIVE_FOOTER_FORMAT = trimLines(`
|
||||
Log file generated on {timestamp}
|
||||
Expires at {expires}
|
||||
`);
|
||||
|
||||
export class SpamPlugin extends Plugin {
|
||||
protected logs: GuildLogs;
|
||||
protected spamLogs: GuildSpamLogs;
|
||||
protected archives: GuildArchives;
|
||||
|
||||
// Handle spam detection with a queue so we don't have overlapping detections on the same user
|
||||
protected spamDetectionQueue: Promise<void>;
|
||||
|
@ -82,7 +96,7 @@ export class SpamPlugin extends Plugin {
|
|||
|
||||
onLoad() {
|
||||
this.logs = new GuildLogs(this.guildId);
|
||||
this.spamLogs = new GuildSpamLogs(this.guildId);
|
||||
this.archives = new GuildArchives(this.guildId);
|
||||
|
||||
this.recentActions = [];
|
||||
this.expiryInterval = setInterval(() => this.clearOldRecentActions(), 1000 * 60);
|
||||
|
@ -138,13 +152,31 @@ export class SpamPlugin extends Plugin {
|
|||
this.recentActions = this.recentActions.filter(action => action.timestamp >= expiryTimestamp);
|
||||
}
|
||||
|
||||
async saveSpamLogs(messages: Message[]) {
|
||||
const channel = messages[0].channel as TextChannel;
|
||||
const header = `Server: ${this.guild.name} (${this.guild.id}), channel: #${channel.name} (${channel.id})`;
|
||||
const logId = await this.spamLogs.createFromMessages(messages, header);
|
||||
async saveSpamArchives(messages: Message[], channel: Channel, user: User) {
|
||||
const expiresAt = moment().add(ARCHIVE_EXPIRY_DAYS, "days");
|
||||
|
||||
const headerStr = formatTemplateString(ARCHIVE_HEADER_FORMAT, {
|
||||
guild: this.guild,
|
||||
channel,
|
||||
user
|
||||
});
|
||||
const msgLines = messages.map(msg => {
|
||||
return formatTemplateString(ARCHIVE_MESSAGE_FORMAT, {
|
||||
message: msg,
|
||||
timestamp: moment(msg.timestamp, "x").format("HH:mm:ss"),
|
||||
user
|
||||
});
|
||||
});
|
||||
const messagesStr = msgLines.join("\n");
|
||||
const footerStr = formatTemplateString(ARCHIVE_FOOTER_FORMAT, {
|
||||
timestamp: moment().format("YYYY-MM-DD [at] HH:mm:ss (Z)"),
|
||||
expires: expiresAt.format("YYYY-MM-DD [at] HH:mm:ss (Z)")
|
||||
});
|
||||
|
||||
const logId = await this.archives.create([headerStr, messagesStr, footerStr].join("\n\n"), expiresAt);
|
||||
|
||||
const url = this.knub.getGlobalConfig().url;
|
||||
return url ? `${url}/spam-logs/${logId}` : `Log ID: ${logId}`;
|
||||
return url ? `${url}/archives/${logId}` : `Archive ID: ${logId}`;
|
||||
}
|
||||
|
||||
async logAndDetectSpam(
|
||||
|
@ -225,7 +257,7 @@ export class SpamPlugin extends Plugin {
|
|||
this.clearRecentUserActions(type, msg.author.id, msg.channel.id);
|
||||
|
||||
// Generate a log from the detected messages
|
||||
const logUrl = await this.saveSpamLogs(uniqueMessages);
|
||||
const logUrl = await this.saveSpamArchives(uniqueMessages, msg.channel, msg.author);
|
||||
|
||||
// Create a case and log the actions taken above
|
||||
const caseType = spamConfig.mute ? CaseType.Mute : CaseType.Note;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue