Dashboard work and related
This commit is contained in:
parent
7bda2b1763
commit
b230a73a6f
44 changed files with 637 additions and 272 deletions
24
src/plugins/GuildInfoSaver.ts
Normal file
24
src/plugins/GuildInfoSaver.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
||||
import { AllowedGuilds } from "../data/AllowedGuilds";
|
||||
import { MINUTES } from "../utils";
|
||||
|
||||
export class GuildInfoSaverPlugin extends ZeppelinPlugin {
|
||||
public static pluginName = "guild_info_saver";
|
||||
protected allowedGuilds: AllowedGuilds;
|
||||
private updateInterval;
|
||||
|
||||
onLoad() {
|
||||
this.allowedGuilds = new AllowedGuilds();
|
||||
|
||||
this.updateGuildInfo();
|
||||
this.updateInterval = setInterval(() => this.updateGuildInfo(), 60 * MINUTES);
|
||||
}
|
||||
|
||||
onUnload() {
|
||||
clearInterval(this.updateInterval);
|
||||
}
|
||||
|
||||
protected updateGuildInfo() {
|
||||
this.allowedGuilds.updateInfo(this.guildId, this.guild.name, this.guild.iconURL);
|
||||
}
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
import http, { ServerResponse } from "http";
|
||||
import { GlobalPlugin, IPluginOptions, logger } from "knub";
|
||||
import { GuildArchives } from "../data/GuildArchives";
|
||||
import { sleep } from "../utils";
|
||||
import moment from "moment-timezone";
|
||||
|
||||
const DEFAULT_PORT = 9920;
|
||||
const archivesRegex = /^\/(spam-logs|archives)\/([a-z0-9\-]+)\/?$/i;
|
||||
|
||||
function notFound(res: ServerResponse) {
|
||||
res.statusCode = 404;
|
||||
res.end("Not Found");
|
||||
}
|
||||
|
||||
interface ILogServerPluginConfig {
|
||||
port: number;
|
||||
}
|
||||
|
||||
export class LogServerPlugin extends GlobalPlugin<ILogServerPluginConfig> {
|
||||
public static pluginName = "log_server";
|
||||
|
||||
protected archives: GuildArchives;
|
||||
protected server: http.Server;
|
||||
|
||||
protected getDefaultOptions(): IPluginOptions<ILogServerPluginConfig> {
|
||||
return {
|
||||
config: {
|
||||
port: DEFAULT_PORT,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async onLoad() {
|
||||
this.archives = new GuildArchives(null);
|
||||
|
||||
this.server = http.createServer(async (req, res) => {
|
||||
const pathMatch = req.url.match(archivesRegex);
|
||||
if (!pathMatch) return notFound(res);
|
||||
|
||||
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);
|
||||
|
||||
let body = log.body;
|
||||
|
||||
// Add some metadata at the end of the log file (but only if it doesn't already have it directly in the body)
|
||||
if (log.body.indexOf("Log file generated on") === -1) {
|
||||
const createdAt = moment(log.created_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\n\nLog file generated on ${createdAt}`;
|
||||
|
||||
if (log.expires_at !== null) {
|
||||
const expiresAt = moment(log.expires_at).format("YYYY-MM-DD [at] HH:mm:ss [(+00:00)]");
|
||||
body += `\nExpires at ${expiresAt}`;
|
||||
}
|
||||
}
|
||||
|
||||
res.setHeader("Content-Type", "text/plain; charset=UTF-8");
|
||||
res.end(body);
|
||||
}
|
||||
});
|
||||
|
||||
const port = this.getConfig().port;
|
||||
let retried = false;
|
||||
|
||||
this.server.on("error", async (err: any) => {
|
||||
if (err.code === "EADDRINUSE" && !retried) {
|
||||
logger.info("Got EADDRINUSE, retrying in 2 sec...");
|
||||
retried = true;
|
||||
await sleep(2000);
|
||||
this.server.listen(port);
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
this.server.listen(port);
|
||||
}
|
||||
|
||||
async onUnload() {
|
||||
return new Promise(resolve => {
|
||||
this.server.close(() => resolve());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -19,9 +19,9 @@ import { SelfGrantableRolesPlugin } from "./SelfGrantableRolesPlugin";
|
|||
import { RemindersPlugin } from "./Reminders";
|
||||
import { WelcomeMessagePlugin } from "./WelcomeMessage";
|
||||
import { BotControlPlugin } from "./BotControl";
|
||||
import { LogServerPlugin } from "./LogServer";
|
||||
import { UsernameSaver } from "./UsernameSaver";
|
||||
import { CustomEventsPlugin } from "./CustomEvents";
|
||||
import { GuildInfoSaverPlugin } from "./GuildInfoSaver";
|
||||
|
||||
/**
|
||||
* Plugins available to be loaded for individual guilds
|
||||
|
@ -48,12 +48,14 @@ export const availablePlugins = [
|
|||
RemindersPlugin,
|
||||
WelcomeMessagePlugin,
|
||||
CustomEventsPlugin,
|
||||
GuildInfoSaverPlugin,
|
||||
];
|
||||
|
||||
/**
|
||||
* Plugins that are always loaded (subset of the names of the plugins in availablePlugins)
|
||||
*/
|
||||
export const basePlugins = [
|
||||
GuildInfoSaverPlugin.pluginName,
|
||||
MessageSaverPlugin.pluginName,
|
||||
NameHistoryPlugin.pluginName,
|
||||
CasesPlugin.pluginName,
|
||||
|
@ -63,4 +65,4 @@ export const basePlugins = [
|
|||
/**
|
||||
* Available global plugins (can't be loaded per-guild, only globally)
|
||||
*/
|
||||
export const availableGlobalPlugins = [BotControlPlugin, LogServerPlugin, UsernameSaver];
|
||||
export const availableGlobalPlugins = [BotControlPlugin, UsernameSaver];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue