2018-11-25 17:04:26 +02:00
|
|
|
import { decorators as d, waitForReaction, waitForReply } from "knub";
|
2018-07-29 23:30:24 +03:00
|
|
|
import { Constants as ErisConstants, Guild, Member, Message, TextChannel, User } from "eris";
|
2018-07-12 02:58:34 +03:00
|
|
|
import humanizeDuration from "humanize-duration";
|
2018-07-12 02:53:26 +03:00
|
|
|
import { GuildCases } from "../data/GuildCases";
|
2018-07-09 02:51:34 +03:00
|
|
|
import {
|
2018-11-24 19:14:12 +02:00
|
|
|
chunkMessageLines,
|
2018-07-09 02:51:34 +03:00
|
|
|
convertDelayStringToMS,
|
2018-08-02 00:51:25 +03:00
|
|
|
disableLinkPreviews,
|
2018-07-09 03:00:10 +03:00
|
|
|
errorMessage,
|
2018-07-29 18:46:49 +03:00
|
|
|
findRelevantAuditLogEntry,
|
2018-07-09 03:00:10 +03:00
|
|
|
formatTemplateString,
|
2018-07-09 02:51:34 +03:00
|
|
|
stripObjectToScalars,
|
2018-08-02 00:51:25 +03:00
|
|
|
successMessage,
|
|
|
|
trimLines
|
2018-07-09 02:51:34 +03:00
|
|
|
} from "../utils";
|
2018-07-08 13:57:27 +03:00
|
|
|
import { GuildMutes } from "../data/GuildMutes";
|
2018-10-26 06:41:20 +03:00
|
|
|
import { CaseTypes } from "../data/CaseTypes";
|
2018-07-29 18:46:49 +03:00
|
|
|
import { GuildLogs } from "../data/GuildLogs";
|
2018-07-09 02:51:34 +03:00
|
|
|
import { LogType } from "../data/LogType";
|
2018-07-29 23:30:24 +03:00
|
|
|
import Timer = NodeJS.Timer;
|
2018-11-25 17:04:26 +02:00
|
|
|
import { ZeppelinPlugin } from "./ZeppelinPlugin";
|
|
|
|
import { GuildActions } from "../data/GuildActions";
|
|
|
|
import { Case } from "../data/entities/Case";
|
|
|
|
import { Mute } from "../data/entities/Mute";
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
enum IgnoredEventType {
|
|
|
|
Ban = 1,
|
|
|
|
Unban,
|
|
|
|
Kick
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IIgnoredEvent {
|
|
|
|
type: IgnoredEventType;
|
|
|
|
userId: string;
|
|
|
|
}
|
|
|
|
|
2018-08-02 02:46:57 +03:00
|
|
|
const CASE_LIST_REASON_MAX_LENGTH = 80;
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
export class ModActionsPlugin extends ZeppelinPlugin {
|
|
|
|
protected actions: GuildActions;
|
|
|
|
protected mutes: GuildMutes;
|
2018-07-12 02:53:26 +03:00
|
|
|
protected cases: GuildCases;
|
2018-07-29 18:46:49 +03:00
|
|
|
protected serverLogs: GuildLogs;
|
2018-07-09 02:51:34 +03:00
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
protected ignoredEvents: IIgnoredEvent[];
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
async onLoad() {
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions = GuildActions.getInstance(this.guildId);
|
2018-10-26 06:41:20 +03:00
|
|
|
this.mutes = GuildMutes.getInstance(this.guildId);
|
2018-11-25 17:04:26 +02:00
|
|
|
this.cases = GuildCases.getInstance(this.guildId);
|
2018-07-29 18:46:49 +03:00
|
|
|
this.serverLogs = new GuildLogs(this.guildId);
|
2018-07-08 13:57:27 +03:00
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoredEvents = [];
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultOptions() {
|
|
|
|
return {
|
|
|
|
config: {
|
|
|
|
dm_on_warn: true,
|
2018-08-02 00:51:25 +03:00
|
|
|
dm_on_mute: false,
|
2018-07-01 03:35:51 +03:00
|
|
|
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}",
|
2018-07-12 01:45:26 +03:00
|
|
|
mute_message: "You have been muted on {guildName}. Reason given: {reason}",
|
|
|
|
timed_mute_message: "You have been muted on {guildName} for {time}. Reason given: {reason}",
|
|
|
|
kick_message: "You have been kicked from {guildName}. Reason given: {reason}",
|
|
|
|
ban_message: "You have been banned from {guildName}. Reason given: {reason}",
|
2018-07-01 03:35:51 +03:00
|
|
|
alert_on_rejoin: false,
|
|
|
|
alert_channel: null
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
note: false,
|
|
|
|
warn: false,
|
|
|
|
mute: false,
|
|
|
|
kick: false,
|
|
|
|
ban: false,
|
2018-07-14 20:55:39 +03:00
|
|
|
view: false,
|
2018-08-16 20:07:43 +03:00
|
|
|
addcase: false,
|
|
|
|
massban: true
|
2018-07-01 03:35:51 +03:00
|
|
|
},
|
|
|
|
overrides: [
|
|
|
|
{
|
|
|
|
level: ">=50",
|
|
|
|
permissions: {
|
|
|
|
note: true,
|
|
|
|
warn: true,
|
|
|
|
mute: true,
|
|
|
|
kick: true,
|
|
|
|
ban: true,
|
2018-07-14 20:55:39 +03:00
|
|
|
view: true,
|
|
|
|
addcase: true
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
2018-08-16 20:07:43 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
level: ">=100",
|
|
|
|
permissions: {
|
|
|
|
massban: true
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
ignoreEvent(type: IgnoredEventType, userId: any, timeout: number = null) {
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoredEvents.push({ type, userId });
|
|
|
|
|
|
|
|
// Clear after expiry (15sec by default)
|
|
|
|
setTimeout(() => {
|
|
|
|
this.clearIgnoredEvent(type, userId);
|
2018-08-18 13:34:57 +03:00
|
|
|
}, timeout || 1000 * 15);
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isEventIgnored(type: IgnoredEventType, userId: any) {
|
|
|
|
return this.ignoredEvents.some(info => type === info.type && userId === info.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearIgnoredEvent(type: IgnoredEventType, userId: any) {
|
2018-08-18 13:34:57 +03:00
|
|
|
this.ignoredEvents.splice(this.ignoredEvents.findIndex(info => type === info.type && userId === info.userId), 1);
|
2018-07-31 04:02:45 +03:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
/**
|
|
|
|
* 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) {
|
2018-07-31 04:02:45 +03:00
|
|
|
if (this.isEventIgnored(IgnoredEventType.Ban, user.id)) {
|
|
|
|
this.clearIgnoredEvent(IgnoredEventType.Ban, user.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:46:49 +03:00
|
|
|
const relevantAuditLogEntry = await findRelevantAuditLogEntry(
|
2018-07-29 23:30:24 +03:00
|
|
|
this.guild,
|
|
|
|
ErisConstants.AuditLogActions.MEMBER_BAN_ADD,
|
2018-07-29 18:46:49 +03:00
|
|
|
user.id
|
|
|
|
);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
|
|
|
if (relevantAuditLogEntry) {
|
|
|
|
const modId = relevantAuditLogEntry.user.id;
|
|
|
|
const auditLogId = relevantAuditLogEntry.id;
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("createCase", {
|
|
|
|
userId: user.id,
|
|
|
|
modId,
|
|
|
|
type: CaseTypes.Ban,
|
|
|
|
auditLogId,
|
|
|
|
reason: relevantAuditLogEntry.reason,
|
|
|
|
automatic: true
|
|
|
|
});
|
2018-07-01 03:35:51 +03:00
|
|
|
} else {
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("createCase", {
|
|
|
|
userId: user.id,
|
|
|
|
type: CaseTypes.Ban
|
|
|
|
});
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2018-07-31 04:02:45 +03:00
|
|
|
if (this.isEventIgnored(IgnoredEventType.Unban, user.id)) {
|
|
|
|
this.clearIgnoredEvent(IgnoredEventType.Unban, user.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:46:49 +03:00
|
|
|
const relevantAuditLogEntry = await findRelevantAuditLogEntry(
|
2018-07-29 23:30:24 +03:00
|
|
|
this.guild,
|
|
|
|
ErisConstants.AuditLogActions.MEMBER_BAN_REMOVE,
|
2018-07-01 03:35:51 +03:00
|
|
|
user.id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (relevantAuditLogEntry) {
|
|
|
|
const modId = relevantAuditLogEntry.user.id;
|
|
|
|
const auditLogId = relevantAuditLogEntry.id;
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("createCase", {
|
|
|
|
userId: user.id,
|
|
|
|
modId,
|
|
|
|
type: CaseTypes.Unban,
|
|
|
|
auditLogId,
|
|
|
|
automatic: true
|
|
|
|
});
|
2018-07-01 03:35:51 +03:00
|
|
|
} else {
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("createCase", {
|
|
|
|
userId: user.id,
|
|
|
|
type: CaseTypes.Unban,
|
|
|
|
automatic: true
|
|
|
|
});
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show an alert if a member with prior notes joins the server
|
|
|
|
*/
|
|
|
|
@d.event("guildMemberAdd")
|
2018-07-29 23:30:24 +03:00
|
|
|
async onGuildMemberAdd(_, member: Member) {
|
2018-07-01 04:31:24 +03:00
|
|
|
if (!this.configValue("alert_on_rejoin")) return;
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-07-01 04:31:24 +03:00
|
|
|
const alertChannelId = this.configValue("alert_channel");
|
|
|
|
if (!alertChannelId) return;
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-07-12 02:53:26 +03:00
|
|
|
const actions = await this.cases.getByUserId(member.id);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
|
|
|
if (actions.length) {
|
|
|
|
const alertChannel: any = this.guild.channels.get(alertChannelId);
|
|
|
|
alertChannel.send(
|
2018-08-18 13:34:57 +03:00
|
|
|
`<@!${member.id}> (${member.user.username}#${member.user.discriminator} \`${member.id}\`) joined with ${
|
|
|
|
actions.length
|
|
|
|
} prior record(s)`
|
2018-07-01 03:35:51 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-29 23:30:24 +03:00
|
|
|
@d.event("guildMemberRemove")
|
|
|
|
async onGuildMemberRemove(_, member: Member) {
|
2018-07-31 04:02:45 +03:00
|
|
|
if (this.isEventIgnored(IgnoredEventType.Kick, member.id)) {
|
|
|
|
this.clearIgnoredEvent(IgnoredEventType.Kick, member.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-29 23:30:24 +03:00
|
|
|
const kickAuditLogEntry = await findRelevantAuditLogEntry(
|
|
|
|
this.guild,
|
|
|
|
ErisConstants.AuditLogActions.MEMBER_KICK,
|
|
|
|
member.id
|
|
|
|
);
|
|
|
|
|
|
|
|
if (kickAuditLogEntry) {
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("createCase", {
|
|
|
|
userId: member.id,
|
|
|
|
modId: kickAuditLogEntry.user.id,
|
|
|
|
type: CaseTypes.Kick,
|
|
|
|
auditLogId: kickAuditLogEntry.id,
|
|
|
|
reason: kickAuditLogEntry.reason,
|
|
|
|
automatic: true
|
|
|
|
});
|
|
|
|
|
2018-07-29 23:30:24 +03:00
|
|
|
this.serverLogs.log(LogType.MEMBER_KICK, {
|
|
|
|
user: stripObjectToScalars(member.user),
|
|
|
|
mod: stripObjectToScalars(kickAuditLogEntry.user)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
/**
|
2018-07-08 13:57:27 +03:00
|
|
|
* Update the specified case by adding more notes/details to it
|
2018-07-01 03:35:51 +03:00
|
|
|
*/
|
2018-07-08 13:57:27 +03:00
|
|
|
@d.command(/update|updatecase/, "<caseNumber:number> <note:string$>")
|
2018-07-01 03:35:51 +03:00
|
|
|
@d.permission("note")
|
|
|
|
async updateCmd(msg: Message, args: any) {
|
2018-08-02 00:51:25 +03:00
|
|
|
const theCase = await this.cases.findByCaseNumber(args.caseNumber);
|
|
|
|
if (!theCase) {
|
2018-11-25 17:04:26 +02:00
|
|
|
msg.channel.createMessage(errorMessage("Case not found"));
|
2018-07-01 03:35:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
await this.actions.fire("createCaseNote", theCase, {
|
|
|
|
modId: msg.author.id,
|
|
|
|
note: args.note
|
|
|
|
});
|
2018-08-02 00:51:25 +03:00
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
msg.channel.createMessage(successMessage(`Case \`#${theCase.case_number}\` updated`));
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.command("note", "<userId:userId> <note:string$>")
|
2018-07-01 03:35:51 +03:00
|
|
|
@d.permission("note")
|
|
|
|
async noteCmd(msg: Message, args: any) {
|
2018-08-02 00:51:25 +03:00
|
|
|
const user = await this.bot.users.get(args.userId);
|
|
|
|
const userName = user ? `${user.username}#${user.discriminator}` : "member";
|
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.userId,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Note,
|
|
|
|
reason: args.note
|
|
|
|
});
|
|
|
|
|
2018-12-22 13:25:17 +02:00
|
|
|
msg.channel.createMessage(successMessage(`Note added on **${userName}** (Case #${createdCase.case_number})`));
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("warn", "<member:Member> <reason:string$>")
|
|
|
|
@d.permission("warn")
|
2018-10-26 06:41:20 +03:00
|
|
|
@d.nonBlocking()
|
2018-07-08 13:57:27 +03:00
|
|
|
async warnCmd(msg: Message, args: any) {
|
2018-07-30 01:44:03 +03:00
|
|
|
// Make sure we're allowed to warn this member
|
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot warn: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
const warnMessage = this.configValue("warn_message")
|
|
|
|
.replace("{guildName}", this.guild.name)
|
|
|
|
.replace("{reason}", args.reason);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
const messageSent = await this.tryToMessageUser(
|
|
|
|
args.member.user,
|
|
|
|
warnMessage,
|
|
|
|
this.configValue("dm_on_warn"),
|
|
|
|
this.configValue("message_on_warn")
|
|
|
|
);
|
2018-07-08 13:57:27 +03:00
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
if (!messageSent) {
|
2018-08-18 13:34:57 +03:00
|
|
|
const failedMsg = await msg.channel.createMessage("Failed to message the user. Log the warning anyway?");
|
2018-07-30 01:44:03 +03:00
|
|
|
const reply = await waitForReaction(this.bot, failedMsg, ["✅", "❌"], msg.author.id);
|
2018-07-09 02:51:34 +03:00
|
|
|
failedMsg.delete();
|
|
|
|
if (!reply || reply.name === "❌") {
|
|
|
|
return;
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase: Case = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Warn,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-08 13:57:27 +03:00
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
msg.channel.createMessage(
|
2018-12-22 13:22:16 +02:00
|
|
|
successMessage(
|
|
|
|
`Warned **${args.member.user.username}#${args.member.user.discriminator}** (Case #${createdCase.case_number})`
|
|
|
|
)
|
2018-08-02 00:51:25 +03:00
|
|
|
);
|
2018-07-09 02:51:34 +03:00
|
|
|
|
|
|
|
this.serverLogs.log(LogType.MEMBER_WARN, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-09 02:51:34 +03:00
|
|
|
member: stripObjectToScalars(args.member, ["user"])
|
|
|
|
});
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("mute", "<member:Member> [time:string] [reason:string$]")
|
|
|
|
@d.permission("mute")
|
|
|
|
async muteCmd(msg: Message, args: any) {
|
|
|
|
if (!this.configValue("mute_role")) {
|
2018-07-12 01:45:26 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot mute: no mute role specified"));
|
2018-07-08 13:57:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we're allowed to mute this member
|
2018-07-12 01:42:18 +03:00
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot mute: insufficient permissions"));
|
|
|
|
return;
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
let messageSent = true;
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
// Convert mute time from e.g. "2h30m" to milliseconds
|
|
|
|
const muteTime = args.time ? convertDelayStringToMS(args.time) : null;
|
2018-07-12 01:38:58 +03:00
|
|
|
const timeUntilUnmute = muteTime && humanizeDuration(muteTime);
|
|
|
|
|
2018-07-08 13:57:27 +03:00
|
|
|
if (muteTime == null && args.time) {
|
|
|
|
// Invalid muteTime -> assume it's actually part of the reason
|
|
|
|
args.reason = `${args.time} ${args.reason ? args.reason : ""}`.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply "muted" role
|
2018-07-29 18:46:49 +03:00
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_ROLE_ADD, args.member.id);
|
2018-11-25 17:04:26 +02:00
|
|
|
const mute: Mute = await this.actions.fire("mute", {
|
|
|
|
member: args.member,
|
|
|
|
muteTime
|
|
|
|
});
|
2018-07-08 13:57:27 +03:00
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
if (!mute) {
|
|
|
|
msg.channel.createMessage(errorMessage("Could not mute the user"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasOldCase = mute.case_id != null;
|
2018-08-05 00:58:03 +03:00
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
let theCase;
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
if (hasOldCase) {
|
2018-12-22 13:22:16 +02:00
|
|
|
theCase = await this.cases.find(mute.case_id);
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
if (args.reason) {
|
2018-11-25 17:04:26 +02:00
|
|
|
// Update old case
|
|
|
|
await this.actions.fire("createCaseNote", mute.case_id, {
|
|
|
|
modId: msg.author.id,
|
|
|
|
note: args.reason
|
|
|
|
});
|
2018-08-05 00:58:03 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-25 17:04:26 +02:00
|
|
|
// Create new case
|
2018-12-22 13:22:16 +02:00
|
|
|
theCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Mute,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
|
|
|
await this.mutes.setCaseId(args.member.id, theCase.id);
|
2018-08-05 00:58:03 +03:00
|
|
|
}
|
2018-07-08 13:57:27 +03:00
|
|
|
|
|
|
|
// Message the user informing them of the mute
|
2018-08-05 00:58:03 +03:00
|
|
|
// Don't message them if we're updating an old mute
|
|
|
|
if (args.reason && !hasOldCase) {
|
2018-08-18 13:34:57 +03:00
|
|
|
const template = muteTime ? this.configValue("timed_mute_message") : this.configValue("mute_message");
|
2018-07-12 01:38:58 +03:00
|
|
|
|
2018-07-12 01:45:26 +03:00
|
|
|
const muteMessage = formatTemplateString(template, {
|
|
|
|
guildName: this.guild.name,
|
|
|
|
reason: args.reason,
|
|
|
|
time: timeUntilUnmute
|
|
|
|
});
|
2018-07-08 13:57:27 +03:00
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
messageSent = await this.tryToMessageUser(
|
|
|
|
args.member.user,
|
|
|
|
muteMessage,
|
|
|
|
this.configValue("dm_on_mute"),
|
|
|
|
this.configValue("message_on_mute")
|
|
|
|
);
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
2018-07-12 01:20:20 +03:00
|
|
|
let response;
|
2018-07-08 13:57:27 +03:00
|
|
|
if (muteTime) {
|
2018-12-22 13:22:16 +02:00
|
|
|
response = `Muted **${args.member.user.username}#${
|
|
|
|
args.member.user.discriminator
|
|
|
|
}** for ${timeUntilUnmute} (Case #${theCase.case_number})`;
|
2018-07-08 13:57:27 +03:00
|
|
|
} else {
|
2018-12-22 13:22:16 +02:00
|
|
|
response = `Muted **${args.member.user.username}#${args.member.user.discriminator}** indefinitely (Case #${
|
|
|
|
theCase.case_number
|
|
|
|
})`;
|
2018-07-08 13:57:27 +03:00
|
|
|
}
|
2018-07-09 03:13:31 +03:00
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
if (!messageSent) response += " (failed to message user)";
|
|
|
|
msg.channel.createMessage(successMessage(response));
|
|
|
|
|
2018-07-12 02:03:07 +03:00
|
|
|
// Log the action
|
2018-07-09 03:13:31 +03:00
|
|
|
this.serverLogs.log(LogType.MEMBER_MUTE, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-09 03:13:31 +03:00
|
|
|
member: stripObjectToScalars(args.member, ["user"])
|
|
|
|
});
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
@d.command("unmute", "<member:Member> [time:string] [reason:string$]")
|
2018-07-13 00:10:20 +03:00
|
|
|
@d.permission("mute")
|
|
|
|
async unmuteCmd(msg: Message, args: any) {
|
|
|
|
if (!this.configValue("mute_role")) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot unmute: no mute role specified"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we're allowed to mute this member
|
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot unmute: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if they're muted in the first place
|
|
|
|
const mute = await this.mutes.findExistingMuteForUserId(args.member.id);
|
|
|
|
if (!mute) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot unmute: member is not muted"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
// Convert unmute time from e.g. "2h30m" to milliseconds
|
|
|
|
const unmuteTime = args.time ? convertDelayStringToMS(args.time) : null;
|
2018-07-13 00:10:20 +03:00
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
if (unmuteTime == null && args.time) {
|
|
|
|
// Invalid unmuteTime -> assume it's actually part of the reason
|
|
|
|
args.reason = `${args.time} ${args.reason ? args.reason : ""}`.trim();
|
|
|
|
}
|
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
// Create a case
|
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Unmute,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
|
|
|
|
2018-08-05 00:58:03 +03:00
|
|
|
if (unmuteTime) {
|
|
|
|
// If we have an unmute time, just update the old mute to expire in that time
|
|
|
|
const timeUntilUnmute = unmuteTime && humanizeDuration(unmuteTime);
|
2018-11-25 17:04:26 +02:00
|
|
|
await this.actions.fire("unmute", { member: args.member, unmuteTime });
|
2018-08-05 00:58:03 +03:00
|
|
|
args.reason = args.reason ? `Timed unmute: ${args.reason}` : "Timed unmute";
|
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
|
|
|
msg.channel.createMessage(
|
|
|
|
successMessage(
|
2018-12-22 13:22:16 +02:00
|
|
|
`Unmuting **${args.member.user.username}#${args.member.user.discriminator}** in ${timeUntilUnmute} (Case #${
|
|
|
|
createdCase.case_number
|
|
|
|
})`
|
2018-08-05 00:58:03 +03:00
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Otherwise remove "muted" role immediately
|
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_ROLE_REMOVE, args.member.id);
|
2018-11-25 17:04:26 +02:00
|
|
|
await this.actions.fire("unmute", { member: args.member });
|
2018-08-05 00:58:03 +03:00
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
|
|
|
msg.channel.createMessage(
|
2018-12-22 13:22:16 +02:00
|
|
|
successMessage(
|
|
|
|
`Unmuted **${args.member.user.username}#${args.member.user.discriminator}** (Case #${
|
|
|
|
createdCase.case_number
|
|
|
|
})`
|
|
|
|
)
|
2018-08-05 00:58:03 +03:00
|
|
|
);
|
|
|
|
}
|
2018-07-13 00:10:20 +03:00
|
|
|
|
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_UNMUTE, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-13 00:10:20 +03:00
|
|
|
member: stripObjectToScalars(args.member, ["user"])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-05 00:18:50 +03:00
|
|
|
@d.command("mutes")
|
|
|
|
@d.permission("view")
|
|
|
|
async mutesCmd(msg: Message) {
|
2018-11-25 17:04:26 +02:00
|
|
|
this.actions.fire("postMuteList", msg.channel);
|
2018-08-05 00:18:50 +03:00
|
|
|
}
|
|
|
|
|
2018-07-12 02:03:22 +03:00
|
|
|
@d.command("kick", "<member:Member> [reason:string$]")
|
|
|
|
@d.permission("kick")
|
2018-11-25 17:04:26 +02:00
|
|
|
async kickCmd(msg, args: { member: Member; reason: string }) {
|
2018-07-12 02:03:22 +03:00
|
|
|
// Make sure we're allowed to kick this member
|
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot kick: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to message the user *before* kicking them, as doing it after may not be possible
|
|
|
|
let messageSent = true;
|
|
|
|
if (args.reason) {
|
|
|
|
const kickMessage = formatTemplateString(this.configValue("kick_message"), {
|
|
|
|
guildName: this.guild.name,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
|
|
|
|
|
|
|
messageSent = await this.tryToMessageUser(
|
|
|
|
args.member.user,
|
|
|
|
kickMessage,
|
|
|
|
this.configValue("dm_on_kick"),
|
|
|
|
this.configValue("message_on_kick")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kick the user
|
2018-07-29 18:46:49 +03:00
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_KICK, args.member.id);
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoreEvent(IgnoredEventType.Kick, args.member.id);
|
2018-07-12 02:03:22 +03:00
|
|
|
args.member.kick(args.reason);
|
|
|
|
|
|
|
|
// Create a case for this action
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Kick,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-12 02:03:22 +03:00
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
2018-12-22 13:22:16 +02:00
|
|
|
let response = `Kicked **${args.member.user.username}#${args.member.user.discriminator}** (Case #${
|
|
|
|
createdCase.case_number
|
|
|
|
})`;
|
2018-07-12 02:03:22 +03:00
|
|
|
if (!messageSent) response += " (failed to message user)";
|
|
|
|
msg.channel.createMessage(successMessage(response));
|
|
|
|
|
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_KICK, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-12-22 12:26:54 +02:00
|
|
|
user: stripObjectToScalars(args.member.user)
|
2018-07-12 02:03:22 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("ban", "<member:Member> [reason:string$]")
|
|
|
|
@d.permission("ban")
|
|
|
|
async banCmd(msg, args) {
|
|
|
|
// Make sure we're allowed to ban this member
|
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot ban: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to message the user *before* banning them, as doing it after may not be possible
|
|
|
|
let messageSent = true;
|
|
|
|
if (args.reason) {
|
|
|
|
const banMessage = formatTemplateString(this.configValue("ban_message"), {
|
|
|
|
guildName: this.guild.name,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
|
|
|
|
|
|
|
messageSent = await this.tryToMessageUser(
|
|
|
|
args.member.user,
|
|
|
|
banMessage,
|
|
|
|
this.configValue("dm_on_ban"),
|
|
|
|
this.configValue("message_on_ban")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ban the user
|
2018-07-29 18:46:49 +03:00
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_BAN, args.member.id);
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoreEvent(IgnoredEventType.Ban, args.member.id);
|
2018-07-12 02:03:22 +03:00
|
|
|
args.member.ban(1, args.reason);
|
|
|
|
|
|
|
|
// Create a case for this action
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Ban,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-12 02:03:22 +03:00
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
2018-12-22 13:22:16 +02:00
|
|
|
let response = `Banned **${args.member.user.username}#${args.member.user.discriminator}** (Case #${
|
|
|
|
createdCase.case_number
|
|
|
|
})`;
|
2018-07-12 02:03:22 +03:00
|
|
|
if (!messageSent) response += " (failed to message user)";
|
|
|
|
msg.channel.createMessage(successMessage(response));
|
|
|
|
|
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_BAN, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-12 02:03:22 +03:00
|
|
|
member: stripObjectToScalars(args.member, ["user"])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.command("softban", "<member:Member> [reason:string$]")
|
|
|
|
@d.permission("ban")
|
|
|
|
async softbanCmd(msg, args) {
|
|
|
|
// Make sure we're allowed to ban this member
|
|
|
|
if (!this.canActOn(msg.member, args.member)) {
|
|
|
|
msg.channel.createMessage(errorMessage("Cannot ban: insufficient permissions"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Softban the user = ban, and immediately unban
|
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_BAN, args.member.id);
|
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, args.member.id);
|
|
|
|
this.ignoreEvent(IgnoredEventType.Ban, args.member.id);
|
|
|
|
this.ignoreEvent(IgnoredEventType.Unban, args.member.id);
|
|
|
|
|
|
|
|
await args.member.ban(1, args.reason);
|
|
|
|
await this.guild.unbanMember(args.member.id);
|
|
|
|
|
|
|
|
// Create a case for this action
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.member.id,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Softban,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-08-02 00:51:25 +03:00
|
|
|
|
|
|
|
// Confirm the action to the moderator
|
|
|
|
msg.channel.createMessage(
|
2018-12-22 13:22:16 +02:00
|
|
|
successMessage(
|
|
|
|
`Softbanned **${args.member.user.username}#${args.member.user.discriminator}** (Case #${
|
|
|
|
createdCase.case_number
|
|
|
|
})`
|
|
|
|
)
|
2018-08-02 00:51:25 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_SOFTBAN, {
|
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
|
|
|
member: stripObjectToScalars(args.member, ["user"])
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@d.command("unban", "<userId:userId> [reason:string$]")
|
2018-07-14 20:55:39 +03:00
|
|
|
@d.permission("ban")
|
2018-12-14 08:54:15 +02:00
|
|
|
async unbanCmd(msg: Message, args: { userId: string; reason: string }) {
|
2018-07-31 04:02:45 +03:00
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, args.userId);
|
2018-07-29 18:46:49 +03:00
|
|
|
|
2018-07-14 20:55:39 +03:00
|
|
|
try {
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoreEvent(IgnoredEventType.Unban, args.userId);
|
2018-08-01 00:52:44 +03:00
|
|
|
await this.guild.unbanMember(args.userId);
|
2018-07-14 20:55:39 +03:00
|
|
|
} catch (e) {
|
|
|
|
msg.channel.createMessage(errorMessage("Failed to unban member"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a case
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-12-14 08:54:15 +02:00
|
|
|
userId: args.userId,
|
2018-11-25 17:04:26 +02:00
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Unban,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-14 20:55:39 +03:00
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
// Confirm the action
|
|
|
|
msg.channel.createMessage(successMessage(`Member unbanned (Case #${createdCase.case_number})`));
|
|
|
|
|
2018-07-14 20:55:39 +03:00
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_UNBAN, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-14 20:55:39 +03:00
|
|
|
userId: args.userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.command("forceban", "<userId:userId> [reason:string$]")
|
2018-07-14 20:55:39 +03:00
|
|
|
@d.permission("ban")
|
|
|
|
async forcebanCmd(msg: Message, args: any) {
|
|
|
|
// If the user exists as a guild member, make sure we can act on them first
|
|
|
|
const member = this.guild.members.get(args.userId);
|
|
|
|
if (member && !this.canActOn(msg.member, member)) {
|
2018-08-18 13:34:57 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot forceban this user: insufficient permissions"));
|
2018-07-14 20:55:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:02:45 +03:00
|
|
|
this.ignoreEvent(IgnoredEventType.Ban, args.userId);
|
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_BAN, args.userId);
|
2018-07-29 18:46:49 +03:00
|
|
|
|
2018-07-14 20:55:39 +03:00
|
|
|
try {
|
|
|
|
await this.guild.banMember(args.userId, 1, args.reason);
|
|
|
|
} catch (e) {
|
|
|
|
msg.channel.createMessage(errorMessage("Failed to forceban member"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a case
|
2018-12-22 13:22:16 +02:00
|
|
|
const createdCase = await this.actions.fire("createCase", {
|
2018-11-25 17:04:26 +02:00
|
|
|
userId: args.userId,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Ban,
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-14 20:55:39 +03:00
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
// Confirm the action
|
|
|
|
msg.channel.createMessage(successMessage(`Member forcebanned (Case #${createdCase.case_number})`));
|
|
|
|
|
2018-07-14 20:55:39 +03:00
|
|
|
// Log the action
|
|
|
|
this.serverLogs.log(LogType.MEMBER_FORCEBAN, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-14 20:55:39 +03:00
|
|
|
userId: args.userId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-16 20:07:43 +03:00
|
|
|
@d.command("massban", "<userIds:string...>")
|
|
|
|
@d.permission("massban")
|
2018-10-26 06:41:20 +03:00
|
|
|
@d.nonBlocking()
|
2018-08-16 20:07:43 +03:00
|
|
|
async massbanCmd(msg: Message, args: { userIds: string[] }) {
|
2018-08-18 13:34:57 +03:00
|
|
|
// Limit to 100 users at once (arbitrary?)
|
|
|
|
if (args.userIds.length > 100) {
|
|
|
|
msg.channel.createMessage(errorMessage(`Can only massban max 100 users at once`));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for ban reason (cleaner this way instead of trying to cram it into the args)
|
2018-08-16 20:07:43 +03:00
|
|
|
msg.channel.createMessage("Ban reason? `cancel` to cancel");
|
|
|
|
const banReasonReply = await waitForReply(this.bot, msg.channel as TextChannel, msg.author.id);
|
2018-08-18 13:34:57 +03:00
|
|
|
if (!banReasonReply || !banReasonReply.content || banReasonReply.content.toLowerCase().trim() === "cancel") {
|
2018-08-16 20:07:43 +03:00
|
|
|
msg.channel.createMessage("Cancelled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const banReason = banReasonReply.content;
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
// Verify we can act on each of the users specified
|
2018-08-16 20:07:43 +03:00
|
|
|
for (const userId of args.userIds) {
|
|
|
|
const member = this.guild.members.get(userId);
|
|
|
|
if (member && !this.canActOn(msg.member, member)) {
|
2018-08-18 13:34:57 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot massban one or more users: insufficient permissions"));
|
2018-08-16 20:07:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
// Ignore automatic ban cases and logs for these users
|
|
|
|
// We'll create our own cases below and post a single "mass banned" log instead
|
2018-08-16 20:07:43 +03:00
|
|
|
args.userIds.forEach(userId => {
|
2018-08-18 13:34:57 +03:00
|
|
|
// Use longer timeouts since this can take a while
|
|
|
|
this.ignoreEvent(IgnoredEventType.Ban, userId, 120 * 1000);
|
|
|
|
this.serverLogs.ignoreLog(LogType.MEMBER_BAN, userId, 120 * 1000);
|
2018-08-16 20:07:43 +03:00
|
|
|
});
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
// Show a loading indicator since this can take a while
|
2018-08-16 20:07:43 +03:00
|
|
|
const loadingMsg = await msg.channel.createMessage("Banning...");
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
// Ban each user and count failed bans (if any)
|
2018-08-16 20:07:43 +03:00
|
|
|
const failedBans = [];
|
|
|
|
for (const userId of args.userIds) {
|
|
|
|
try {
|
|
|
|
await this.guild.banMember(userId);
|
2018-11-25 17:04:26 +02:00
|
|
|
|
|
|
|
await this.actions.fire("createCase", {
|
|
|
|
userId,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes.Ban,
|
|
|
|
reason: `Mass ban: ${banReason}`,
|
|
|
|
postInCaseLog: false
|
|
|
|
});
|
2018-08-16 20:07:43 +03:00
|
|
|
} catch (e) {
|
|
|
|
failedBans.push(userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 13:34:57 +03:00
|
|
|
// Clear loading indicator
|
2018-08-16 20:07:43 +03:00
|
|
|
loadingMsg.delete();
|
|
|
|
|
|
|
|
const successfulBanCount = args.userIds.length - failedBans.length;
|
|
|
|
if (successfulBanCount === 0) {
|
2018-08-18 13:34:57 +03:00
|
|
|
// All bans failed - don't create a log entry and notify the user
|
2018-08-16 20:07:43 +03:00
|
|
|
msg.channel.createMessage(errorMessage("All bans failed. Make sure the IDs are valid."));
|
|
|
|
} else {
|
2018-08-18 13:34:57 +03:00
|
|
|
// Some or all bans were successful. Create a log entry for the mass ban and notify the user.
|
2018-08-16 20:07:43 +03:00
|
|
|
this.serverLogs.log(LogType.MASSBAN, {
|
|
|
|
mod: stripObjectToScalars(msg.author),
|
|
|
|
count: successfulBanCount
|
|
|
|
});
|
|
|
|
|
|
|
|
if (failedBans.length) {
|
|
|
|
msg.channel.createMessage(
|
2018-08-18 13:34:57 +03:00
|
|
|
successMessage(`Banned ${successfulBanCount} users, ${failedBans.length} failed: ${failedBans.join(" ")}`)
|
2018-08-16 20:07:43 +03:00
|
|
|
);
|
|
|
|
} else {
|
2018-08-18 13:34:57 +03:00
|
|
|
msg.channel.createMessage(successMessage(`Banned ${successfulBanCount} users successfully`));
|
2018-08-16 20:07:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.command("addcase", "<type:string> <target:userId> [reason:string$]")
|
2018-07-14 20:55:39 +03:00
|
|
|
@d.permission("addcase")
|
|
|
|
async addcaseCmd(msg: Message, args: any) {
|
|
|
|
// Verify the user id is a valid snowflake-ish
|
2018-07-30 01:44:03 +03:00
|
|
|
if (!args.target.match(/^[0-9]{17,20}$/)) {
|
2018-07-14 20:55:39 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot add case: invalid user id"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user exists as a guild member, make sure we can act on them first
|
|
|
|
const member = this.guild.members.get(args.userId);
|
|
|
|
if (member && !this.canActOn(msg.member, member)) {
|
2018-08-18 13:34:57 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot add case on this user: insufficient permissions"));
|
2018-07-14 20:55:39 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the case type is valid
|
|
|
|
const type: string = args.type[0].toUpperCase() + args.type.slice(1).toLowerCase();
|
2018-10-26 06:41:20 +03:00
|
|
|
if (!CaseTypes[type]) {
|
2018-07-14 20:55:39 +03:00
|
|
|
msg.channel.createMessage(errorMessage("Cannot add case: invalid case type"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the case
|
2018-11-25 17:04:26 +02:00
|
|
|
const theCase: Case = await this.actions.fire("createCase", {
|
|
|
|
userId: args.target,
|
|
|
|
modId: msg.author.id,
|
|
|
|
type: CaseTypes[type],
|
|
|
|
reason: args.reason
|
|
|
|
});
|
2018-07-14 20:55:39 +03:00
|
|
|
|
2018-12-22 13:22:16 +02:00
|
|
|
msg.channel.createMessage(successMessage(`Case #${theCase.case_number} created`));
|
2018-11-25 17:04:26 +02:00
|
|
|
|
|
|
|
// Log the action
|
2018-07-14 20:55:39 +03:00
|
|
|
this.serverLogs.log(LogType.CASE_CREATE, {
|
2018-07-29 23:30:24 +03:00
|
|
|
mod: stripObjectToScalars(msg.member.user),
|
2018-07-29 18:46:49 +03:00
|
|
|
userId: args.userId,
|
|
|
|
caseNum: theCase.case_number,
|
|
|
|
caseType: type.toUpperCase()
|
2018-07-14 20:55:39 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-01 03:35:51 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.command(/showcase|case/, "<caseNumber:number>")
|
2018-07-01 03:35:51 +03:00
|
|
|
@d.permission("view")
|
2018-08-02 00:51:25 +03:00
|
|
|
async showcaseCmd(msg: Message, args: { caseNumber: number }) {
|
|
|
|
// Assume case id
|
|
|
|
const theCase = await this.cases.findByCaseNumber(args.caseNumber);
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
if (!theCase) {
|
2018-11-25 17:04:26 +02:00
|
|
|
msg.channel.createMessage(errorMessage("Case not found"));
|
2018-08-02 00:51:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-25 17:04:26 +02:00
|
|
|
await this.actions.fire("postCase", {
|
|
|
|
caseId: theCase.id,
|
|
|
|
channel: msg.channel
|
|
|
|
});
|
2018-08-02 00:51:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-02 03:01:22 +03:00
|
|
|
@d.command(/cases|usercases/, "<userId:userId> [expanded:string]")
|
2018-08-02 00:51:25 +03:00
|
|
|
@d.permission("view")
|
2018-08-02 03:01:22 +03:00
|
|
|
async usercasesCmd(msg: Message, args: { userId: string; expanded?: string }) {
|
2018-10-26 06:41:20 +03:00
|
|
|
const cases = await this.cases.with("notes").getByUserId(args.userId);
|
2018-08-02 00:51:25 +03:00
|
|
|
const user = this.bot.users.get(args.userId);
|
|
|
|
const userName = user ? `${user.username}#${user.discriminator}` : "Unknown#0000";
|
|
|
|
const prefix = this.knub.getGuildData(this.guildId).config.prefix;
|
|
|
|
|
|
|
|
if (cases.length === 0) {
|
|
|
|
msg.channel.createMessage("No cases found for the specified user!");
|
|
|
|
} else {
|
2018-08-02 03:03:59 +03:00
|
|
|
if (args.expanded && args.expanded.startsWith("expand")) {
|
2018-11-24 19:14:12 +02:00
|
|
|
if (cases.length > 8) {
|
|
|
|
msg.channel.createMessage("Too many cases for expanded view. Please use compact view instead.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 03:01:22 +03:00
|
|
|
// Expanded view (= individual case embeds)
|
|
|
|
for (const theCase of cases) {
|
2018-11-25 17:04:26 +02:00
|
|
|
await this.actions.fire("postCase", {
|
|
|
|
caseId: theCase.id,
|
|
|
|
channel: msg.channel
|
|
|
|
});
|
2018-08-02 02:46:57 +03:00
|
|
|
}
|
2018-08-02 03:01:22 +03:00
|
|
|
} else {
|
|
|
|
// Compact view (= regular message with a preview of each case)
|
|
|
|
const lines = [];
|
|
|
|
for (const theCase of cases) {
|
2018-10-26 06:41:20 +03:00
|
|
|
theCase.notes.sort((a, b) => (a.created_at > b.created_at ? 1 : -1));
|
|
|
|
const firstNote = theCase.notes[0];
|
2018-08-02 03:01:22 +03:00
|
|
|
let reason = firstNote ? firstNote.body : "";
|
|
|
|
|
|
|
|
if (reason.length > CASE_LIST_REASON_MAX_LENGTH) {
|
|
|
|
const match = reason.slice(CASE_LIST_REASON_MAX_LENGTH, 20).match(/(?:[.,!?\s]|$)/);
|
2018-08-18 13:34:57 +03:00
|
|
|
const nextWhitespaceIndex = match ? CASE_LIST_REASON_MAX_LENGTH + match.index : CASE_LIST_REASON_MAX_LENGTH;
|
2018-08-02 03:01:22 +03:00
|
|
|
if (nextWhitespaceIndex < reason.length) {
|
|
|
|
reason = reason.slice(0, nextWhitespaceIndex - 1) + "...";
|
|
|
|
}
|
|
|
|
}
|
2018-08-02 02:46:57 +03:00
|
|
|
|
2018-08-02 03:01:22 +03:00
|
|
|
reason = disableLinkPreviews(reason);
|
2018-08-02 02:46:57 +03:00
|
|
|
|
2018-10-26 06:41:20 +03:00
|
|
|
lines.push(`Case \`#${theCase.case_number}\` __${CaseTypes[theCase.type]}__ ${reason}`);
|
2018-08-02 03:01:22 +03:00
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
|
2018-08-02 03:01:22 +03:00
|
|
|
const finalMessage = trimLines(`
|
2018-08-02 00:51:25 +03:00
|
|
|
Cases for **${userName}**:
|
2018-11-24 19:14:12 +02:00
|
|
|
|
2018-08-02 00:51:25 +03:00
|
|
|
${lines.join("\n")}
|
2018-11-24 19:14:12 +02:00
|
|
|
|
|
|
|
Use \`${prefix}case <num>\` to see more info about individual cases
|
2018-08-02 00:51:25 +03:00
|
|
|
`);
|
|
|
|
|
2018-11-24 19:14:12 +02:00
|
|
|
const finalMessageChunks = chunkMessageLines(finalMessage);
|
|
|
|
for (const msgChunk of finalMessageChunks) {
|
|
|
|
msg.channel.createMessage(msgChunk);
|
|
|
|
}
|
2018-08-02 03:01:22 +03:00
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
/**
|
|
|
|
* Attempts to message the specified user through DMs and/or the message channel.
|
|
|
|
* Returns a promise that resolves to a boolean indicating whether we were able to message them or not.
|
|
|
|
*/
|
2018-08-18 13:34:57 +03:00
|
|
|
protected async tryToMessageUser(user: User, str: string, useDM: boolean, useChannel: boolean): Promise<boolean> {
|
2018-07-12 01:20:20 +03:00
|
|
|
let messageSent = false;
|
|
|
|
|
2018-07-12 01:43:11 +03:00
|
|
|
if (!useDM && !useChannel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 01:20:20 +03:00
|
|
|
if (useDM) {
|
|
|
|
try {
|
|
|
|
const dmChannel = await this.bot.getDMChannel(user.id);
|
|
|
|
await dmChannel.createMessage(str);
|
|
|
|
messageSent = true;
|
|
|
|
} catch (e) {} // tslint:disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useChannel && this.configValue("message_channel")) {
|
|
|
|
try {
|
2018-07-12 01:45:26 +03:00
|
|
|
const channel = this.guild.channels.get(this.configValue("message_channel")) as TextChannel;
|
2018-07-12 01:20:20 +03:00
|
|
|
await channel.createMessage(`<@!${user.id}> ${str}`);
|
|
|
|
messageSent = true;
|
|
|
|
} catch (e) {} // tslint:disable-line
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageSent;
|
|
|
|
}
|
2018-07-01 03:35:51 +03:00
|
|
|
}
|