2018-07-12 03:02:47 +03:00
|
|
|
import { decorators as d, Plugin } from "knub";
|
2018-07-09 02:48:36 +03:00
|
|
|
import { GuildServerLogs } from "../data/GuildServerLogs";
|
|
|
|
import { LogType } from "../data/LogType";
|
|
|
|
import { TextChannel } from "eris";
|
2018-07-12 03:02:47 +03:00
|
|
|
import { formatTemplateString, stripObjectToScalars } from "../utils";
|
2018-07-12 03:02:13 +03:00
|
|
|
import DefaultLogMessages from "../data/DefaultLogMessages.json";
|
2018-07-12 02:58:34 +03:00
|
|
|
import moment from "moment-timezone";
|
2018-07-12 03:02:47 +03:00
|
|
|
import humanizeDuration from "humanize-duration";
|
2018-07-09 02:48:36 +03:00
|
|
|
|
|
|
|
interface ILogChannel {
|
|
|
|
include?: LogType[];
|
|
|
|
exclude?: LogType[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ILogChannelMap {
|
|
|
|
[channelId: string]: ILogChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class LogsPlugin extends Plugin {
|
|
|
|
protected serverLogs: GuildServerLogs;
|
|
|
|
protected logListener;
|
|
|
|
|
|
|
|
getDefaultOptions() {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
channels: {},
|
|
|
|
format: {
|
|
|
|
timestamp: "HH:mm:ss",
|
2018-07-12 03:02:13 +03:00
|
|
|
...DefaultLogMessages
|
2018-07-09 02:48:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
this.serverLogs = new GuildServerLogs(this.guildId);
|
|
|
|
|
|
|
|
this.logListener = ({ type, data }) => this.log(type, data);
|
|
|
|
this.serverLogs.on("log", this.logListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
onUnload() {
|
|
|
|
this.serverLogs.removeListener("log", this.logListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
log(type, data) {
|
|
|
|
const logChannels: ILogChannelMap = this.configValue("channels");
|
|
|
|
for (const [channelId, opts] of Object.entries(logChannels)) {
|
|
|
|
const channel = this.guild.channels.get(channelId);
|
|
|
|
if (!channel || !(channel instanceof TextChannel)) continue;
|
|
|
|
|
|
|
|
if (
|
|
|
|
(opts.include && opts.include.includes(type)) ||
|
|
|
|
(opts.exclude && !opts.exclude.includes(type))
|
|
|
|
) {
|
|
|
|
const message = this.getLogMessage(type, data);
|
|
|
|
if (message) channel.createMessage(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getLogMessage(type, data): string {
|
|
|
|
const format = this.configValue(`format.${LogType[type]}`, "");
|
|
|
|
if (format === "") return;
|
|
|
|
|
|
|
|
const formatted = formatTemplateString(format, data);
|
|
|
|
|
|
|
|
const timestampFormat = this.configValue("format.timestamp");
|
|
|
|
if (timestampFormat) {
|
|
|
|
const timestamp = moment().format(timestampFormat);
|
|
|
|
return `\`[${timestamp}]\` ${formatted}`;
|
|
|
|
} else {
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 03:02:47 +03:00
|
|
|
|
|
|
|
@d.event("guildMemberAdd")
|
|
|
|
onMemberJoin(_, member) {
|
|
|
|
const newThreshold = moment().valueOf() - 1000 * 60 * 60;
|
|
|
|
const accountAge = humanizeDuration(moment().valueOf() - member.createdAt, {
|
|
|
|
largest: 2,
|
|
|
|
round: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.log(LogType.MEMBER_JOIN, {
|
|
|
|
member: stripObjectToScalars(member, ["user"]),
|
|
|
|
new: member.createdAt >= newThreshold ? " :new:" : "",
|
|
|
|
account_age: accountAge
|
|
|
|
});
|
|
|
|
}
|
2018-07-09 02:48:36 +03:00
|
|
|
}
|