zappyzep/src/plugins/Slowmode.ts

340 lines
12 KiB
TypeScript
Raw Normal View History

import { decorators as d, IPluginOptions } from "knub";
import { GuildChannel, Message, TextChannel, Constants as ErisConstants, User } from "eris";
import { convertDelayStringToMS, createChunkedMessage, errorMessage, noop, successMessage } from "../utils";
2018-12-15 17:04:04 +02:00
import { GuildSlowmodes } from "../data/GuildSlowmodes";
import humanizeDuration from "humanize-duration";
import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildSavedMessages } from "../data/GuildSavedMessages";
2018-12-15 17:04:04 +02:00
2019-04-15 14:01:49 +03:00
const NATIVE_SLOWMODE_LIMIT = 6 * 60 * 60; // 6 hours
const MAX_SLOWMODE = 60 * 60 * 24 * 365 * 100; // 100 years
interface ISlowmodePluginConfig {
use_native_slowmode: boolean;
can_manage: boolean;
is_affected: boolean;
}
export class SlowmodePlugin extends ZeppelinPlugin<ISlowmodePluginConfig> {
2019-01-13 23:37:53 +02:00
public static pluginName = "slowmode";
2018-12-15 17:04:04 +02:00
protected slowmodes: GuildSlowmodes;
protected savedMessages: GuildSavedMessages;
2018-12-15 17:04:04 +02:00
protected clearInterval;
private onMessageCreateFn;
getDefaultOptions(): IPluginOptions<ISlowmodePluginConfig> {
2018-12-15 17:04:04 +02:00
return {
config: {
use_native_slowmode: true,
can_manage: false,
is_affected: true,
2018-12-15 17:04:04 +02:00
},
overrides: [
{
level: ">=50",
config: {
can_manage: true,
is_affected: false,
},
},
],
2018-12-15 17:04:04 +02:00
};
}
onLoad() {
this.slowmodes = GuildSlowmodes.getInstance(this.guildId);
this.savedMessages = GuildSavedMessages.getInstance(this.guildId);
2018-12-15 17:04:04 +02:00
this.clearInterval = setInterval(() => this.clearExpiredSlowmodes(), 2000);
this.onMessageCreateFn = this.onMessageCreate.bind(this);
this.savedMessages.events.on("create", this.onMessageCreateFn);
2018-12-15 17:04:04 +02:00
}
onUnload() {
clearInterval(this.clearInterval);
this.savedMessages.events.off("create", this.onMessageCreateFn);
2018-12-15 17:04:04 +02:00
}
/**
* Applies a bot-maintained slowmode to the specified user id on the specified channel.
2018-12-15 17:04:04 +02:00
* This sets the channel permissions so the user is unable to send messages there, and saves the slowmode in the db.
*/
async applyBotSlowmodeToUserId(channel: GuildChannel & TextChannel, userId: string) {
2018-12-15 17:04:04 +02:00
// Deny sendMessage permission from the user. If there are existing permission overwrites, take those into account.
const existingOverride = channel.permissionOverwrites.get(userId);
const newDeniedPermissions =
(existingOverride ? existingOverride.deny : 0) | ErisConstants.Permissions.sendMessages;
const newAllowedPermissions =
(existingOverride ? existingOverride.allow : 0) & ~ErisConstants.Permissions.sendMessages;
await channel.editPermission(userId, newAllowedPermissions, newDeniedPermissions, "member");
await this.slowmodes.addSlowmodeUser(channel.id, userId);
}
/**
* Clears bot-maintained slowmode from the specified user id on the specified channel.
2018-12-15 17:04:04 +02:00
* This reverts the channel permissions changed above and clears the database entry.
*/
async clearBotSlowmodeFromUserId(channel: GuildChannel & TextChannel, userId: string) {
2018-12-15 17:04:04 +02:00
// We only need to tweak permissions if there is an existing permission override
// In most cases there should be, since one is created in applySlowmodeToUserId()
const existingOverride = channel.permissionOverwrites.get(userId);
if (existingOverride) {
if (existingOverride.allow === 0 && existingOverride.deny === ErisConstants.Permissions.sendMessages) {
// If the only override for this user is what we applied earlier, remove the entire permission overwrite
await channel.deletePermission(userId);
} else {
// Otherwise simply negate the sendMessages permission from the denied permissions
const newDeniedPermissions = existingOverride.deny & ~ErisConstants.Permissions.sendMessages;
await channel.editPermission(userId, existingOverride.allow, newDeniedPermissions, "member");
}
}
await this.slowmodes.clearSlowmodeUser(channel.id, userId);
}
/**
* Disable slowmode on the specified channel. Clears any existing slowmode perms.
2018-12-15 17:04:04 +02:00
*/
async disableBotSlowmodeForChannel(channel: GuildChannel & TextChannel) {
2018-12-15 17:04:04 +02:00
// Disable channel slowmode
await this.slowmodes.deleteChannelSlowmode(channel.id);
2018-12-15 17:04:04 +02:00
// Remove currently applied slowmodes
const users = await this.slowmodes.getChannelSlowmodeUsers(channel.id);
2018-12-15 17:04:04 +02:00
const failedUsers = [];
for (const slowmodeUser of users) {
try {
await this.clearBotSlowmodeFromUserId(channel, slowmodeUser.user_id);
2018-12-15 17:04:04 +02:00
} catch (e) {
// Removing the slowmode failed. Record this so the permissions can be changed manually, and remove the database entry.
failedUsers.push(slowmodeUser.user_id);
await this.slowmodes.clearSlowmodeUser(channel.id, slowmodeUser.user_id);
2018-12-15 17:04:04 +02:00
}
}
return { failedUsers };
}
/**
* COMMAND: Disable slowmode on the specified channel
*/
@d.command("slowmode disable", "<channel:channel>")
@d.permission("can_manage")
async disableSlowmodeCmd(msg: Message, args: { channel: GuildChannel & TextChannel }) {
const botSlowmode = await this.slowmodes.getChannelSlowmode(args.channel.id);
const hasNativeSlowmode = args.channel.rateLimitPerUser;
if (!botSlowmode && hasNativeSlowmode === 0) {
msg.channel.createMessage(errorMessage("Channel is not on slowmode!"));
return;
}
const initMsg = await msg.channel.createMessage("Disabling slowmode...");
// Disable bot-maintained slowmode
let failedUsers = [];
if (botSlowmode) {
const result = await this.disableBotSlowmodeForChannel(args.channel);
failedUsers = result.failedUsers;
}
// Disable native slowmode
if (hasNativeSlowmode) {
await args.channel.edit({ rateLimitPerUser: 0 });
}
2018-12-15 17:04:04 +02:00
if (failedUsers.length) {
msg.channel.createMessage(
successMessage(
`Slowmode disabled! Failed to clear slowmode from the following users:\n\n<@!${failedUsers.join(">\n<@!")}>`,
),
2018-12-15 17:04:04 +02:00
);
} else {
msg.channel.createMessage(successMessage("Slowmode disabled!"));
initMsg.delete().catch(noop);
}
}
/**
* COMMAND: Clear slowmode from a specific user on a specific channel
*/
2019-04-20 19:55:35 +03:00
@d.command("slowmode clear", "<channel:channel> <user:resolvedUserLoose>")
@d.permission("can_manage")
async clearSlowmodeCmd(msg: Message, args: { channel: GuildChannel & TextChannel; user: User }) {
const channelSlowmode = await this.slowmodes.getChannelSlowmode(args.channel.id);
if (!channelSlowmode) {
msg.channel.createMessage(errorMessage("Channel doesn't have slowmode!"));
return;
}
await this.clearBotSlowmodeFromUserId(args.channel, args.user.id);
msg.channel.createMessage(
successMessage(
`Slowmode cleared from **${args.user.username}#${args.user.discriminator}** in <#${args.channel.id}>`,
),
);
}
@d.command("slowmode list")
@d.permission("can_manage")
async slowmodeListCmd(msg: Message) {
const channels = this.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 this.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!"));
}
}
2018-12-15 17:04:04 +02:00
/**
* COMMAND: Set slowmode for the specified channel
2018-12-15 17:04:04 +02:00
*/
@d.command("slowmode", "<channel:channel> <time:string>", {
overloads: ["<time:string>"],
})
@d.permission("can_manage")
2018-12-15 17:04:04 +02:00
async slowmodeCmd(msg: Message, args: { channel?: GuildChannel & TextChannel; time: string }) {
const channel = args.channel || msg.channel;
if (channel == null || !(channel instanceof TextChannel)) {
msg.channel.createMessage(errorMessage("Channel must be a text channel"));
return;
}
const seconds = Math.ceil(convertDelayStringToMS(args.time, "s") / 1000);
const useNativeSlowmode = this.getConfigForChannel(channel).use_native_slowmode && seconds <= NATIVE_SLOWMODE_LIMIT;
if (seconds === 0) {
return this.disableSlowmodeCmd(msg, { channel });
}
if (seconds > MAX_SLOWMODE) {
this.sendErrorMessage(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 this.slowmodes.getChannelSlowmode(channel.id);
if (existingBotSlowmode) {
await this.disableBotSlowmodeForChannel(channel);
}
// Set slowmode
channel.edit({
rateLimitPerUser: seconds,
});
} else {
// Bot-maintained slowmode
// If there is an existing native slowmode, disable that first
if (channel.rateLimitPerUser) {
await channel.edit({
rateLimitPerUser: 0,
});
}
await this.slowmodes.setChannelSlowmode(channel.id, seconds);
}
2018-12-15 17:04:04 +02:00
const humanizedSlowmodeTime = humanizeDuration(seconds * 1000);
const slowmodeType = useNativeSlowmode ? "native slowmode" : "bot-maintained slowmode";
2018-12-15 17:04:04 +02:00
msg.channel.createMessage(
successMessage(`Set ${humanizedSlowmodeTime} slowmode for <#${channel.id}> (${slowmodeType})`),
2018-12-15 17:04:04 +02:00
);
}
/**
* EVENT: On every message, check if the channel has a bot-maintained slowmode. If it does, apply slowmode to the user.
2018-12-15 17:04:04 +02:00
* If the user already had slowmode but was still able to send a message (e.g. sending a lot of messages at once),
* remove the messages sent after slowmode was applied.
*/
async onMessageCreate(msg: SavedMessage) {
if (msg.is_bot) return;
const channel = this.guild.channels.get(msg.channel_id) as GuildChannel & TextChannel;
if (!channel) return;
// Don't apply slowmode if the lock was interrupted earlier (e.g. the message was caught by word filters)
const thisMsgLock = await this.locks.acquire(`message-${msg.id}`);
if (thisMsgLock.interrupted) return;
// Make sure this user is affected by the slowmode
2019-05-02 08:21:11 +03:00
const member = await this.getMember(msg.user_id);
const isAffected = this.hasPermission("is_affected", { channelId: channel.id, userId: msg.user_id, member });
if (!isAffected) return thisMsgLock.unlock();
2018-12-15 17:04:04 +02:00
// Check if this channel even *has* a bot-maintained slowmode
const channelSlowmode = await this.slowmodes.getChannelSlowmode(channel.id);
if (!channelSlowmode) return thisMsgLock.unlock();
2018-12-15 17:04:04 +02:00
// Delete any extra messages sent after a slowmode was already applied
const userHasSlowmode = await this.slowmodes.userHasSlowmode(channel.id, msg.user_id);
2018-12-15 17:04:04 +02:00
if (userHasSlowmode) {
const message = await channel.getMessage(msg.id);
if (message) {
message.delete();
return thisMsgLock.interrupt();
}
return thisMsgLock.unlock();
2018-12-15 17:04:04 +02:00
}
await this.applyBotSlowmodeToUserId(channel, msg.user_id);
thisMsgLock.unlock();
2018-12-15 17:04:04 +02:00
}
/**
* Clears all expired bot-maintained user slowmodes in this guild
2018-12-15 17:04:04 +02:00
*/
async clearExpiredSlowmodes() {
const expiredSlowmodeUsers = await this.slowmodes.getExpiredSlowmodeUsers();
for (const user of expiredSlowmodeUsers) {
const channel = this.guild.channels.get(user.channel_id);
if (!channel) {
await this.slowmodes.clearSlowmodeUser(user.channel_id, user.user_id);
continue;
}
await this.clearBotSlowmodeFromUserId(channel as GuildChannel & TextChannel, user.user_id);
2018-12-15 17:04:04 +02:00
}
}
}