diff --git a/src/data/GuildSlowmodes.ts b/src/data/GuildSlowmodes.ts index f6f27d76..db0d4dc8 100644 --- a/src/data/GuildSlowmodes.ts +++ b/src/data/GuildSlowmodes.ts @@ -18,8 +18,8 @@ export class GuildSlowmodes extends BaseRepository { return this.slowmodeChannels.findOne({ where: { guild_id: this.guildId, - channel_id: channelId - } + channel_id: channelId, + }, }); } @@ -29,25 +29,25 @@ export class GuildSlowmodes extends BaseRepository { await this.slowmodeChannels.update( { guild_id: this.guildId, - channel_id: channelId + channel_id: channelId, }, { - slowmode_seconds: seconds - } + slowmode_seconds: seconds, + }, ); } else { await this.slowmodeChannels.insert({ guild_id: this.guildId, channel_id: channelId, - slowmode_seconds: seconds + slowmode_seconds: seconds, }); } } - async clearChannelSlowmode(channelId): Promise { + async deleteChannelSlowmode(channelId): Promise { await this.slowmodeChannels.delete({ guild_id: this.guildId, - channel_id: channelId + channel_id: channelId, }); } @@ -55,7 +55,7 @@ export class GuildSlowmodes extends BaseRepository { return this.slowmodeUsers.findOne({ guild_id: this.guildId, channel_id: channelId, - user_id: userId + user_id: userId, }); } @@ -77,11 +77,11 @@ export class GuildSlowmodes extends BaseRepository { { guild_id: this.guildId, channel_id: channelId, - user_id: userId + user_id: userId, }, { - expires_at: expiresAt - } + expires_at: expiresAt, + }, ); } else { // Add new @@ -89,7 +89,7 @@ export class GuildSlowmodes extends BaseRepository { guild_id: this.guildId, channel_id: channelId, user_id: userId, - expires_at: expiresAt + expires_at: expiresAt, }); } } @@ -98,7 +98,7 @@ export class GuildSlowmodes extends BaseRepository { await this.slowmodeUsers.delete({ guild_id: this.guildId, channel_id: channelId, - user_id: userId + user_id: userId, }); } @@ -106,8 +106,8 @@ export class GuildSlowmodes extends BaseRepository { return this.slowmodeUsers.find({ where: { guild_id: this.guildId, - channel_id: channelId - } + channel_id: channelId, + }, }); } diff --git a/src/plugins/Slowmode.ts b/src/plugins/Slowmode.ts index 749b3674..9b463509 100644 --- a/src/plugins/Slowmode.ts +++ b/src/plugins/Slowmode.ts @@ -1,24 +1,30 @@ -import { Plugin, decorators as d, IBasePluginConfig, IPluginOptions } from "knub"; +import { decorators as d, IPluginOptions } from "knub"; import { GuildChannel, Message, TextChannel, Constants as ErisConstants, User } from "eris"; import { convertDelayStringToMS, errorMessage, noop, successMessage } from "../utils"; import { GuildSlowmodes } from "../data/GuildSlowmodes"; import humanizeDuration from "humanize-duration"; import { ZeppelinPlugin } from "./ZeppelinPlugin"; +interface ISlowmodePluginConfig { + use_native_slowmode: boolean; +} + interface ISlowmodePluginPermissions { manage: boolean; affected: boolean; } -export class SlowmodePlugin extends ZeppelinPlugin { +export class SlowmodePlugin extends ZeppelinPlugin { public static pluginName = "slowmode"; protected slowmodes: GuildSlowmodes; protected clearInterval; - getDefaultOptions(): IPluginOptions { + getDefaultOptions(): IPluginOptions { return { - config: {}, + config: { + use_native_slowmode: true, + }, permissions: { manage: false, @@ -47,10 +53,10 @@ export class SlowmodePlugin extends ZeppelinPlugin") - @d.permission("manage") - async disableSlowmodeCmd(msg: Message, args: { channel: GuildChannel & TextChannel }) { - const slowmode = await this.slowmodes.getChannelSlowmode(args.channel.id); - if (!slowmode) { - msg.channel.createMessage(errorMessage("Channel is not on slowmode!")); - return; - } - + async disableBotSlowmodeForChannel(channel: GuildChannel & TextChannel) { // Disable channel slowmode - const initMsg = await msg.channel.createMessage("Disabling slowmode..."); - await this.slowmodes.clearChannelSlowmode(args.channel.id); + await this.slowmodes.deleteChannelSlowmode(channel.id); // Remove currently applied slowmodes - const users = await this.slowmodes.getChannelSlowmodeUsers(args.channel.id); + const users = await this.slowmodes.getChannelSlowmodeUsers(channel.id); const failedUsers = []; for (const slowmodeUser of users) { try { - await this.removeSlowmodeFromUserId(args.channel, slowmodeUser.user_id); + await this.clearBotSlowmodeFromUserId(channel, slowmodeUser.user_id); } 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(args.channel.id, slowmodeUser.user_id); + await this.slowmodes.clearSlowmodeUser(channel.id, slowmodeUser.user_id); } } + return { failedUsers }; + } + + /** + * COMMAND: Disable slowmode on the specified channel + */ + @d.command("slowmode disable", "") + @d.permission("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 }); + } + if (failedUsers.length) { msg.channel.createMessage( successMessage( - `Slowmode disabled! Failed to remove slowmode from the following users:\n\n<@!${failedUsers.join(">\n<@!")}>`, + `Slowmode disabled! Failed to clear slowmode from the following users:\n\n<@!${failedUsers.join(">\n<@!")}>`, ), ); } else { @@ -138,7 +166,7 @@ export class SlowmodePlugin extends ZeppelinPlugin`, @@ -147,7 +175,7 @@ export class SlowmodePlugin extends ZeppelinPlugin ") @d.command("slowmode", "") @@ -161,16 +189,43 @@ export class SlowmodePlugin extends ZeppelinPlugin (1 message in ${humanizedSlowmodeTime})`), + successMessage(`Set ${humanizedSlowmodeTime} slowmode for <#${channel.id}> (${slowmodeType})`), ); } /** - * EVENT: On every new message, check if the channel has slowmode. If it does, apply slowmode to the user. + * EVENT: On every message, check if the channel has a bot-maintained slowmode. If it does, apply slowmode to the user. * 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. */ @@ -188,11 +243,11 @@ export class SlowmodePlugin extends ZeppelinPlugin