Skip to content

Commit

Permalink
wip - code rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
camalot committed Sep 24, 2023
1 parent 21055e7 commit e2e162e
Show file tree
Hide file tree
Showing 28 changed files with 769 additions and 134 deletions.
3 changes: 1 addition & 2 deletions bot/cogs/_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ async def on_voice_state_update(self, member, before, after):
except Exception as ex:
self.log.error(guild_id, _method , str(ex), traceback.format_exc())


@voice.command()
async def version(self, ctx):
author = ctx.author
Expand Down Expand Up @@ -1609,7 +1608,6 @@ async def _name(self, ctx, name: str = None, saveSettings: bool = True):
self.db.close()
await ctx.message.delete()


@voice.command(aliases=["rename"])
async def force_name(self, ctx, *, name: str = None):
_method = inspect.stack()[1][3]
Expand Down Expand Up @@ -2303,6 +2301,7 @@ def set_guild_strings(self, guildId: int):

# for x in self.strings[str(guildId)]:
# self.log.debug(guildId, _method, self.get_string(guildId, x))

def get_string(self, guildId: int, key: str):
_method = inspect.stack()[1][3]
if not key:
Expand Down
381 changes: 381 additions & 0 deletions bot/cogs/channel.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bot/cogs/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def help(self, ctx, command: str = "", subcommand: str = ""):
await self.root_help(ctx)
else:
await self.subcommand_help(ctx, command, subcommand)

async def subcommand_help(self, ctx, command: str = "", subcommand: str = ""):
_method = inspect.stack()[1][3]
if ctx.guild:
Expand Down
22 changes: 11 additions & 11 deletions bot/cogs/lib/CategorySelectView.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def __init__(self, ctx, placeholder: str, categories, allow_none: bool = False,

class CategorySelectView(discord.ui.View):
def __init__(
self,
ctx,
placeholder: str,
categories,
select_callback=None,
timeout_callback=None,
allow_none: bool = False,
allow_new: bool = False,
timeout: int = 180
) -> None:
self,
ctx,
placeholder: str,
categories,
select_callback=None,
timeout_callback=None,
allow_none: bool = False,
allow_new: bool = False,
timeout: int = 180
) -> None:
super().__init__(timeout=timeout)
_method = inspect.stack()[0][3]
# get the file name without the extension and without the directory
Expand All @@ -76,7 +76,7 @@ async def on_select(self, interaction: discord.Interaction) -> None:
_method = inspect.stack()[0][3]
if interaction.user.id != self.ctx.author.id:
return

self.log.debug(self.ctx.guild.id, f"{self._module}.{_method}", "Item Selected")
if self.select_callback is not None:
await self.select_callback(self, interaction)
Expand Down
18 changes: 9 additions & 9 deletions bot/cogs/lib/ChannelSelect.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ def __init__(self, ctx, placeholder: str, channels, allow_none: bool = False) ->

