mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-11 12:55:01 +00:00
slowmode: fix bot slowmodes not being applied for slowmodes over 6h; add -mode option; add eager permission checks
This commit is contained in:
parent
805e275f5b
commit
bb823b6274
12 changed files with 227 additions and 110 deletions
|
@ -2,6 +2,10 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
|
|||
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
|
||||
import { slowmodeCmd } from "../types";
|
||||
import { clearBotSlowmodeFromUserId } from "../util/clearBotSlowmodeFromUserId";
|
||||
import { asSingleLine, disableInlineCode } from "../../../utils";
|
||||
import { getMissingChannelPermissions } from "../../../utils/getMissingChannelPermissions";
|
||||
import { BOT_SLOWMODE_CLEAR_PERMISSIONS } from "../requiredPermissions";
|
||||
import { missingPermissionError } from "../../../utils/missingPermissionError";
|
||||
|
||||
export const SlowmodeClearCmd = slowmodeCmd({
|
||||
trigger: ["slowmode clear", "slowmode c"],
|
||||
|
@ -21,14 +25,29 @@ export const SlowmodeClearCmd = slowmodeCmd({
|
|||
return;
|
||||
}
|
||||
|
||||
const me = pluginData.guild.members.get(pluginData.client.user.id);
|
||||
const missingPermissions = getMissingChannelPermissions(me, args.channel, BOT_SLOWMODE_CLEAR_PERMISSIONS);
|
||||
if (missingPermissions) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Unable to clear slowmode. ${missingPermissionError(missingPermissions)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await clearBotSlowmodeFromUserId(pluginData, args.channel, args.user.id, args.force);
|
||||
} catch (e) {
|
||||
return sendErrorMessage(
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Failed to clear slowmode from **${args.user.username}#${args.user.discriminator}** in <#${args.channel.id}>`,
|
||||
asSingleLine(`
|
||||
Failed to clear slowmode from **${args.user.username}#${args.user.discriminator}** in <#${args.channel.id}>:
|
||||
\`${disableInlineCode(e.message)}\`
|
||||
`),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
sendSuccessMessage(
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
|
||||
import { slowmodeCmd } from "../types";
|
||||
import { disableBotSlowmodeForChannel } from "../util/disableBotSlowmodeForChannel";
|
||||
import { noop } from "src/utils";
|
||||
import { actualDisableSlowmodeCmd } from "../util/actualDisableSlowmodeCmd";
|
||||
|
||||
export const SlowmodeDisableCmd = slowmodeCmd({
|
||||
|
|
|
@ -3,7 +3,7 @@ import { slowmodeCmd } from "../types";
|
|||
import { TextChannel } from "eris";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
|
||||
export const SlowmodeGetChannelCmd = slowmodeCmd({
|
||||
export const SlowmodeGetCmd = slowmodeCmd({
|
||||
trigger: "slowmode",
|
||||
permission: "can_manage",
|
||||
source: "guild",
|
|
@ -1,93 +0,0 @@
|
|||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { slowmodeCmd } from "../types";
|
||||
import { TextChannel } from "eris";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
|
||||
import { convertDelayStringToMS, HOURS, DAYS } from "src/utils";
|
||||
import { disableBotSlowmodeForChannel } from "../util/disableBotSlowmodeForChannel";
|
||||
import { actualDisableSlowmodeCmd } from "../util/actualDisableSlowmodeCmd";
|
||||
|
||||
const NATIVE_SLOWMODE_LIMIT = 6 * HOURS; // 6 hours
|
||||
const MAX_SLOWMODE = DAYS * 365 * 100; // 100 years
|
||||
|
||||
export const SlowmodeSetChannelCmd = slowmodeCmd({
|
||||
trigger: "slowmode",
|
||||
permission: "can_manage",
|
||||
source: "guild",
|
||||
|
||||
// prettier-ignore
|
||||
signature: [
|
||||
{
|
||||
time: ct.string(),
|
||||
},
|
||||
{
|
||||
channel: ct.textChannel(),
|
||||
time: ct.string(),
|
||||
}
|
||||
],
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const channel = args.channel || msg.channel;
|
||||
|
||||
if (channel == null || !(channel instanceof TextChannel)) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Channel must be a text channel");
|
||||
return;
|
||||
}
|
||||
|
||||
const seconds = Math.ceil(convertDelayStringToMS(args.time, "s") / 1000);
|
||||
const useNativeSlowmode =
|
||||
pluginData.config.getForChannel(channel).use_native_slowmode && seconds <= NATIVE_SLOWMODE_LIMIT;
|
||||
|
||||
if (seconds === 0) {
|
||||
// Workaround until we can call SlowmodeDisableCmd from here
|
||||
return actualDisableSlowmodeCmd(msg, { channel }, pluginData);
|
||||
}
|
||||
|
||||
if (seconds > MAX_SLOWMODE) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Sorry, slowmodes can be at most 100 years long. Maybe 99 would be enough?`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (useNativeSlowmode) {
|
||||
// Native slowmode
|
||||
|
||||
// If there is an existing bot-maintained slowmode, disable that first
|
||||
const existingBotSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
|
||||
if (existingBotSlowmode) {
|
||||
await disableBotSlowmodeForChannel(pluginData, channel);
|
||||
}
|
||||
|
||||
// Set slowmode
|
||||
try {
|
||||
await channel.edit({
|
||||
rateLimitPerUser: seconds,
|
||||
});
|
||||
} catch (e) {
|
||||
return sendErrorMessage(pluginData, msg.channel, "Failed to set native slowmode (check permissions)");
|
||||
}
|
||||
} else {
|
||||
// Bot-maintained slowmode
|
||||
|
||||
// If there is an existing native slowmode, disable that first
|
||||
if (channel.rateLimitPerUser) {
|
||||
await channel.edit({
|
||||
rateLimitPerUser: 0,
|
||||
});
|
||||
}
|
||||
|
||||
await pluginData.state.slowmodes.setChannelSlowmode(channel.id, seconds);
|
||||
}
|
||||
|
||||
const humanizedSlowmodeTime = humanizeDuration(seconds * 1000);
|
||||
const slowmodeType = useNativeSlowmode ? "native slowmode" : "bot-maintained slowmode";
|
||||
sendSuccessMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Set ${humanizedSlowmodeTime} slowmode for <#${channel.id}> (${slowmodeType})`,
|
||||
);
|
||||
},
|
||||
});
|
154
backend/src/plugins/Slowmode/commands/SlowmodeSetCmd.ts
Normal file
154
backend/src/plugins/Slowmode/commands/SlowmodeSetCmd.ts
Normal file
|
@ -0,0 +1,154 @@
|
|||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { slowmodeCmd } from "../types";
|
||||
import { TextChannel } from "eris";
|
||||
import humanizeDuration from "humanize-duration";
|
||||
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
|
||||
import { asSingleLine, DAYS, disableInlineCode, HOURS, MINUTES } from "src/utils";
|
||||
import { disableBotSlowmodeForChannel } from "../util/disableBotSlowmodeForChannel";
|
||||
import { actualDisableSlowmodeCmd } from "../util/actualDisableSlowmodeCmd";
|
||||
import { getMissingPermissions } from "../../../utils/getMissingPermissions";
|
||||
import { missingPermissionError } from "../../../utils/missingPermissionError";
|
||||
import { BOT_SLOWMODE_PERMISSIONS, NATIVE_SLOWMODE_PERMISSIONS } from "../requiredPermissions";
|
||||
|
||||
const MAX_NATIVE_SLOWMODE = 6 * HOURS; // 6 hours
|
||||
const MAX_BOT_SLOWMODE = DAYS * 365 * 100; // 100 years
|
||||
const MIN_BOT_SLOWMODE = 15 * MINUTES;
|
||||
|
||||
const validModes = ["bot", "native"];
|
||||
type TMode = "bot" | "native";
|
||||
|
||||
export const SlowmodeSetCmd = slowmodeCmd({
|
||||
trigger: "slowmode",
|
||||
permission: "can_manage",
|
||||
source: "guild",
|
||||
|
||||
// prettier-ignore
|
||||
signature: [
|
||||
{
|
||||
time: ct.delay(),
|
||||
|
||||
mode: ct.string({ option: true, shortcut: "m" }),
|
||||
},
|
||||
{
|
||||
channel: ct.textChannel(),
|
||||
time: ct.delay(),
|
||||
|
||||
mode: ct.string({ option: true, shortcut: "m" }),
|
||||
}
|
||||
],
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const channel: TextChannel = args.channel || msg.channel;
|
||||
|
||||
if (args.time === 0) {
|
||||
// Workaround until we can call SlowmodeDisableCmd from here
|
||||
return actualDisableSlowmodeCmd(msg, { channel }, pluginData);
|
||||
}
|
||||
|
||||
const defaultMode: TMode =
|
||||
pluginData.config.getForChannel(channel).use_native_slowmode && args.time <= MAX_NATIVE_SLOWMODE
|
||||
? "native"
|
||||
: "bot";
|
||||
|
||||
const mode = (args.mode as TMode) || defaultMode;
|
||||
if (!validModes.includes(mode)) {
|
||||
sendErrorMessage(pluginData, msg.channel, "--mode must be 'bot' or 'native'");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate durations
|
||||
if (mode === "native" && args.time > MAX_NATIVE_SLOWMODE) {
|
||||
sendErrorMessage(pluginData, msg.channel, "Native slowmode can only be set to 6h or less");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode === "bot" && args.time > MAX_BOT_SLOWMODE) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Sorry, bot managed slowmodes can be at most 100 years long. Maybe 99 would be enough?`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode === "bot" && args.time < MIN_BOT_SLOWMODE) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
asSingleLine(`
|
||||
Bot managed slowmode must be 15min or more.
|
||||
Use \`--mode native\` to use native slowmodes for short slowmodes instead.
|
||||
`),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Verify permissions
|
||||
const channelPermissions = channel.permissionsOf(pluginData.client.user.id);
|
||||
|
||||
if (mode === "native") {
|
||||
const missingPermissions = getMissingPermissions(channelPermissions, NATIVE_SLOWMODE_PERMISSIONS);
|
||||
if (missingPermissions) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Unable to set native slowmode. ${missingPermissionError(missingPermissions)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mode === "bot") {
|
||||
const missingPermissions = getMissingPermissions(channelPermissions, BOT_SLOWMODE_PERMISSIONS);
|
||||
if (missingPermissions) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Unable to set bot managed slowmode. ${missingPermissionError(missingPermissions)}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the slowmode!
|
||||
const rateLimitSeconds = Math.ceil(args.time / 1000);
|
||||
|
||||
if (mode === "native") {
|
||||
// If there is an existing bot-maintained slowmode, disable that first
|
||||
const existingBotSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
|
||||
if (existingBotSlowmode) {
|
||||
await disableBotSlowmodeForChannel(pluginData, channel);
|
||||
}
|
||||
|
||||
// Set native slowmode
|
||||
try {
|
||||
await channel.edit({
|
||||
rateLimitPerUser: rateLimitSeconds,
|
||||
});
|
||||
} catch (e) {
|
||||
return sendErrorMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Failed to set native slowmode: ${disableInlineCode(e.message)}`,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// If there is an existing native slowmode, disable that first
|
||||
if (channel.rateLimitPerUser) {
|
||||
await channel.edit({
|
||||
rateLimitPerUser: 0,
|
||||
});
|
||||
}
|
||||
|
||||
await pluginData.state.slowmodes.setChannelSlowmode(channel.id, rateLimitSeconds);
|
||||
}
|
||||
|
||||
const humanizedSlowmodeTime = humanizeDuration(args.time);
|
||||
const slowmodeType = mode === "native" ? "native slowmode" : "bot-maintained slowmode";
|
||||
sendSuccessMessage(
|
||||
pluginData,
|
||||
msg.channel,
|
||||
`Set ${humanizedSlowmodeTime} slowmode for <#${channel.id}> (${slowmodeType})`,
|
||||
);
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue