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