Skip to content

Commit 38616f2

Browse files
committed
refactor(tempvc): use list comprehension and asyncio vs loops
1 parent 6466abd commit 38616f2

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

tux/cogs/services/temp_vc.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import asyncio
2+
13
import discord
24
from discord.ext import commands
35

@@ -34,15 +36,16 @@ async def on_voice_state_update(
3436
# Ensure CONFIGants are set correctly
3537
temp_channel_id = int(CONFIG.TEMPVC_CHANNEL_ID or "0")
3638
temp_category_id = int(CONFIG.TEMPVC_CATEGORY_ID or "0")
37-
if temp_channel_id == 0 or temp_category_id == 0:
39+
40+
if 0 in {temp_category_id, temp_channel_id}:
3841
return
3942

4043
# When user joins the temporary voice channel
4144
if after.channel and after.channel.id == temp_channel_id:
4245
await self._handle_user_join(member, after.channel)
4346

4447
# When user leaves any voice channel
45-
elif before.channel:
48+
if before.channel:
4649
await self._handle_user_leave(before.channel, after.channel, temp_channel_id, temp_category_id)
4750

4851
async def _handle_user_join(
@@ -61,11 +64,12 @@ async def _handle_user_join(
6164
The channel that the member joined.
6265
"""
6366

64-
for voice_channel in channel.guild.voice_channels:
65-
# Check if the channel is a temporary channel and if it is the user's channel
66-
if voice_channel.name == self.base_vc_name + member.name:
67-
await member.move_to(voice_channel)
68-
return
67+
tasks = [
68+
member.move_to(voice_channel)
69+
for voice_channel in channel.guild.voice_channels
70+
if voice_channel.name == self.base_vc_name + member.name
71+
]
72+
asyncio.gather(*tasks)
6973

7074
# Create a new channel for the user if it doesn't exist
7175
new_channel = await channel.clone(name=self.base_vc_name + member.name)
@@ -107,19 +111,16 @@ async def _handle_user_leave(
107111
return
108112

109113
# Delete the channel if it is empty
110-
if len(before_channel.members) == 0:
114+
if before_channel.members == []:
111115
await before_channel.delete()
112116

113117
# Search and delete all empty temporary channels
114-
for channel in category.voice_channels:
115-
if (
116-
not channel.name.startswith(self.base_vc_name)
117-
or len(channel.members) != 0
118-
or channel.id == temp_channel_id
119-
):
120-
continue
121-
122-
await channel.delete()
118+
tasks = [
119+
channel.delete()
120+
for channel in category.voice_channels
121+
if channel.name.startswith(self.base_vc_name) and channel.members == [] and channel.id == temp_channel_id
122+
]
123+
await asyncio.gather(*tasks)
123124

124125

125126
async def setup(bot: Tux) -> None:

0 commit comments

Comments
 (0)