diff --git a/config/settings.yml.example b/config/settings.yml.example index 111ecf375..874fd01fd 100644 --- a/config/settings.yml.example +++ b/config/settings.yml.example @@ -65,6 +65,7 @@ USER_IDS: TEMPVC_CATEGORY_ID: 123456789012345679 # Set this to the channel ID where you want the temporary voice channels to be created. TEMPVC_CHANNEL_ID: 123456789012345679 +TEMPVC_BASE_NAME: "/tmp/" # This will automatically give people with a status regex a role. STATUS_ROLES: diff --git a/tux/cogs/services/temp_vc.py b/tux/cogs/services/temp_vc.py index bdf13a0fb..45c177cd6 100644 --- a/tux/cogs/services/temp_vc.py +++ b/tux/cogs/services/temp_vc.py @@ -1,3 +1,5 @@ +import asyncio + import discord from discord.ext import commands @@ -8,7 +10,7 @@ class TempVc(commands.Cog): def __init__(self, bot: Tux) -> None: self.bot = bot - self.base_vc_name: str = "/tmp/" + self.base_vc_name: str = CONFIG.TEMPVC_BASE_NAME or "/tmp/" @commands.Cog.listener() async def on_voice_state_update( @@ -34,7 +36,8 @@ async def on_voice_state_update( # Ensure CONFIGants are set correctly temp_channel_id = int(CONFIG.TEMPVC_CHANNEL_ID or "0") temp_category_id = int(CONFIG.TEMPVC_CATEGORY_ID or "0") - if temp_channel_id == 0 or temp_category_id == 0: + + if 0 in {temp_category_id, temp_channel_id}: return # When user joins the temporary voice channel @@ -42,7 +45,7 @@ async def on_voice_state_update( await self._handle_user_join(member, after.channel) # When user leaves any voice channel - elif before.channel: + if before.channel: await self._handle_user_leave(before.channel, after.channel, temp_channel_id, temp_category_id) async def _handle_user_join( @@ -61,11 +64,12 @@ async def _handle_user_join( The channel that the member joined. """ - for voice_channel in channel.guild.voice_channels: - # Check if the channel is a temporary channel and if it is the user's channel - if voice_channel.name == self.base_vc_name + member.name: - await member.move_to(voice_channel) - return + tasks = [ + member.move_to(voice_channel) + for voice_channel in channel.guild.voice_channels + if voice_channel.name == self.base_vc_name + member.name + ] + asyncio.gather(*tasks) # Create a new channel for the user if it doesn't exist new_channel = await channel.clone(name=self.base_vc_name + member.name) @@ -107,19 +111,16 @@ async def _handle_user_leave( return # Delete the channel if it is empty - if len(before_channel.members) == 0: + if before_channel.members == []: await before_channel.delete() # Search and delete all empty temporary channels - for channel in category.voice_channels: - if ( - not channel.name.startswith(self.base_vc_name) - or len(channel.members) != 0 - or channel.id == temp_channel_id - ): - continue - - await channel.delete() + tasks = [ + channel.delete() + for channel in category.voice_channels + if channel.name.startswith(self.base_vc_name) and channel.members == [] and channel.id == temp_channel_id + ] + await asyncio.gather(*tasks) async def setup(bot: Tux) -> None: diff --git a/tux/utils/config.py b/tux/utils/config.py index dd3cc7fc3..9b6f583f1 100644 --- a/tux/utils/config.py +++ b/tux/utils/config.py @@ -132,6 +132,7 @@ def BOT_TOKEN(self) -> str: # noqa: N802 # Temp VC TEMPVC_CATEGORY_ID: Final[str | None] = config["TEMPVC_CATEGORY_ID"] TEMPVC_CHANNEL_ID: Final[str | None] = config["TEMPVC_CHANNEL_ID"] + TEMPVC_BASE_NAME: Final[str | None] = config["TEMPVC_BASE_NAME"] # GIF ratelimiter RECENT_GIF_AGE: Final[int] = config["GIF_LIMITER"]["RECENT_GIF_AGE"]