Skip to content

Commit d9ab393

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

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

tux/cogs/services/temp_vc.py

Lines changed: 21 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,15 @@ 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+
# for voice_channel in [v for v in channel.guild.voice_channels if v.name == self.base_vc_name + member.name]:
68+
# await member.move_to(voice_channel)
69+
# return
70+
tasks = [
71+
member.move_to(voice_channel)
72+
for voice_channel in channel.guild.voice_channels
73+
if voice_channel.name == self.base_vc_name + member.name
74+
]
75+
asyncio.gather(*tasks)
6976

7077
# Create a new channel for the user if it doesn't exist
7178
new_channel = await channel.clone(name=self.base_vc_name + member.name)
@@ -107,19 +114,16 @@ async def _handle_user_leave(
107114
return
108115

109116
# Delete the channel if it is empty
110-
if len(before_channel.members) == 0:
117+
if before_channel.members == []:
111118
await before_channel.delete()
112119

113120
# 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()
121+
tasks = [
122+
channel.delete()
123+
for channel in category.voice_channels
124+
if channel.name.startswith(self.base_vc_name) and channel.members == [] and channel.id == temp_channel_id
125+
]
126+
await asyncio.gather(*tasks)
123127

124128

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

0 commit comments

Comments
 (0)