From 3fe67e18b029d741276d4cb0cdbee78bd7e35e63 Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 7 May 2023 16:23:16 +0200 Subject: [PATCH] Channel will terminate when the last user quit --- cogs/cogChannel.py | 15 +++++++++++---- functions.py | 36 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/cogs/cogChannel.py b/cogs/cogChannel.py index 1df78c1..7adfa1c 100644 --- a/cogs/cogChannel.py +++ b/cogs/cogChannel.py @@ -9,6 +9,7 @@ from discord import ( utils, ) from discord.ext import commands +from discord.abc import GuildChannel from functions import ( appendLog, @@ -32,6 +33,11 @@ class CogChannel(commands.Cog): def __init__(self, client): self.client = client + @commands.Cog.listener() + async def on_guild_channel_delete(self, channel: GuildChannel): + if isUserVoice(channel): + await removeUserVoice(channel) + @Cog.listener() async def on_voice_state_update( self, member: Member, before: VoiceState, after: VoiceState @@ -44,14 +50,15 @@ class CogChannel(commands.Cog): # If user left vc if before.channel is not None and len(before.channel.members) == 0: if isUserVoice(vc_from): - if isVoiceOfUser(vc_from, member): - await removeUserVoice(vc_from) - else: + if not isVoiceOfUser(vc_from, member): if loadJson("config.json")["enable_nomic"]: await changeNomicPerms("deny", vc_from, member) + await removeUserVoice(vc_from) # If user joined vc - if after.channel is not None and after.channel.id == guildConfGet(member.guild, "channel"): + if after.channel is not None and after.channel.id == guildConfGet( + member.guild, "channel" + ): if isUserVoice(vc_to): if loadJson("config.json")["enable_nomic"]: await changeNomicPerms("allow", vc_to, member) diff --git a/functions.py b/functions.py index 7d6b713..bd7a69e 100644 --- a/functions.py +++ b/functions.py @@ -253,31 +253,45 @@ async def removeUserVoice(vc: VoiceChannel) -> None: nomic_channel = utils.get(vc.guild.channels, id=vc_conf["nomic"]) remove(vc_file) - - await needed_channel.delete() - if debug: - appendLog( - f"Removed voice channel '{needed_channel}' ({str(needed_channel.id)}) of user with id {str(vc_conf['ownerid'])}", - guild=vc.guild, - ) - else: + + if needed_channel is None: appendLog( f"Removed voice channel '{needed_channel}' of user with id {str(vc_conf['ownerid'])}", guild=vc.guild, ) + else: + await needed_channel.delete() - if loadJson("config.json")["enable_nomic"]: - await nomic_channel.delete() if debug: appendLog( - f"Removed nomic channel {nomic_channel} ({str(nomic_channel.id)}) of channel with id {str(needed_channel.id)}", + f"Removed voice channel '{needed_channel}' ({str(needed_channel.id)}) of user with id {str(vc_conf['ownerid'])}", guild=vc.guild, ) else: + appendLog( + f"Removed voice channel '{needed_channel}' of user with id {str(vc_conf['ownerid'])}", + guild=vc.guild, + ) + + if loadJson("config.json")["enable_nomic"]: + if nomic_channel is None: appendLog( f"Removed nomic channel '{nomic_channel}' of channel with id {str(needed_channel.id)}", guild=vc.guild, ) + else: + await nomic_channel.delete() + + if debug: + appendLog( + f"Removed nomic channel '{nomic_channel}' ({str(nomic_channel.id)}) of channel with id {str(needed_channel.id)}", + guild=vc.guild, + ) + else: + appendLog( + f"Removed nomic channel '{nomic_channel}' of channel with id {str(needed_channel.id)}", + guild=vc.guild, + ) else: return