-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodisconnect.py
71 lines (57 loc) · 2.4 KB
/
autodisconnect.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
import asyncio
import discord
import logging
from typing import Union
from redbot.core import commands, Config
log = logging.getLogger('red.autodisconnect')
class Autodisconnect(commands.Cog):
"""Carl's Autodisconnect Cog"""
guild_default = {
'timeout': -1,
}
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, 1337, True)
self.config.register_guild(**self.guild_default)
async def cog_load(self):
log.info('%s: Cog Load Start', self.__cog_name__)
log.info('%s: Cog Load Finish', self.__cog_name__)
async def cog_unload(self):
log.info('%s: Cog Unload', self.__cog_name__)
@commands.Cog.listener(name='on_voice_state_update')
async def on_voice_state_update(self, member: discord.Member,
before, after):
def check(m: discord.Member, b, a):
return a.channel != m.guild.afk_channel and m == member
if not after.channel or not member.guild.afk_channel or \
before.channel == after.channel or \
after.channel != member.guild.afk_channel:
return
timeout = await self.config.guild(member.guild).timeout()
if timeout < 0:
return
try:
await self.bot.wait_for("voice_state_update", check=check,
timeout=timeout * 60)
except asyncio.TimeoutError:
pass
else:
return
await member.move_to(None)
@commands.command(name='autodisconnect', aliases=['autodc'])
@commands.guild_only()
@commands.admin_or_permissions(manage_guild=True)
async def autodisconnect(self, ctx: commands.Context,
minutes: Union[int, float, str]):
"""Set Autodisconnect time. Use -1 to disable or 0 for instant."""
if isinstance(minutes, str):
if minutes.lower() in ['disable', 'off', 'stop']:
await self.config.guild(ctx.guild).timeout.set(-1)
return await ctx.send("Autodisconnect disabled.")
try:
minutes = int(minutes)
await self.config.guild(ctx.guild).timeout.set(minutes)
await ctx.send(f"Autodisconnect timeout set to **{minutes}** minutes.")
except Exception as error:
log.error(error)
await ctx.send(f"I don't know what to do with: **{minutes}**")