Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
[Other]: refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
nixxoq committed Nov 21, 2023
1 parent 829a226 commit 7ca17f2
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 125 deletions.
17 changes: 8 additions & 9 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def __init__(self, debug: bool = False) -> None:
self.i18n.load("src/utils/locale")
self.debug = debug


async def on_message(self, message: disnake.Message):
prefix = await get_prefix(message)
prefix_len = len(prefix)
Expand Down Expand Up @@ -61,10 +60,10 @@ async def on_ready(self) -> None:
event_name = extension[:-3]
self.load_extension(f"src.cogs.{event_name}")
except (
commands.ExtensionNotFound,
commands.NoEntryPointError,
commands.ExtensionFailed,
commands.ExtensionError,
commands.ExtensionNotFound,
commands.NoEntryPointError,
commands.ExtensionFailed,
commands.ExtensionError,
) as e:
logging.error(
f"\n\nFailed to load {extension}!\n{traceback.print_exception(e)}"
Expand All @@ -80,10 +79,10 @@ async def on_ready(self) -> None:
try:
self.load_extension(f"src.events.{event[:-3]}")
except (
commands.ExtensionNotFound,
commands.NoEntryPointError,
commands.ExtensionFailed,
commands.ExtensionError,
commands.ExtensionNotFound,
commands.NoEntryPointError,
commands.ExtensionFailed,
commands.ExtensionError,
) as e:
logging.error(
f"\n\nFailed to load {extension}!\n{traceback.print_exception(e)}"
Expand Down
82 changes: 38 additions & 44 deletions src/cogs/GiveawaysCog.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ async def end_gw(self, i):

if len(users) > 0:
prize = i.get("prize")
await message.reply(
f"{winners} congratulations, you have won **{prize}**!"
)
await message.reply(f"{winners} congratulations, you have won **{prize}**!")

@commands.slash_command(
name=Localized("giveaway", key="GIVEAWAY_COMMAND_NAME"),
Expand All @@ -66,27 +64,27 @@ async def giveaway(self, interaction):
name=Localized("create", key="GIVEAWAY_CREATE_COMMAND_NAME"),
)
async def create(
self,
interaction: disnake.ApplicationCommandInteraction,
prize: str = commands.Param(
description=Localized(
"Choice prize", key="GIVEAWAY_CREATE_COMMAND_PRIZE_DESC"
),
name=Localized("prize", key="GIVEAWAY_CREATE_COMMAND_PRIZE_NAME"),
self,
interaction: disnake.ApplicationCommandInteraction,
prize: str = commands.Param(
description=Localized(
"Choice prize", key="GIVEAWAY_CREATE_COMMAND_PRIZE_DESC"
),
winners: int = commands.Param(
description=Localized(
"Winners", key="GIVEAWAY_CREATE_COMMAND_WINNERS_DESC"
),
name=Localized("winners", key="GIVEAWAY_CREATE_COMMAND_WINNERS_NAME"),
name=Localized("prize", key="GIVEAWAY_CREATE_COMMAND_PRIZE_NAME"),
),
winners: int = commands.Param(
description=Localized(
"Winners", key="GIVEAWAY_CREATE_COMMAND_WINNERS_DESC"
),
duration: str = commands.Param(
description=Localized(
"Example: 1d1h1m1s (1 day, 1 hour, 1 minute, 1 second)",
key="GIVEAWAY_CREATE_COMMAND_DURATION_DESC",
),
name=Localized("duration", key="GIVEAWAY_CREATE_COMMAND_DURATION_NAME"),
name=Localized("winners", key="GIVEAWAY_CREATE_COMMAND_WINNERS_NAME"),
),
duration: str = commands.Param(
description=Localized(
"Example: 1d1h1m1s (1 day, 1 hour, 1 minute, 1 second)",
key="GIVEAWAY_CREATE_COMMAND_DURATION_DESC",
),
name=Localized("duration", key="GIVEAWAY_CREATE_COMMAND_DURATION_NAME"),
),
):
duration_regex = r"(\d+)([dhms])"

Expand Down Expand Up @@ -138,22 +136,22 @@ async def create(
name=Localized("reroll", key="GIVEAWAY_REROLL_COMMAND_NAME"),
)
async def reroll(
self,
interaction,
message_id=commands.Param(
description=Localized(
"Message ID", key="GIVEAWAY_REROLL_COMMAND_MESSAGEID_DESC"
),
name=Localized("message_id", key="GIVEAWAY_REROLL_COMMAND_MESSAGEID_NAME"),
self,
interaction,
message_id=commands.Param(
description=Localized(
"Message ID", key="GIVEAWAY_REROLL_COMMAND_MESSAGEID_DESC"
),
winners: int = commands.Param(
description=Localized(
"Choice winners (default: 1)",
key="GIVEAWAY_REROLL_COMMAND_WINNERS_DESC",
),
name=Localized("winners", key="GIVEAWAY_REROLL_COMMAND_WINNERS_NAME"),
default=1,
name=Localized("message_id", key="GIVEAWAY_REROLL_COMMAND_MESSAGEID_NAME"),
),
winners: int = commands.Param(
description=Localized(
"Choice winners (default: 1)",
key="GIVEAWAY_REROLL_COMMAND_WINNERS_DESC",
),
name=Localized("winners", key="GIVEAWAY_REROLL_COMMAND_WINNERS_NAME"),
default=1,
),
):
try:
giveaway_msg = await interaction.channel.fetch_message(message_id)
Expand All @@ -163,8 +161,8 @@ async def reroll(

if giveaway_msg.author == self.bot:
if (
not giveaway_msg.embeds
or not giveaway_msg.embeds[0].title == "Giveaway"
not giveaway_msg.embeds
or not giveaway_msg.embeds[0].title == "Giveaway"
):
await interaction.send(
"The above message is not a giveaway message.", ephemeral=True
Expand Down Expand Up @@ -207,13 +205,9 @@ async def check_gw(self):
await self.bot.wait_until_ready()

async for giveaway_data in self.giveaway_db.collection.find({}):
channel: Optional[disnake.TextChannel] = (
self.bot.get_channel(giveaway_data["channel_id"])
or
await self.bot.fetch_channel(
giveaway_data["channel_id"]
)
)
channel: Optional[disnake.TextChannel] = self.bot.get_channel(
giveaway_data["channel_id"]
) or await self.bot.fetch_channel(giveaway_data["channel_id"])
try:
await channel.fetch_message(giveaway_data["message_id"])
except (disnake.NotFound, disnake.Forbidden, disnake.HTTPException):
Expand Down
8 changes: 3 additions & 5 deletions src/cogs/ModerationCog.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ async def refresh_embed():
)

start = index * per_page
for page in pages[start:(index + 1) * per_page]:
for page in pages[start : (index + 1) * per_page]:
mutes_embed.add_field(name="", value=page, inline=False)
return mutes_embed

Expand Down Expand Up @@ -453,7 +453,7 @@ async def refresh_embed():
bans_embed.set_footer(text=f"{ctx.author}", icon_url=ctx.author.avatar)

start = index * per_page
for page in pages[start: (index + 1) * per_page]:
for page in pages[start : (index + 1) * per_page]:
bans_embed.add_field(name="", value=page, inline=False)
return bans_embed

Expand Down Expand Up @@ -565,9 +565,7 @@ async def refresh_buttons(ind: int, total_p: int):
)
return [prev_button, next_button, delete_button]

async def refresh_embed(
context: commands.Context, warns_list: List[Any], i, _
):
async def refresh_embed(context: commands.Context, warns_list: List[Any], i, _):
warns_embed = Embed(
title=f"Warns of {user}",
description=f"**Total warns count:** {len(warns_list)}",
Expand Down
28 changes: 14 additions & 14 deletions src/cogs/VoiceCog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

class MusicPlayer(mafic.Player[commands.Bot]):
def __init__(
self,
client: commands.Bot,
channel: disnake.VoiceChannel,
self,
client: commands.Bot,
channel: disnake.VoiceChannel,
) -> None:
super().__init__(client, channel)

Expand All @@ -31,7 +31,7 @@ def __init__(self, message_id: int, *, timeout: float | None = None) -> None:

@disnake.ui.button(label="Skip", style=disnake.ButtonStyle.green)
async def skip(
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
) -> None:
player: MusicPlayer

Expand All @@ -56,7 +56,7 @@ async def skip(

@disnake.ui.button(label="Resume/Pause", style=disnake.ButtonStyle.gray)
async def resume_and_pause(
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
):
player: MusicPlayer

Expand All @@ -70,7 +70,7 @@ async def resume_and_pause(

@disnake.ui.button(label="Queue", style=disnake.ButtonStyle.blurple)
async def queue(
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
self, _: disnake.ui.Button, interaction: disnake.MessageInteraction
):
player: MusicPlayer

Expand Down Expand Up @@ -298,22 +298,22 @@ async def on_track_start(self, event: mafic.TrackStartEvent) -> None:
async def on_message(self, message: disnake.Message):
if message.guild:
if rooms := await self.private_rooms.get_private_room(
message.guild.id, to_return="channels"
message.guild.id, to_return="channels"
):
for room in rooms:
if (
message.channel.id == room
and message.author.id != self.bot.user.id
message.channel.id == room
and message.author.id != self.bot.user.id
):
await message.delete()
break

@commands.Cog.listener()
async def on_voice_state_update(
self,
member: disnake.Member,
before: disnake.VoiceState,
after: disnake.VoiceState,
self,
member: disnake.Member,
before: disnake.VoiceState,
after: disnake.VoiceState,
):
if after.channel and len(after.channel.members) != 0:
is_main_room = await self.private_rooms.get_private_room(
Expand Down Expand Up @@ -348,7 +348,7 @@ async def on_voice_state_update(
return
elif before.channel and len(before.channel.members) == 0:
if room_channels := await self.private_rooms.get_private_room(
member.guild.id, to_return="channels"
member.guild.id, to_return="channels"
):
for room in room_channels:
if room_id := room.get("channel_id", None):
Expand Down
4 changes: 3 additions & 1 deletion src/events/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ async def on_guild_role_delete(self, role: disnake.Role) -> None:
await channel.send(embed=embed)

@commands.Cog.listener()
async def on_guild_role_update(self, before: disnake.Role, after: disnake.Role) -> None:
async def on_guild_role_update(
self, before: disnake.Role, after: disnake.Role
) -> None:
embed = disnake.Embed(
title="Synth | Updated Role",
color=self.settings_db.get_embed_color(before.guild.id),
Expand Down
4 changes: 1 addition & 3 deletions src/events/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ async def on_raw_bulk_message_delete(
await channel.send(embeds=embeds)

for file in range(0, len(files), 10):
await channel.send(
files=files[file: file + 10]
)
await channel.send(files=files[file : file + 10])

@commands.Cog.listener()
async def on_message_edit(
Expand Down
10 changes: 6 additions & 4 deletions src/utils/database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def _add_to_cache(self, param_filter: Dict[str, Any]) -> Any:
self.collection_cache[_id] = param_filter

if self.debug:
logging.info(
f"[{self.name}]: Added {_id} to cache\nValue: {param_filter}"
)
logging.info(f"[{self.name}]: Added {_id} to cache\nValue: {param_filter}")

return param_filter

Expand Down Expand Up @@ -107,7 +105,11 @@ async def get_items_in_db(
def get_items_in_cache(
self, query: Dict[Any, Any], to_return: str = None
) -> Union[
List[Dict[Union[int, str], Dict[str, Any]]], Dict[str, Any], Dict[str, Any], int, str
List[Dict[Union[int, str], Dict[str, Any]]],
Dict[str, Any],
Dict[str, Any],
int,
str,
]:
"""
Get items from cache by search query
Expand Down
Loading

0 comments on commit 7ca17f2

Please sign in to comment.