3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-11 12:55:01 +00:00

Migrate Slowmode to new Plugin structure

This commit is contained in:
Dark 2020-07-23 00:25:40 +02:00
parent ebcb28261b
commit b4b8680431
14 changed files with 538 additions and 0 deletions

View file

@ -0,0 +1,40 @@
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { sendErrorMessage, sendSuccessMessage } from "src/pluginUtils";
import { slowmodeCmd } from "../types";
import { clearBotSlowmodeFromUserId } from "../util/clearBotSlowmodeFromUserId";
export const SlowmodeClearCmd = slowmodeCmd({
trigger: ["slowmode clear", "slowmode c"],
permission: "can_manage",
signature: {
channel: ct.textChannel(),
user: ct.resolvedUserLoose(),
force: ct.bool({ option: true, isSwitch: true }),
},
async run({ message: msg, args, pluginData }) {
const channelSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(args.channel.id);
if (!channelSlowmode) {
sendErrorMessage(pluginData, msg.channel, "Channel doesn't have slowmode!");
return;
}
try {
await clearBotSlowmodeFromUserId(pluginData, args.channel, args.user.id, args.force);
} catch (e) {
return sendErrorMessage(
pluginData,
msg.channel,
`Failed to clear slowmode from **${args.user.username}#${args.user.discriminator}** in <#${args.channel.id}>`,
);
}
sendSuccessMessage(
pluginData,
msg.channel,
`Slowmode cleared from **${args.user.username}#${args.user.discriminator}** in <#${args.channel.id}>`,
);
},
});

View file

@ -0,0 +1,20 @@
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({
trigger: ["slowmode disable", "slowmode d"],
permission: "can_manage",
signature: {
channel: ct.textChannel(),
},
async run({ message: msg, args, pluginData }) {
// Workaround until you can call this cmd from SlowmodeSetChannelCmd
actualDisableSlowmodeCmd(msg, args, pluginData);
},
});

View file

@ -0,0 +1,37 @@
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { slowmodeCmd } from "../types";
import { TextChannel } from "eris";
import humanizeDuration from "humanize-duration";
export const SlowmodeGetChannelCmd = slowmodeCmd({
trigger: "slowmode",
permission: "can_manage",
source: "guild",
signature: {
channel: ct.textChannel({ option: true }),
},
async run({ message: msg, args, pluginData }) {
const channel = args.channel || (msg.channel as TextChannel);
let currentSlowmode = channel.rateLimitPerUser;
let isNative = true;
if (!currentSlowmode) {
const botSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
if (botSlowmode) {
currentSlowmode = botSlowmode.slowmode_seconds;
isNative = false;
}
}
if (currentSlowmode) {
const humanized = humanizeDuration(channel.rateLimitPerUser * 1000);
const slowmodeType = isNative ? "native" : "bot-maintained";
msg.channel.createMessage(`The current slowmode of <#${channel.id}> is **${humanized}** (${slowmodeType})`);
} else {
msg.channel.createMessage("Channel is not on slowmode");
}
},
});

View file

@ -0,0 +1,46 @@
import { slowmodeCmd } from "../types";
import { GuildChannel, TextChannel } from "eris";
import { createChunkedMessage } from "knub/dist/helpers";
import { errorMessage } from "src/utils";
import humanizeDuration from "humanize-duration";
export const SlowmodeListCmd = slowmodeCmd({
trigger: ["slowmode list", "slowmode l", "slowmodes"],
permission: "can_manage",
async run({ message: msg, pluginData }) {
const channels = pluginData.guild.channels;
const slowmodes: Array<{ channel: GuildChannel; seconds: number; native: boolean }> = [];
for (const channel of channels.values()) {
if (!(channel instanceof TextChannel)) continue;
// Bot slowmode
const botSlowmode = await pluginData.state.slowmodes.getChannelSlowmode(channel.id);
if (botSlowmode) {
slowmodes.push({ channel, seconds: botSlowmode.slowmode_seconds, native: false });
continue;
}
// Native slowmode
if (channel.rateLimitPerUser) {
slowmodes.push({ channel, seconds: channel.rateLimitPerUser, native: true });
continue;
}
}
if (slowmodes.length) {
const lines = slowmodes.map(slowmode => {
const humanized = humanizeDuration(slowmode.seconds * 1000);
const type = slowmode.native ? "native slowmode" : "bot slowmode";
return `<#${slowmode.channel.id}> **${humanized}** ${type}`;
});
createChunkedMessage(msg.channel, lines.join("\n"));
} else {
msg.channel.createMessage(errorMessage("No active slowmodes!"));
}
},
});

View file

@ -0,0 +1,93 @@
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})`,
);
},
});