class ChannelSelectView(discord.ui.View):
def __init__(
self,
ctx,
placeholder: str,
channels,
select_callback=None,
timeout_callback=None,
allow_none: bool = False,
timeout: int = 180
) -> None:
self,
ctx,
placeholder: str,
channels,
select_callback=None,
timeout_callback=None,
allow_none: bool = False,
timeout: int = 180
) -> None:
super().__init__(timeout=timeout)
_method = inspect.stack()[0][3]
# get the file name without the extension and without the directory
Expand Down
2 changes: 1 addition & 1 deletion bot/cogs/lib/RoleSelectView.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(

async def on_select_callback(self, interaction: discord.Interaction) -> None:
if interaction.user.id != self.ctx.author.id:
return
return
if self.select_callback is not None:
await self.select_callback(self, interaction)
self.stop()
Expand Down
2 changes: 1 addition & 1 deletion bot/cogs/lib/YesOrNoView.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
async def answer_callback(self, interaction: discord.Interaction) -> None:
# check if the user who clicked the button is the same as the user who started the command
if interaction.user.id != self.ctx.author.id:
return
return

await interaction.delete_original_response()
if self.button_answer_callback is not None:
Expand Down
4 changes: 0 additions & 4 deletions bot/cogs/lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ def get_all_from_voice_channel_table(self):
pass
def get_all_guild_settings(self):
pass
def insert_log(self, guildId: int, level: str, method: str, message: str, stack: str = None):
pass
def clear_log(self, guildId: int):
pass

def UPDATE_SCHEMA(self):
pass
9 changes: 5 additions & 4 deletions bot/cogs/lib/logger.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys
import typing

from bot.cogs.lib import loglevel, mongo
from bot.cogs.lib import loglevel
from bot.cogs.lib.mongodb.logs import LogsMongoDatabase
from bot.cogs.lib.colors import Colors


class Log():
class Log:
def __init__(self, minimumLogLevel: loglevel.LogLevel = loglevel.LogLevel.DEBUG):
self.db = mongo.MongoDatabase()
self.db = LogsMongoDatabase()
self.minimum_log_level = minimumLogLevel

def __write(
Expand Down Expand Up @@ -41,7 +42,7 @@ def debug(self, guildId: int, method: str, message: str, stackTrace: typing.Opti
stackTrace=stackTrace,
file=sys.stdout,
)

def info(self, guildId: int, method: str, message: str, stackTrace: typing.Optional[str] = None):
self.__write(
guildId=guildId,
Expand Down
10 changes: 5 additions & 5 deletions bot/cogs/lib/loglevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ class LogLevel(Enum):

def __ge__(self, other):
if self.__class__ is other.__class__:
return self.value >= other.value
return self.value >= other.value
return NotImplemented

def __gt__(self, other):
if self.__class__ is other.__class__:
return self.value > other.value
return self.value > other.value
return NotImplemented

def __le__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return self.value < other.value
return NotImplemented

def __lt__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return self.value < other.value
return NotImplemented

def __eq__(self, other):
if self.__class__ is other.__class__:
return self.value == other.value
return self.value == other.value
return NotImplemented
33 changes: 0 additions & 33 deletions bot/cogs/lib/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,6 @@ def close(self):
print(ex)
traceback.print_exc()

def insert_log(
self,
guildId: int,
level: loglevel.LogLevel,
method: str,
message: str,
stackTrace: typing.Optional[str] = None
):
try:
if self.connection is None:
self.open()
payload = {
"guild_id": guildId,
"timestamp": utils.get_timestamp(),
"level": level.name,
"method": method,
"message": message,
"stack_trace": stackTrace
}
self.connection.logs.insert_one(payload)
except Exception as ex:
print(ex)
traceback.print_exc()

def clear_log(self, guildId):
try:
if self.connection is None:
self.open()
self.connection.logs.delete_many({ "guild_id": guildId })
except Exception as ex:
print(ex)
traceback.print_exc()

def get_tracked_voice_channel_ids(self, guildId):
try:
if self.connection is None:
Expand Down
94 changes: 94 additions & 0 deletions bot/cogs/lib/mongodb/channels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import inspect
import traceback
import os
import typing

from bot.cogs.lib import loglevel, utils
from bot.cogs.lib.mongodb.database import Database
from pymongo import MongoClient

class ChannelsDatabase(Database):
def __init__(self):
super().__init__()
self._module = os.path.basename(__file__)[:-3]
self._class = self.__class__.__name__

self.db_url = utils.dict_get(os.environ, "VCB_MONGODB_URL", default_value="")
self.db_name = utils.dict_get(os.environ, "VCB_MONGODB_DBNAME", default_value="voicecreate_v2")

self.client: typing.Optional[MongoClient] = None
self.connection: typing.Optional[typing.Any] = None
pass

def get_tracked_channel_owner(self, guildId, voiceChannelId):
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()
owner = self.connection.voice_channels.find_one(
{"guildID": guildId, "voiceID": voiceChannelId}, { "userID": 1 }
)
if owner:
return owner['userID']
return None
except Exception as ex:
self.log(
guildId,
loglevel.LogLevel.ERROR,
f"{self._module}.{self._class}.{_method}",
f"{ex}",
traceback.format_exc(),
)

def update_tracked_channel_owner(self, guildId, voiceChannelId, ownerId, newOwnerId):
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()
self.connection.voice_channels.update_one(
{"guildID": guildId, "voiceID": voiceChannelId, "userID": ownerId}, {"$set": { "userID": newOwnerId }}
)
self.connection.text_channels.update_one(
{"guildID": guildId, "voiceID": voiceChannelId, "userID": ownerId}, {"$set": { "userID": newOwnerId }}
)
except Exception as ex:
self.log(
guildId,
loglevel.LogLevel.ERROR,
f"{self._module}.{self._class}.{_method}",
f"{ex}",
traceback.format_exc(),
)

def get_channel_owner_id(self, guildId, channelId):
_method = inspect.stack()[0][3]
try:
if self.connection is None:
self.open()
item = self.connection.voice_channels.find_one({"guildID": guildId, "voiceID": channelId}, {"userID": 1})
if item:
return int(item['userID'])
item = self.connection.text_channels.find_one({"guildID": guildId, "channelID": channelId}, {"userID": 1})
if item:
return int(item['userID'])
return None
except Exception as ex:
self.log(
guildId,
loglevel.LogLevel.ERROR,
f"{self._module}.{self._class}.{_method}",
f"{ex}",
traceback.format_exc(),
)

def get_text_channel_id(self, guildId, voiceChannelId):
try:
if self.connection is None:
self.open()
result = self.connection.text_channels.find_one({"guildID": guildId, "voiceID": voiceChannelId})
if result:
return result['channelID']
return None
except Exception as ex:
print(ex)
traceback.print_exc()
Loading

0 comments on commit e2e162e

Please sign in to comment.