diff --git a/backend/src/plugins/CompanionChannels/CompanionChannelsPlugin.ts b/backend/src/plugins/CompanionChannels/CompanionChannelsPlugin.ts index a631d813..dc74002d 100644 --- a/backend/src/plugins/CompanionChannels/CompanionChannelsPlugin.ts +++ b/backend/src/plugins/CompanionChannels/CompanionChannelsPlugin.ts @@ -4,6 +4,8 @@ import { VoiceChannelJoinEvt } from "./events/VoiceChannelJoinEvt"; import { VoiceChannelSwitchEvt } from "./events/VoiceChannelSwitchEvt"; import { VoiceChannelLeaveEvt } from "./events/VoiceChannelLeaveEvt"; import { trimPluginDescription } from "../../utils"; +import { LogsPlugin } from "../Logs/LogsPlugin"; +import { CooldownManager } from "knub"; const defaultOptions = { config: { @@ -22,8 +24,13 @@ export const CompanionChannelsPlugin = zeppelinPlugin, userId: string, voiceChannelId: string, oldChannelId?: string, ); -export function handleCompanionPermissions( +export async function handleCompanionPermissions( pluginData: PluginData, userId: string, voiceChannelId: null, oldChannelId: string, ); -export function handleCompanionPermissions( +export async function handleCompanionPermissions( pluginData: PluginData, userId: string, voiceChannelId?: string, oldChannelId?: string, ) { + if (pluginData.state.errorCooldownManager.isOnCooldown(ERROR_COOLDOWN_KEY)) { + return; + } + const permsToDelete: Set = new Set(); // channelId[] const oldPerms: Map = new Map(); // channelId => permissions const permsToSet: Map = new Map(); // channelId => permissions @@ -51,15 +61,34 @@ export function handleCompanionPermissions( } } - for (const channelId of permsToDelete) { - const channel = pluginData.guild.channels.get(channelId); - if (!channel || !(channel instanceof TextChannel)) continue; - channel.deletePermission(userId, `Companion Channel for ${oldChannelId} | User Left`); - } + try { + for (const channelId of permsToDelete) { + const channel = pluginData.guild.channels.get(channelId); + if (!channel || !(channel instanceof TextChannel)) continue; + await channel.deletePermission(userId, `Companion Channel for ${oldChannelId} | User Left`); + } - for (const [channelId, permissions] of permsToSet) { - const channel = pluginData.guild.channels.get(channelId); - if (!channel || !(channel instanceof TextChannel)) continue; - channel.editPermission(userId, permissions, 0, "member", `Companion Channel for ${voiceChannelId} | User Joined`); + for (const [channelId, permissions] of permsToSet) { + const channel = pluginData.guild.channels.get(channelId); + if (!channel || !(channel instanceof TextChannel)) continue; + await channel.editPermission( + userId, + permissions, + 0, + "member", + `Companion Channel for ${voiceChannelId} | User Joined`, + ); + } + } catch (e) { + if (isDiscordRESTError(e) && e.code === 50001) { + const logs = pluginData.getPlugin(LogsPlugin); + logs.log(LogType.BOT_ALERT, { + body: `Missing permissions to handle companion channels. Pausing companion channels for 5 minutes or until the bot is reloaded on this server.`, + }); + pluginData.state.errorCooldownManager.setCooldown(ERROR_COOLDOWN_KEY, ERROR_COOLDOWN); + return; + } + + throw e; } } diff --git a/backend/src/plugins/CompanionChannels/types.ts b/backend/src/plugins/CompanionChannels/types.ts index e27277f3..781ad7e3 100644 --- a/backend/src/plugins/CompanionChannels/types.ts +++ b/backend/src/plugins/CompanionChannels/types.ts @@ -1,6 +1,6 @@ import * as t from "io-ts"; import { tNullable } from "../../utils"; -import { BasePluginType } from "knub"; +import { BasePluginType, CooldownManager } from "knub"; import { GuildLogs } from "../../data/GuildLogs"; import { GuildSavedMessages } from "../../data/GuildSavedMessages"; @@ -24,5 +24,7 @@ export interface ICompanionChannelMap { export interface CompanionChannelsPluginType extends BasePluginType { config: TConfigSchema; - state: {}; + state: { + errorCooldownManager: CooldownManager; + }; }