From 99e41e56519606a1fd8e438f6d81f2f1430c8fff Mon Sep 17 00:00:00 2001 From: s369269 Date: Wed, 25 Aug 2021 00:23:41 +0200 Subject: [PATCH 1/2] first try at auto add and delete of channel --- bot/commandowner.py | 9 ++++--- main.py | 62 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/bot/commandowner.py b/bot/commandowner.py index 08b4803..fe34742 100644 --- a/bot/commandowner.py +++ b/bot/commandowner.py @@ -18,9 +18,12 @@ def __init__(self, bot, helpf, tban, jh, xpf): @commands.command(name='test', pass_context=True, brief='Testing command for programmer.', description='You need privilege level Owner to use this command. Only the programmer knows what happens here.') @isBotOwnerCommand() - async def test(self, ctx): - await ctx.send("Geht") - await ctx.message.delete() + async def test(self, ctx, channelID): + channel = self.bot.get_channel(int(channelID)) + newName = channel.name[:-1] + str(int(channel.name[-1])+1) + newChannel = await channel.clone(name = newName) + # Move channel + await newChannel.move(after = channel) @commands.command(name="ping") @isBotOwnerCommand() diff --git a/main.py b/main.py index 6053d8b..b87e83e 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import certifi import os import discord -from discord.utils import get +from discord.utils import get, find from discord.ext import commands import time import datetime @@ -301,6 +301,66 @@ async def on_error(event, *args, **kwargs): await helpf.log(message,2) """ +@bot.event +async def on_voice_state_update(member, before, after): + """ + Clones joined channel if it has a number in the end + """ + # when user joins channel: before = None; after is a voicestate + + + """ + User joins channel after.channel. If channel ends with a number, than a copy will be created with the lowest possible ending number. + """ + if after.channel and len(after.channel.members) <= 1: + # Get channels to get lowest enumeration of channel + afterNumber = None + nameIndex = -1 + while after.channel.name[nameIndex:].isdigit(): + afterNumber = int(after.channel.name[nameIndex:]) # number on the end of channel name + nameIndex -= 1 + nameIndex += 1 + + serverid = int(jh.getFromConfig("guilde")) + allChannel = helpf.getVoiceChannelsFrom(serverid) + channelWithoutNumber = after.channel.name[:nameIndex] + # When after channel name ends with number and channel number 1 has user in it + if afterNumber and len(find(lambda c: c.name == (channelWithoutNumber + "1"), allChannel).members): + # Get channels with after.channel.name without numbers in it and end with digits + voiceChanelsWithName = [channel for channel in allChannel if after.channel.name[:nameIndex] in channel.name and channel.name[len(channelWithoutNumber):].isdigit()] + # Get all numbers in the end of voiceChannelsWithName + numbersOfChannels = [int(channel.name[len(channelWithoutNumber):]) for channel in voiceChanelsWithName] + lowestFreeID = min([i for i in range(2, max(numbersOfChannels) + 2) if not i in numbersOfChannels]) + + channelWithNumberBefore = find(lambda c: c.name[-len(str(lowestFreeID - 1)):] == str(lowestFreeID - 1), voiceChanelsWithName) + newChannelName = channelWithoutNumber + str(lowestFreeID) + # Create channel and gets it + newChannel = await channelWithNumberBefore.clone(name = newChannelName) + # Move channel after channelWithNumberBefore + await newChannel.move(after = channelWithNumberBefore) + + """ + When a user leaves a channel (before.channel) with a number endingn nobody else is connected und and number is not 1, than the channel will be deleted. + """ + if before.channel and len(before.channel.members) == 0 and before.channel.name[-1].isdigit(): + # Member left first channel + if before.channel.name[-1] == "1" and not before.channel.name[-2].isdigit(): + # Delete last channel, which has no user in it + serverid = int(jh.getFromConfig("guilde")) + allChannel = helpf.getVoiceChannelsFrom(serverid) + + channelWithoutNumber = before.channel.name[:-1] + print([channel for channel in allChannel if channelWithoutNumber in channel.name and len(channel.members) == 0]) + lastChannel = max([channel for channel in allChannel if channelWithoutNumber in channel.name and len(channel.members) == 0]) + + await lastChannel.delete() + + + # User left channel, which is not the first channel. So it will be deleted + else: + await before.channel.delete() + + jh.config["log"] = "False" jh.saveConfig() print("Set log to False.") From 2c581deb2d3d2398da7979000fade517064dcbd9 Mon Sep 17 00:00:00 2001 From: s369269 Date: Wed, 25 Aug 2021 00:42:47 +0200 Subject: [PATCH 2/2] dynamic-voice-channel finished --- main.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index b87e83e..0063099 100644 --- a/main.py +++ b/main.py @@ -309,6 +309,29 @@ async def on_voice_state_update(member, before, after): # when user joins channel: before = None; after is a voicestate + """ + When a user leaves a channel (before.channel) with a number endingn nobody else is connected und and number is not 1, than the channel will be deleted. + """ + if before.channel and len(before.channel.members) == 0 and before.channel.name[-1].isdigit(): + # Member left first channel + if before.channel.name[-1] == "1" and not before.channel.name[-2].isdigit(): + # Delete last channel, which has no user in it + serverid = int(jh.getFromConfig("guilde")) + allChannel = helpf.getVoiceChannelsFrom(serverid) + + channelWithoutNumber = before.channel.name[:-1] + notFirstVoiceChannel = [channel for channel in allChannel if channelWithoutNumber in channel.name and len(channel.members) == 0 and channel.name != channelWithoutNumber + "1"] + if notFirstVoiceChannel: + lastChannel = max(notFirstVoiceChannel, key = lambda c: int(c.name[len(channelWithoutNumber):])) + + await lastChannel.delete() + + + # User left channel, which is not the first channel. So it will be deleted + else: + await before.channel.delete() + + """ User joins channel after.channel. If channel ends with a number, than a copy will be created with the lowest possible ending number. """ @@ -339,27 +362,6 @@ async def on_voice_state_update(member, before, after): # Move channel after channelWithNumberBefore await newChannel.move(after = channelWithNumberBefore) - """ - When a user leaves a channel (before.channel) with a number endingn nobody else is connected und and number is not 1, than the channel will be deleted. - """ - if before.channel and len(before.channel.members) == 0 and before.channel.name[-1].isdigit(): - # Member left first channel - if before.channel.name[-1] == "1" and not before.channel.name[-2].isdigit(): - # Delete last channel, which has no user in it - serverid = int(jh.getFromConfig("guilde")) - allChannel = helpf.getVoiceChannelsFrom(serverid) - - channelWithoutNumber = before.channel.name[:-1] - print([channel for channel in allChannel if channelWithoutNumber in channel.name and len(channel.members) == 0]) - lastChannel = max([channel for channel in allChannel if channelWithoutNumber in channel.name and len(channel.members) == 0]) - - await lastChannel.delete() - - - # User left channel, which is not the first channel. So it will be deleted - else: - await before.channel.delete() - jh.config["log"] = "False" jh.saveConfig()