Skip to content

Commit

Permalink
Merge pull request #7 from DARSHTRON/main
Browse files Browse the repository at this point in the history
clean up and fixing, refactronig
  • Loading branch information
its-darsh authored Jun 27, 2023
2 parents 68726c8 + b5a9b7f commit 258db1a
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 71 deletions.
112 changes: 58 additions & 54 deletions cogs/voice/DataBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 16 additions & 4 deletions cogs/voice/Embeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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!
""")
27 changes: 14 additions & 13 deletions cogs/voice/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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))

0 comments on commit 258db1a

Please sign in to comment.