From 30870d3c3b3ce3a601b645b62d59e5a545a7c706 Mon Sep 17 00:00:00 2001 From: The Darsh <129121284+DARSHTRON@users.noreply.github.com> Date: Tue, 27 Jun 2023 08:15:35 +0300 Subject: [PATCH 1/3] feat: added new listener `on_guild_remove` to help free up the DB fix: some uncaught error and possible error feat: added new listener `on_guild_remove` to help free up the DB via deleting that guild data from the database --- cogs/voice/main.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/cogs/voice/main.py b/cogs/voice/main.py index c562a1f..6231208 100644 --- a/cogs/voice/main.py +++ b/cogs/voice/main.py @@ -4,6 +4,7 @@ Main File | main.py """ import discord +import asyncio from cogs.voice.Embeds import Embeds from cogs.voice.Views import Views from cogs.voice.DataBase import DataBase @@ -18,7 +19,7 @@ class Control(discord.Cog): def __init__(self, bot): self.bot: discord.Bot = bot - temp_voice = discord.SlashCommandGroup(name="temp_voice", description="To manage temp channels on your server", guild_only=True, default_member_permissions=DEFAULT_PERMISSIONS) + temp_voice = discord.SlashCommandGroup(name="voice", description="To manage temp voice channels on your server", guild_only=True, default_member_permissions=DEFAULT_PERMISSIONS) @temp_voice.command(name="setup",description="To setup temp channels on your server", guild_only=True) async def setup(self, ctx: discord.ApplicationContext): await ctx.response.defer() @@ -34,7 +35,7 @@ async def setup(self, ctx: discord.ApplicationContext): @discord.Cog.listener() async def on_ready(self): self.bot.add_view(Views.Dropdown()) - print("Loadded Presistent View (Dropdown)") + print("[πŸ”΅] Loadded Presistent View") @discord.Cog.listener() async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState): @@ -62,26 +63,26 @@ async def on_guild_channel_update(self, before: discord.abc.GuildChannel, after: if not before.id in map((lambda x: x[1] if x else []), (await db.get_guild(before.guild.id))): return if not after.category: - try: - embed = Embeds.Warning() - await after.send(f"{after.guild.owner.mention}", embed=embed) - except: - pass + try: embed = Embeds.DeleteWarning(); await after.send(f"{after.guild.owner.mention}", embed=embed) + except: pass # ~~^~~ smi colon deez nutz return await db.remove_guild(before.guild.id, before.id) - else: - return + else: return @discord.Cog.listener() async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel): - if not channel.id in await db.get_guilds(channel.guild.id): + if not channel.id in map((lambda x: x[1] if x else []), (await db.get_guild(channel.guild.id))): return return await db.remove_guild(channel.guild.id, channel.id) - - + + @discord.Cog.listener() + async def on_guild_remove(self, guild: discord.Guild): + if not guild.id in map((lambda x: x[0] if x else []), (await db.get_guild(guild.id))): + return + return await asyncio.gather(*(db.remove_guild(guild.id, channel_id) for channel_id in map(lambda x: x[1] if x else [], await db.get_guild(guild.id)))) + # ~~^~~ goodluck understanding what's going on :3 # - Do You know What Girl Called "Lisa"? # - No! - def setup(bot: discord.Bot): bot.add_cog(Control(bot)) From 69d9c328596b57b50fdaef4405b06269d8fff27d Mon Sep 17 00:00:00 2001 From: The Darsh <129121284+DARSHTRON@users.noreply.github.com> Date: Tue, 27 Jun 2023 08:16:39 +0300 Subject: [PATCH 2/3] feat: added new warn embed --- cogs/voice/Embeds.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cogs/voice/Embeds.py b/cogs/voice/Embeds.py index ecdc99b..cfb413e 100644 --- a/cogs/voice/Embeds.py +++ b/cogs/voice/Embeds.py @@ -8,6 +8,7 @@ class Embeds(): + """Class contains all the embed objects for VoiceFive""" class Panel(discord.Embed): """Discord Embed that will be called when a user create's a temp channel""" def __init__(self, member: discord.Member): @@ -41,15 +42,26 @@ def __init__(self, interaction: discord.Interaction, channel: discord.VoiceChann self.set_footer(text=f"Requested by {interaction.user}", icon_url=interaction.user.display_avatar.url) - class Warning(discord.Embed): + class DeleteWarning(discord.Embed): """Discord Embed that will be used to warn the owner with wrong bot usage""" - def __init__(self): - super().__init__(title="Warning ⚠", color=discord.Color.red()) + def __init__(self, user: discord.Member = None): + super().__init__(title="Warning :warning:", color=discord.Color.red()) + self.add_field(name="Info", value=f"> User {user.mention} has deleted the temp channel parent!") if user else None self.add_field( name="Message", value=f""" -Please resetup your server temp channels +> Please resetup your server temp channels - If you want to edit the channel's preferences you can just edit its name/prefrences or anything! - Editing the channel's name/prefrences will make it's child's same is its parent. - To change the temp channel category you can just place it at any category but **DO NOT** let it without category! """) + + class ClearWarning(discord.Embed): + """Discord Embed that will be used to warn the temp channel owner for clearing the channel content""" + def __init__(self): + super().__init__(title="Warning :warning:", color=discord.Color.red()) + self.add_field( + name="Message", + value=f""" +> You Are About Deleting All Channel Messages, There No Way To Restore The Deleted Messages! + """) From b5a9b7fa3de59505ab0e15fce4e5a5ae6653caa6 Mon Sep 17 00:00:00 2001 From: The Darsh <129121284+DARSHTRON@users.noreply.github.com> Date: Tue, 27 Jun 2023 08:17:31 +0300 Subject: [PATCH 3/3] refactor: trying to fix this shit ;-; --- cogs/voice/DataBase.py | 112 +++++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 54 deletions(-) diff --git a/cogs/voice/DataBase.py b/cogs/voice/DataBase.py index e897d67..50af734 100644 --- a/cogs/voice/DataBase.py +++ b/cogs/voice/DataBase.py @@ -2,85 +2,89 @@ Database Quering Helper For VoiceFive Cog DataBaseUtil | DataBase.py -""" -import aiosqlite +DataBase Info: + β•švoice.db + β•‘ + ╠═ channels + β•‘ ╠═ user_id + β•‘ ╠═ channel_id + β•‘ β•šβ• guild_id + β•‘ + ╠═ guilds + β•‘ ╠═ guild_id + β•‘ ╠═ vc_id + β•‘ ╠═ vc_category_id + β•‘ ╠═ channel_limit + β•‘ β•šβ• channel_bitrate + β•‘ + β•šβ• rejected_users <- Unused -DATABASE_FILE = 'voice.db' # mhm, i think json might be better :> -# ^^^^^^ following up to this point, i hate my ideas, fuck json ._. - -# database tables: channels, guilds, rejected_users -# channels schema: CREATE TABLE "channels" ("user_id" INTEGER,"channel_id" INTEGER, "guild_id" INTEGER) -# guilds schema: CREATE TABLE "guilds" ("guild_id" INTEGER,"vc_id" INTEGER, "vc_category_id" INTEGER,"channel_limit" INTEGER, "channel_bitrate" INTEGER) -# rejected_users schema: CREATE TABLE "rejected_users" ("user_id" INTEGER,"channel_id" INTEGER, "guild_id" INTEGER) +""" +import aiosqlite +DATABASE_FILE = 'voice.db' class DataBase(): - """PREPARE YOURSELF TO SEE THE MOST DUMBEST CLASS EVER!""" # FIXME... pls :3 + """PREPARE YOURSELF TO SEE THE MOST DUMBEST CLASS EVER!""" # FIXME... def __init__(self): pass + + @classmethod + async def create_conn(self): + return await aiosqlite.connect(DATABASE_FILE) @classmethod async def get_guild(self, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as db: - async with db.execute("SELECT * FROM guilds WHERE guild_id = ?", (guild_id,)) as cursor: - return await cursor.fetchall() + db = await self.create_conn() + async with db.execute("SELECT * FROM guilds WHERE guild_id = ?", (guild_id,)) as cursor: + return await cursor.fetchall() + @classmethod async def create_channel(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("INSERT INTO channels VALUES (?,?,?)", (user_id, channel_id, guild_id)) as cursor: - await conn.commit() - return + db = await self.create_conn() + async with db.execute("INSERT INTO channels VALUES (?,?,?)", (user_id, channel_id, guild_id)): + return await db.commit() + @classmethod async def delete_channel(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("DELETE FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor: - await conn.commit() - return + db = await self.create_conn() + async with db.execute("DELETE FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)): + return await db.commit() + @classmethod async def get_channel(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - if not user_id: - async with conn.execute("SELECT * FROM channels WHERE channel_id = ? AND guild_id = ?", (channel_id, guild_id,)) as cursor: - return await cursor.fetchone() - else: - async with conn.execute("SELECT * FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor: - return await cursor.fetchone() + query, values = ("SELECT * FROM channels WHERE channel_id = ? AND guild_id = ?", (channel_id, guild_id,)) if not user_id else ("SELECT * FROM channels WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) + db = await self.create_conn() + async with db.execute(query, values) as cursor: + return await cursor.fetchone() @classmethod async def get_rejected_user(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("SELECT * FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor: - return await cursor.fetchone() + db = await self.create_conn() + async with db.execute("SELECT * FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor: + return await cursor.fetchone() + @classmethod async def add_user_to_rejected(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("INSERT INTO rejected_users VALUES (?,?,?)", (user_id, channel_id, guild_id)) as cursor: - await conn.commit() - return + db = await self.create_conn() + async with db.execute("INSERT INTO rejected_users VALUES (?,?,?)", (user_id, channel_id, guild_id)): + return await db.commit() + @classmethod async def remove_user_from_reject(self, user_id, channel_id, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("DELETE FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)) as cursor: - await conn.commit() - return + db = await self.create_conn() + async with db.execute("DELETE FROM rejected_users WHERE user_id = ? AND channel_id = ? AND guild_id = ?", (user_id, channel_id, guild_id)): + return await db.commit() @classmethod async def add_guild(self, guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("INSERT INTO guilds VALUES (?,?,?,?,?)", (guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate)) as cursor: - await conn.commit() - return + db = await self.create_conn() + async with db.execute("INSERT INTO guilds VALUES (?,?,?,?,?)", (guild_id, vc_id, vc_category_id, channel_limit, channel_bitrate)): + return await db.commit() + @classmethod async def remove_guild(self, guild_id, vc_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("DELETE FROM guilds WHERE guild_id = ? AND vc_id = ?", (guild_id, vc_id,)) as cursor: - await conn.commit() - return - @classmethod - async def get_guilds(self, guild_id): - async with aiosqlite.connect(DATABASE_FILE) as conn: - async with conn.execute("SELECT * FROM guilds WHERE guild_id = ?", (guild_id,)) as cursor: - return await cursor.fetchall() - -# TODO: This Class intented to be refactored one day ;3 + db = await self.create_conn() + async with db.execute("DELETE FROM guilds WHERE guild_id = ? AND vc_id = ?", (guild_id, vc_id,)): + return await db.commit()