1
+ import discord
2
+ import argparse
3
+
4
+ INSITGATOR_NAME = 'New Session'
5
+ GENERATED_PREFIX = '#'
6
+ GENERATED_NAME = '{}{}'
7
+
8
+ class VoicesBot (discord .Client ):
9
+ async def on_ready (self ):
10
+ self .instigator_channels = {}
11
+
12
+ for guild in self .guilds :
13
+ channel = self .instigator_channel (guild )
14
+ if not channel :
15
+ channel = await guild .create_voice_channel (INSITGATOR_NAME )
16
+ self .instigator_channels [guild ] = channel
17
+
18
+ print ('Logged on as {0}!' .format (self .user ))
19
+
20
+ async def on_voice_state_update (self , member , before , after ):
21
+ if before .channel :
22
+ guild = before .channel .guild
23
+
24
+ if before .channel .name .startswith (GENERATED_PREFIX ) and len (before .channel .members ) == 0 :
25
+ await self .remove_channel (before .channel )
26
+
27
+ if after .channel :
28
+ print (member .display_name + " joined voice channel: " + after .channel .name )
29
+
30
+ guild = after .channel .guild
31
+
32
+ if after .channel == self .instigator_channel (guild ):
33
+ new_channel = await self .create_next_channel (guild )
34
+ for member in after .channel .members :
35
+ await member .move_to (new_channel )
36
+
37
+ async def create_next_channel (self , guild ):
38
+ inst = self .instigator_channel (guild )
39
+ name = GENERATED_NAME .format (GENERATED_PREFIX , self .num_channels (guild ) + 1 )
40
+ return await inst .clone (name = name )
41
+
42
+ async def remove_channel (self , channel ):
43
+ guild = channel .guild
44
+ await channel .delete ()
45
+ self .rename_channels (guild )
46
+
47
+ def rename_channels (self , guild ):
48
+ i = 0
49
+ for channel in guild .voice_channels :
50
+ if channel .name .startswith (GENERATED_PREFIX ):
51
+ i += 1
52
+ channel .name = GENERATED_NAME .format (GENERATED_PREFIX , i )
53
+
54
+ def instigator_channel (self , guild ):
55
+ if guild in self .instigator_channels :
56
+ return self .instigator_channels [guild ]
57
+
58
+ for channel in guild .voice_channels :
59
+ if channel .name == INSITGATOR_NAME :
60
+ return channel
61
+
62
+ return None
63
+
64
+ def num_channels (self , guild ):
65
+ count = 0
66
+ for channel in guild .voice_channels :
67
+ if channel .name .startswith (GENERATED_PREFIX ):
68
+ count += 1
69
+ return count
70
+
71
+
72
+
73
+ if __name__ == '__main__' :
74
+ parser = argparse .ArgumentParser (description = 'Automatically manage discord voice channels.' )
75
+ parser .add_argument ('client_id' , help = 'discord client id' )
76
+ args = parser .parse_args ()
77
+
78
+ client = VoicesBot ()
79
+ client .run (args .client_id )
0 commit comments