1
+ import asyncio
2
+
1
3
import discord
2
4
from discord .ext import commands
3
5
@@ -34,15 +36,16 @@ async def on_voice_state_update(
34
36
# Ensure CONFIGants are set correctly
35
37
temp_channel_id = int (CONFIG .TEMPVC_CHANNEL_ID or "0" )
36
38
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 }:
38
41
return
39
42
40
43
# When user joins the temporary voice channel
41
44
if after .channel and after .channel .id == temp_channel_id :
42
45
await self ._handle_user_join (member , after .channel )
43
46
44
47
# When user leaves any voice channel
45
- elif before .channel :
48
+ if before .channel :
46
49
await self ._handle_user_leave (before .channel , after .channel , temp_channel_id , temp_category_id )
47
50
48
51
async def _handle_user_join (
@@ -61,11 +64,15 @@ async def _handle_user_join(
61
64
The channel that the member joined.
62
65
"""
63
66
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 )
69
76
70
77
# Create a new channel for the user if it doesn't exist
71
78
new_channel = await channel .clone (name = self .base_vc_name + member .name )
@@ -107,19 +114,16 @@ async def _handle_user_leave(
107
114
return
108
115
109
116
# Delete the channel if it is empty
110
- if len ( before_channel .members ) == 0 :
117
+ if before_channel .members == [] :
111
118
await before_channel .delete ()
112
119
113
120
# 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 )
123
127
124
128
125
129
async def setup (bot : Tux ) -> None :
0 commit comments