mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Initial commit in new repository
This commit is contained in:
commit
23c78f2c9c
15 changed files with 4048 additions and 0 deletions
67
src/data/GuildModActions.ts
Normal file
67
src/data/GuildModActions.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
import knex from "../knex";
|
||||
|
||||
export class GuildModActions {
|
||||
protected guildId: string;
|
||||
|
||||
constructor(guildId) {
|
||||
this.guildId = guildId;
|
||||
}
|
||||
|
||||
find(id: number) {
|
||||
return knex("mod_actions")
|
||||
.where("guild_id", this.guildId)
|
||||
.where("id", id)
|
||||
.first();
|
||||
}
|
||||
|
||||
findByCaseNumber(caseNumber: number) {
|
||||
return knex("mod_actions")
|
||||
.where("guild_id", this.guildId)
|
||||
.where("case_number", caseNumber)
|
||||
.first();
|
||||
}
|
||||
|
||||
getActionNotes(actionId: number) {
|
||||
return knex("mod_action_notes")
|
||||
.where("mod_action_id", actionId)
|
||||
.select();
|
||||
}
|
||||
|
||||
getByUserId(userId: string) {
|
||||
return knex("mod_actions")
|
||||
.where("guild_id", this.guildId)
|
||||
.where("user_id", userId)
|
||||
.select();
|
||||
}
|
||||
|
||||
create(data) {
|
||||
return knex
|
||||
.insert({
|
||||
...data,
|
||||
guild_id: this.guildId,
|
||||
case_number: knex.raw(
|
||||
"(SELECT IFNULL(MAX(case_number)+1, 1) FROM mod_actions AS ma2 WHERE guild_id = ?)",
|
||||
this.guildId
|
||||
)
|
||||
})
|
||||
.returning("id")
|
||||
.into("mod_actions")
|
||||
.then(ids => Number(ids[0]));
|
||||
}
|
||||
|
||||
update(id, data) {
|
||||
return knex("mod_actions")
|
||||
.where("id", id)
|
||||
.update(data);
|
||||
}
|
||||
|
||||
createNote(modActionId: number, data: any) {
|
||||
return knex
|
||||
.insert({
|
||||
...data,
|
||||
mod_action_id: modActionId
|
||||
})
|
||||
.into("mod_action_notes")
|
||||
.return();
|
||||
}
|
||||
}
|
31
src/index.ts
Normal file
31
src/index.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
require("dotenv").config();
|
||||
|
||||
process.on("unhandledRejection", (reason, p) => {
|
||||
// tslint:disable-next-line
|
||||
console.error(reason);
|
||||
process.exit();
|
||||
});
|
||||
|
||||
import { Client } from "eris";
|
||||
import { Knub } from "knub";
|
||||
import { BotControlPlugin } from "./plugins/BotControl";
|
||||
import { ModActionsPlugin } from "./plugins/ModActions";
|
||||
import { UtilityPlugin } from "./plugins/Utility";
|
||||
import knex from "./knex";
|
||||
|
||||
// Run latest database migrations
|
||||
knex.migrate.latest().then(() => {
|
||||
const client = new Client(process.env.TOKEN);
|
||||
|
||||
const bot = new Knub(client, {
|
||||
plugins: {
|
||||
utility: UtilityPlugin,
|
||||
mod_notes: ModActionsPlugin
|
||||
},
|
||||
globalPlugins: {
|
||||
bot_control: BotControlPlugin
|
||||
}
|
||||
});
|
||||
|
||||
bot.run();
|
||||
});
|
6
src/knex.ts
Normal file
6
src/knex.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
const knexfile = require("../knexfile");
|
||||
import * as knex from "knex";
|
||||
|
||||
const db = knex(knexfile);
|
||||
|
||||
export default db;
|
46
src/plugins/BotControl.ts
Normal file
46
src/plugins/BotControl.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { decorators as d, GlobalPlugin } from "knub";
|
||||
import * as child_process from "child_process";
|
||||
import { Message } from "eris";
|
||||
|
||||
/**
|
||||
* A global plugin that allows bot owners to control the bot
|
||||
*/
|
||||
export class BotControlPlugin extends GlobalPlugin {
|
||||
getDefaultOptions() {
|
||||
return {
|
||||
config: {
|
||||
owners: []
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
isOwner(userId) {
|
||||
return this.configValue("owners").includes(userId);
|
||||
}
|
||||
|
||||
@d.command("bot_update")
|
||||
async updateCmd(msg: Message) {
|
||||
if (!this.isOwner(msg.author.id)) return;
|
||||
|
||||
const updateCmd = this.configValue("update_cmd");
|
||||
if (!updateCmd) {
|
||||
msg.channel.createMessage("Update command not specified!");
|
||||
return;
|
||||
}
|
||||
|
||||
msg.channel.createMessage("Updating...");
|
||||
const updater = child_process.exec(updateCmd, { cwd: process.cwd() });
|
||||
updater.stderr.on("data", data => {
|
||||
// tslint:disable-next-line
|
||||
console.error(data);
|
||||
});
|
||||
}
|
||||
|
||||
@d.command("bot_reload")
|
||||
async reloadCmd(msg: Message) {
|
||||
if (!this.isOwner(msg.author.id)) return;
|
||||
|
||||
msg.channel.createMessage("Reloading...");
|
||||
this.knub.reloadGuild(this.guildId);
|
||||
}
|
||||
}
|
384
src/plugins/ModActions.ts
Normal file
384
src/plugins/ModActions.ts
Normal file
|
@ -0,0 +1,384 @@
|
|||
import { Plugin, decorators as d } from "knub";
|
||||
import {
|
||||
Guild,
|
||||
GuildAuditLogEntry,
|
||||
Member,
|
||||
Message,
|
||||
TextChannel,
|
||||
User
|
||||
} from "eris";
|
||||
import * as moment from "moment";
|
||||
import { GuildModActions } from "../data/GuildModActions";
|
||||
|
||||
enum ActionType {
|
||||
Ban = 1,
|
||||
Unban,
|
||||
Note,
|
||||
Warn,
|
||||
Kick
|
||||
}
|
||||
|
||||
const sleep = (ms: number): Promise<void> => {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(resolve, ms);
|
||||
});
|
||||
};
|
||||
|
||||
export class ModActionsPlugin extends Plugin {
|
||||
protected modActions: GuildModActions;
|
||||
|
||||
async onLoad() {
|
||||
this.modActions = new GuildModActions(this.guildId);
|
||||
}
|
||||
|
||||
getDefaultOptions() {
|
||||
return {
|
||||
config: {
|
||||
dm_on_warn: true,
|
||||
dm_on_mute: true,
|
||||
dm_on_kick: false,
|
||||
dm_on_ban: false,
|
||||
message_on_warn: false,
|
||||
message_on_mute: false,
|
||||
message_on_kick: false,
|
||||
message_on_ban: false,
|
||||
message_channel: null,
|
||||
warn_message: "You have received a warning on {guildName}: {reason}",
|
||||
mute_message: "You have been muted on {guildName} for {reason}",
|
||||
kick_message: "You have been kicked from {guildName} for {reason}",
|
||||
ban_message: "You have been banned from {guildName} for {reason}",
|
||||
log_automatic_actions: true,
|
||||
action_log_channel: null,
|
||||
alert_on_rejoin: false,
|
||||
alert_channel: null
|
||||
},
|
||||
permissions: {
|
||||
note: false,
|
||||
warn: false,
|
||||
mute: false,
|
||||
kick: false,
|
||||
ban: false,
|
||||
view: false
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
level: ">=50",
|
||||
permissions: {
|
||||
note: true,
|
||||
warn: true,
|
||||
mute: true,
|
||||
kick: true,
|
||||
ban: true,
|
||||
view: true
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a BAN action automatically when a user is banned.
|
||||
* Attempts to find the ban's details in the audit log.
|
||||
*/
|
||||
@d.event("guildBanAdd")
|
||||
async onGuildBanAdd(guild: Guild, user: User) {
|
||||
await sleep(1000); // Wait a moment for the audit log to update
|
||||
const relevantAuditLogEntry = await this.findRelevantAuditLogEntry(
|
||||
"MEMBER_BAN_ADD",
|
||||
user.id
|
||||
);
|
||||
|
||||
let modActionId;
|
||||
|
||||
if (relevantAuditLogEntry) {
|
||||
const modId = relevantAuditLogEntry.user.id;
|
||||
const auditLogId = relevantAuditLogEntry.id;
|
||||
|
||||
modActionId = await this.createModAction(
|
||||
user.id,
|
||||
modId,
|
||||
ActionType.Ban,
|
||||
auditLogId
|
||||
);
|
||||
|
||||
if (relevantAuditLogEntry.reason) {
|
||||
await this.createModActionNote(
|
||||
modActionId,
|
||||
modId,
|
||||
relevantAuditLogEntry.reason
|
||||
);
|
||||
}
|
||||
} else {
|
||||
modActionId = await this.createModAction(user.id, null, ActionType.Ban);
|
||||
}
|
||||
|
||||
this.displayModAction(modActionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an UNBAN mod action automatically when a user is unbanned.
|
||||
* Attempts to find the unban's details in the audit log.
|
||||
*/
|
||||
@d.event("guildBanRemove")
|
||||
async onGuildBanRemove(guild: Guild, user: User) {
|
||||
const relevantAuditLogEntry = await this.findRelevantAuditLogEntry(
|
||||
"MEMBER_BAN_REMOVE",
|
||||
user.id
|
||||
);
|
||||
|
||||
let modActionId;
|
||||
|
||||
if (relevantAuditLogEntry) {
|
||||
const modId = relevantAuditLogEntry.user.id;
|
||||
const auditLogId = relevantAuditLogEntry.id;
|
||||
|
||||
modActionId = await this.createModAction(
|
||||
user.id,
|
||||
modId,
|
||||
ActionType.Unban,
|
||||
auditLogId
|
||||
);
|
||||
} else {
|
||||
modActionId = await this.createModAction(user.id, null, ActionType.Unban);
|
||||
}
|
||||
|
||||
this.displayModAction(modActionId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an alert if a member with prior notes joins the server
|
||||
*/
|
||||
@d.event("guildMemberAdd")
|
||||
async onGuildMemberAdd(member: Member) {
|
||||
if (! this.configValue('alert_on_rejoin')) return;
|
||||
|
||||
const alertChannelId = this.configValue('alert_channel');
|
||||
if (! alertChannelId) return;
|
||||
|
||||
const actions = await this.modActions.getByUserId(member.id);
|
||||
|
||||
if (actions.length) {
|
||||
const alertChannel: any = this.guild.channels.get(alertChannelId);
|
||||
alertChannel.send(
|
||||
`<@!${member.id}> (${member.user.username}#${
|
||||
member.user.discriminator
|
||||
} \`${member.id}\`) joined with ${actions.length} prior record(s)`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified case by adding more details to it
|
||||
*/
|
||||
@d.command("update", "<caseNumber:number> <note:string$>")
|
||||
@d.permission("note")
|
||||
async updateCmd(msg: Message, args: any) {
|
||||
const action = await this.modActions.findByCaseNumber(args.caseNumber);
|
||||
if (!action) {
|
||||
msg.channel.createMessage("Case not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (action.mod_id === null) {
|
||||
// If the action has no moderator information, assume the first one to update it did the action
|
||||
await this.modActions.update(action.id, {
|
||||
mod_id: msg.author.id,
|
||||
mod_name: `${msg.author.username}#${msg.author.discriminator}`
|
||||
});
|
||||
}
|
||||
|
||||
await this.createModActionNote(action.id, msg.author.id, args.note);
|
||||
|
||||
this.displayModAction(action.id, msg.channel.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new NOTE type mod action and add the specified note to it
|
||||
*/
|
||||
@d.command("note", "<userId:string> <note:string$>")
|
||||
@d.permission("note")
|
||||
async noteCmd(msg: Message, args: any) {
|
||||
const actionId = await this.createModAction(
|
||||
args.userId,
|
||||
msg.author.id,
|
||||
ActionType.Note
|
||||
);
|
||||
await this.createModActionNote(actionId, msg.author.id, args.note);
|
||||
|
||||
this.displayModAction(actionId, msg.channel.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a case or list of cases
|
||||
* If the argument passed is a case id, display that case
|
||||
* If the argument passed is a user id, show all cases on that user
|
||||
*/
|
||||
@d.command("/showcase|case|cases|usercases/", "<caseNumberOrUserId:string>")
|
||||
@d.permission("view")
|
||||
async showcaseCmd(msg: Message, args: any) {
|
||||
if (args.caseNumberOrUserId.length >= 17) {
|
||||
// Assume user id
|
||||
const actions = await this.modActions.getByUserId(args.userId);
|
||||
|
||||
if (actions.length === 0) {
|
||||
msg.channel.createMessage("No cases found for the specified user!");
|
||||
} else {
|
||||
for (const action of actions) {
|
||||
await this.displayModAction(action, msg.channel.id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Assume case id
|
||||
const action = await this.modActions.findByCaseNumber(args.caseNumber);
|
||||
|
||||
if (!action) {
|
||||
msg.channel.createMessage("Case not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
this.displayModAction(action.id, msg.channel.id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows information about the specified action in a message embed.
|
||||
* If no channelId is specified, uses the channel id from config.
|
||||
*/
|
||||
protected async displayModAction(actionOrId: any, channelId: string = null) {
|
||||
let action;
|
||||
if (typeof actionOrId === "number") {
|
||||
action = await this.modActions.find(actionOrId);
|
||||
} else {
|
||||
action = actionOrId;
|
||||
}
|
||||
|
||||
if (!action) return;
|
||||
|
||||
if (!channelId) {
|
||||
channelId = this.configValue('action_log_channel');
|
||||
}
|
||||
|
||||
if (!channelId) return;
|
||||
|
||||
const notes = await this.modActions.getActionNotes(action.id);
|
||||
|
||||
const createdAt = moment(action.created_at);
|
||||
const actionTypeStr = ActionType[action.action_type].toUpperCase();
|
||||
|
||||
const embed: any = {
|
||||
title: `${actionTypeStr} - Case #${action.case_number}`,
|
||||
footer: {
|
||||
text: `Case created at ${createdAt.format("YYYY-MM-DD [at] HH:mm")}`
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: "User",
|
||||
value: `${action.user_name}\n<@!${action.user_id}>`,
|
||||
inline: true
|
||||
},
|
||||
{
|
||||
name: "Moderator",
|
||||
value: `${action.mod_name}\n<@!${action.mod_id}>`,
|
||||
inline: true
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
if (actionTypeStr === "BAN") {
|
||||
embed.color = 0xe67e22;
|
||||
} else if (actionTypeStr === "UNBAN") {
|
||||
embed.color = 0x9b59b6;
|
||||
} else if (actionTypeStr === "NOTE") {
|
||||
embed.color = 0x3498db;
|
||||
}
|
||||
|
||||
if (notes.length) {
|
||||
notes.forEach((note: any) => {
|
||||
const noteDate = moment(note.created_at);
|
||||
embed.addField(
|
||||
`${note.mod_name} at ${noteDate.format("YYYY-MM-DD [at] HH:mm")}:`,
|
||||
note.body
|
||||
);
|
||||
});
|
||||
} else {
|
||||
embed.addField("!!! THIS CASE HAS NO NOTES !!!", "\u200B");
|
||||
}
|
||||
|
||||
(this.bot.guilds
|
||||
.get(this.guildId)
|
||||
.channels.get(channelId) as TextChannel).createMessage({
|
||||
embed
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to find a relevant audit log entry for the given user and action. Only accepts audit log entries from the past 10 minutes.
|
||||
*/
|
||||
protected async findRelevantAuditLogEntry(
|
||||
actionType: string,
|
||||
userId: string
|
||||
): Promise<GuildAuditLogEntry> {
|
||||
const auditLogEntries = await this.bot.getGuildAuditLogs(
|
||||
this.guildId,
|
||||
5,
|
||||
actionType
|
||||
);
|
||||
|
||||
auditLogEntries.entries.sort((a, b) => {
|
||||
if (a.createdAt > b.createdAt) return -1;
|
||||
if (a.createdAt > b.createdAt) return 1;
|
||||
return 0;
|
||||
});
|
||||
|
||||
const cutoffDate = new Date();
|
||||
cutoffDate.setTime(cutoffDate.getTime() - 1000 * 15);
|
||||
const cutoffTS = cutoffDate.getTime();
|
||||
|
||||
return auditLogEntries.entries.find(entry => {
|
||||
return entry.target.id === userId && entry.createdAt >= cutoffTS;
|
||||
});
|
||||
}
|
||||
|
||||
protected async createModAction(
|
||||
userId: string,
|
||||
modId: string,
|
||||
actionType: ActionType,
|
||||
auditLogId: string = null
|
||||
): Promise<number> {
|
||||
const user = this.bot.users.get(userId);
|
||||
const userName = user
|
||||
? `${user.username}#${user.discriminator}`
|
||||
: "Unknown#0000";
|
||||
|
||||
const mod = this.bot.users.get(modId);
|
||||
const modName = mod
|
||||
? `${mod.username}#${mod.discriminator}`
|
||||
: "Unknown#0000";
|
||||
|
||||
return this.modActions.create({
|
||||
user_id: userId,
|
||||
user_name: userName,
|
||||
mod_id: modId,
|
||||
mod_name: modName,
|
||||
action_type: actionType,
|
||||
audit_log_id: auditLogId
|
||||
});
|
||||
}
|
||||
|
||||
protected async createModActionNote(
|
||||
modActionId: number,
|
||||
modId: string,
|
||||
body: string
|
||||
) {
|
||||
const mod = this.bot.users.get(modId);
|
||||
const modName = mod
|
||||
? `${mod.username}#${mod.discriminator}`
|
||||
: "Unknown#0000";
|
||||
|
||||
return this.modActions.createNote(modActionId, {
|
||||
mod_id: modId,
|
||||
mod_name: modName,
|
||||
body
|
||||
});
|
||||
}
|
||||
}
|
29
src/plugins/Utility.ts
Normal file
29
src/plugins/Utility.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { Plugin, decorators as d } from "knub";
|
||||
import { Message, TextChannel } from "eris";
|
||||
|
||||
export class UtilityPlugin extends Plugin {
|
||||
getDefaultOptions() {
|
||||
return {
|
||||
permissions: {
|
||||
roles: false
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
level: ">=50",
|
||||
permissions: {
|
||||
roles: true
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
@d.command("roles")
|
||||
@d.permission("roles")
|
||||
async rolesCmd(msg: Message) {
|
||||
const roles = (msg.channel as TextChannel).guild.roles.map(
|
||||
role => `${role.name} ${role.id}`
|
||||
);
|
||||
msg.channel.createMessage("```" + roles.join("\n") + "```");
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue