fix: check for too many permission overwrites in companion channels

This commit is contained in:
Dragory 2023-04-01 21:53:12 +03:00
parent dd1afc4dab
commit c1a91d4dec
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1

View file

@ -10,6 +10,10 @@ import { getCompanionChannelOptsForVoiceChannelId } from "./getCompanionChannelO
const ERROR_COOLDOWN_KEY = "errorCooldown";
const ERROR_COOLDOWN = 5 * MINUTES;
// The real limit is 500, but to be on the safer side, this is lower
// Temporary fix until we move to role-based companion channels
const MAX_OVERWRITES = 450;
export async function handleCompanionPermissions(
pluginData: GuildPluginData<CompanionChannelsPluginType>,
userId: string,
@ -50,6 +54,8 @@ export async function handleCompanionPermissions(
}
}
const logs = pluginData.getPlugin(LogsPlugin);
try {
for (const channelId of permsToDelete) {
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake);
@ -63,6 +69,12 @@ export async function handleCompanionPermissions(
for (const [channelId, permissions] of permsToSet) {
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake);
if (!channel || !(channel instanceof TextChannel)) continue;
if (channel.permissionOverwrites.cache.size >= MAX_OVERWRITES) {
logs.logBotAlert({
body: `Could not apply companion channel permissions for <#${channel.id}>: too many permissions`,
});
continue;
}
pluginData.state.serverLogs.ignoreLog(LogType.CHANNEL_UPDATE, channelId, 3 * 1000);
const fullSerialized = new PermissionsBitField(BigInt(permissions)).serialize();
const onlyAllowed = filterObject(fullSerialized, (v) => v === true);
@ -72,8 +84,6 @@ export async function handleCompanionPermissions(
}
} catch (e) {
if (isDiscordAPIError(e)) {
const logs = pluginData.getPlugin(LogsPlugin);
if (e.code === 50001) {
logs.logBotAlert({
body: `One of the companion channels can't be accessed. Pausing companion channels for 5 minutes or until the bot is reloaded on this server.`,