2020-10-01 01:43:38 +03:00
|
|
|
import { modActionsCmd, IgnoredEventType } from "../types";
|
2020-07-24 02:25:33 +02:00
|
|
|
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
|
|
|
import { canActOn, sendErrorMessage, hasPermission, sendSuccessMessage } from "../../../pluginUtils";
|
2021-01-28 00:20:55 +01:00
|
|
|
import { resolveUser, resolveMember, stripObjectToScalars, noop } from "../../../utils";
|
2020-07-24 02:25:33 +02:00
|
|
|
import { isBanned } from "../functions/isBanned";
|
|
|
|
import { readContactMethodsFromArgs } from "../functions/readContactMethodsFromArgs";
|
|
|
|
import { formatReasonWithAttachments } from "../functions/formatReasonWithAttachments";
|
|
|
|
import { banUserId } from "../functions/banUserId";
|
2020-12-22 22:13:08 +02:00
|
|
|
import { getMemberLevel, waitForReaction } from "knub/dist/helpers";
|
2021-01-28 00:20:55 +01:00
|
|
|
import humanizeDuration from "humanize-duration";
|
2021-04-02 15:43:13 +02:00
|
|
|
import { CasesPlugin } from "../../../plugins/Cases/CasesPlugin";
|
|
|
|
import { CaseTypes } from "../../../data/CaseTypes";
|
|
|
|
import { LogType } from "../../../data/LogType";
|
|
|
|
import { banLock } from "../../../utils/lockNameHelpers";
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
|
|
const opts = {
|
|
|
|
mod: ct.member({ option: true }),
|
|
|
|
notify: ct.string({ option: true }),
|
|
|
|
"notify-channel": ct.textChannel({ option: true }),
|
2020-07-30 12:19:17 +03:00
|
|
|
"delete-days": ct.number({ option: true, shortcut: "d" }),
|
2020-07-24 02:25:33 +02:00
|
|
|
};
|
|
|
|
|
2020-10-01 01:43:38 +03:00
|
|
|
export const BanCmd = modActionsCmd({
|
2020-07-24 02:25:33 +02:00
|
|
|
trigger: "ban",
|
|
|
|
permission: "can_ban",
|
2021-01-28 00:20:55 +01:00
|
|
|
description: "Ban or Tempban the specified member",
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
|
|
signature: [
|
2021-01-28 00:20:55 +01:00
|
|
|
{
|
|
|
|
user: ct.string(),
|
|
|
|
time: ct.delay(),
|
|
|
|
reason: ct.string({ required: false, catchAll: true }),
|
|
|
|
|
|
|
|
...opts,
|
|
|
|
},
|
2020-07-24 02:25:33 +02:00
|
|
|
{
|
|
|
|
user: ct.string(),
|
|
|
|
reason: ct.string({ required: false, catchAll: true }),
|
|
|
|
|
|
|
|
...opts,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
|
|
|
|
async run({ pluginData, message: msg, args }) {
|
|
|
|
const user = await resolveUser(pluginData.client, args.user);
|
2020-12-17 04:12:49 +02:00
|
|
|
if (!user.id) {
|
2021-01-17 21:21:18 +02:00
|
|
|
sendErrorMessage(pluginData, msg.channel, `User not found`);
|
|
|
|
return;
|
2020-12-17 04:12:49 +02:00
|
|
|
}
|
2021-01-28 00:20:55 +01:00
|
|
|
const time = args["time"] ? args["time"] : null;
|
2020-07-24 02:25:33 +02:00
|
|
|
|
2021-01-28 00:20:55 +01:00
|
|
|
const reason = formatReasonWithAttachments(args.reason, msg.attachments);
|
2020-07-24 02:25:33 +02:00
|
|
|
const memberToBan = await resolveMember(pluginData.client, pluginData.guild, user.id);
|
2021-01-28 00:20:55 +01:00
|
|
|
// The moderator who did the action is the message author or, if used, the specified -mod
|
|
|
|
let mod = msg.member;
|
|
|
|
if (args.mod) {
|
|
|
|
if (!hasPermission(pluginData, "can_act_as_other", { message: msg, channelId: msg.channel.id })) {
|
2021-04-02 14:43:52 +01:00
|
|
|
sendErrorMessage(pluginData, msg.channel, "You don't have permission to use -mod");
|
2021-01-28 00:20:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mod = args.mod;
|
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
|
2021-01-28 00:20:55 +01:00
|
|
|
// acquire a lock because of the needed user-inputs below (if banned/not on server)
|
2021-04-02 15:43:13 +02:00
|
|
|
const lock = await pluginData.locks.acquire(banLock(user));
|
2020-11-20 02:31:31 +01:00
|
|
|
let forceban = false;
|
2021-01-28 00:20:55 +01:00
|
|
|
const existingTempban = await pluginData.state.tempbans.findExistingTempbanForUserId(user.id);
|
2020-07-24 02:25:33 +02:00
|
|
|
if (!memberToBan) {
|
2021-04-28 21:24:56 +03:00
|
|
|
const banned = await isBanned(pluginData, user.id);
|
2020-07-24 02:25:33 +02:00
|
|
|
if (banned) {
|
2021-01-28 00:20:55 +01:00
|
|
|
// Abort if trying to ban user indefinitely if they are already banned indefinitely
|
|
|
|
if (!existingTempban && !time) {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, `User is already banned indefinitely.`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the mod if we should update the existing ban
|
|
|
|
const alreadyBannedMsg = await msg.channel.createMessage("User is already banned, update ban?");
|
|
|
|
const reply = await waitForReaction(pluginData.client, alreadyBannedMsg, ["✅", "❌"], msg.author.id);
|
|
|
|
|
|
|
|
alreadyBannedMsg.delete().catch(noop);
|
|
|
|
if (!reply || reply.name === "❌") {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, "User already banned, update cancelled by moderator");
|
|
|
|
lock.unlock();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Update or add new tempban / remove old tempban
|
|
|
|
if (time && time > 0) {
|
|
|
|
if (existingTempban) {
|
|
|
|
pluginData.state.tempbans.updateExpiryTime(user.id, time, mod.id);
|
|
|
|
} else {
|
|
|
|
pluginData.state.tempbans.addTempban(user.id, time, mod.id);
|
|
|
|
}
|
|
|
|
} else if (existingTempban) {
|
|
|
|
pluginData.state.tempbans.clear(user.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new case for the updated ban since we never stored the old case id and log the action
|
|
|
|
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
|
|
|
const createdCase = await casesPlugin.createCase({
|
|
|
|
modId: mod.id,
|
|
|
|
type: CaseTypes.Ban,
|
|
|
|
userId: user.id,
|
|
|
|
reason,
|
|
|
|
noteDetails: [`Ban updated to ${time ? humanizeDuration(time) : "indefinite"}`],
|
|
|
|
});
|
|
|
|
const logtype = time ? LogType.MEMBER_TIMED_BAN : LogType.MEMBER_BAN;
|
|
|
|
pluginData.state.serverLogs.log(logtype, {
|
|
|
|
mod: stripObjectToScalars(mod.user),
|
|
|
|
user: stripObjectToScalars(user),
|
|
|
|
caseNumber: createdCase.case_number,
|
|
|
|
reason,
|
|
|
|
banTime: time ? humanizeDuration(time) : null,
|
|
|
|
});
|
|
|
|
|
|
|
|
sendSuccessMessage(
|
|
|
|
pluginData,
|
|
|
|
msg.channel,
|
|
|
|
`Ban updated to ${time ? "expire in " + humanizeDuration(time) + " from now" : "indefinite"}`,
|
|
|
|
);
|
|
|
|
lock.unlock();
|
|
|
|
return;
|
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
} else {
|
2020-11-20 02:31:31 +01:00
|
|
|
// Ask the mod if we should upgrade to a forceban as the user is not on the server
|
|
|
|
const notOnServerMsg = await msg.channel.createMessage("User not found on the server, forceban instead?");
|
|
|
|
const reply = await waitForReaction(pluginData.client, notOnServerMsg, ["✅", "❌"], msg.author.id);
|
|
|
|
|
2021-01-28 00:20:55 +01:00
|
|
|
notOnServerMsg.delete().catch(noop);
|
2020-11-20 02:31:31 +01:00
|
|
|
if (!reply || reply.name === "❌") {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, "User not on server, ban cancelled by moderator");
|
2021-01-28 00:20:55 +01:00
|
|
|
lock.unlock();
|
2020-11-20 02:31:31 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
forceban = true;
|
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 02:31:31 +01:00
|
|
|
// Make sure we're allowed to ban this member if they are on the server
|
|
|
|
if (!forceban && !canActOn(pluginData, msg.member, memberToBan!)) {
|
2020-12-22 22:13:08 +02:00
|
|
|
const ourLevel = getMemberLevel(pluginData, msg.member);
|
|
|
|
const targetLevel = getMemberLevel(pluginData, memberToBan!);
|
|
|
|
sendErrorMessage(
|
|
|
|
pluginData,
|
|
|
|
msg.channel,
|
|
|
|
`Cannot ban: target permission level is equal or higher to yours, ${targetLevel} >= ${ourLevel}`,
|
|
|
|
);
|
2021-01-28 00:20:55 +01:00
|
|
|
lock.unlock();
|
2020-07-24 02:25:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let contactMethods;
|
|
|
|
try {
|
|
|
|
contactMethods = readContactMethodsFromArgs(args);
|
|
|
|
} catch (e) {
|
|
|
|
sendErrorMessage(pluginData, msg.channel, e.message);
|
2021-01-28 00:20:55 +01:00
|
|
|
lock.unlock();
|
2020-07-24 02:25:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteMessageDays = args["delete-days"] ?? pluginData.config.getForMessage(msg).ban_delete_message_days;
|
2021-01-28 00:20:55 +01:00
|
|
|
const banResult = await banUserId(
|
|
|
|
pluginData,
|
|
|
|
user.id,
|
|
|
|
reason,
|
|
|
|
{
|
|
|
|
contactMethods,
|
|
|
|
caseArgs: {
|
|
|
|
modId: mod.id,
|
|
|
|
ppId: mod.id !== msg.author.id ? msg.author.id : undefined,
|
|
|
|
},
|
|
|
|
deleteMessageDays,
|
2020-07-24 02:25:33 +02:00
|
|
|
},
|
2021-01-28 00:20:55 +01:00
|
|
|
time,
|
|
|
|
);
|
2020-07-24 02:25:33 +02:00
|
|
|
|
|
|
|
if (banResult.status === "failed") {
|
2020-12-22 22:10:41 +02:00
|
|
|
sendErrorMessage(pluginData, msg.channel, `Failed to ban member: ${banResult.error}`);
|
2021-01-28 00:20:55 +01:00
|
|
|
lock.unlock();
|
2020-07-24 02:25:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 00:20:55 +01:00
|
|
|
let forTime = "";
|
|
|
|
if (time && time > 0) {
|
|
|
|
if (existingTempban) {
|
|
|
|
pluginData.state.tempbans.updateExpiryTime(user.id, time, mod.id);
|
|
|
|
} else {
|
|
|
|
pluginData.state.tempbans.addTempban(user.id, time, mod.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
forTime = `for ${humanizeDuration(time)} `;
|
|
|
|
}
|
|
|
|
|
2020-07-24 02:25:33 +02:00
|
|
|
// Confirm the action to the moderator
|
2020-11-20 02:31:31 +01:00
|
|
|
let response = "";
|
|
|
|
if (!forceban) {
|
2021-01-28 00:20:55 +01:00
|
|
|
response = `Banned **${user.username}#${user.discriminator}** ${forTime}(Case #${banResult.case.case_number})`;
|
2020-11-20 02:31:31 +01:00
|
|
|
if (banResult.notifyResult.text) response += ` (${banResult.notifyResult.text})`;
|
|
|
|
} else {
|
2021-01-28 00:20:55 +01:00
|
|
|
response = `Member forcebanned ${forTime}(Case #${banResult.case.case_number})`;
|
2020-11-20 02:31:31 +01:00
|
|
|
}
|
2020-07-24 02:25:33 +02:00
|
|
|
|
2021-01-28 00:20:55 +01:00
|
|
|
lock.unlock();
|
2020-07-24 02:25:33 +02:00
|
|
|
sendSuccessMessage(pluginData, msg.channel, response);
|
|
|
|
},
|
|
|
|
});
|