Skip to content

Commit

Permalink
refact(ctx): reduce usage of MyContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Jul 27, 2024
1 parent 9b2a043 commit 024650b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 61 deletions.
19 changes: 8 additions & 11 deletions core/checks/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@ async def is_bot_admin(ctx: MyContext | discord.Interaction | discord.User):
user = user.id
return user in admins_id

async def is_support_staff(ctx: MyContext | discord.Interaction) -> bool:
async def is_support_staff(interaction: discord.Interaction[Axobot]) -> bool:
"Check if the user is one of the bot staff, either by flag or by role"
user = ctx.author if isinstance(ctx, commands.Context) else ctx.user
if user.id in admins_id:
if interaction.user.id in admins_id:
return True
bot = ctx.bot if isinstance(ctx, commands.Context) else ctx.client
if users_cog := bot.get_cog("Users"):
return await users_cog.has_userflag(user, "support")
server = bot.get_guild(SUPPORT_GUILD_ID.id)
if users_cog := interaction.client.get_cog("Users"):
return await users_cog.has_userflag(interaction.user, "support")
server = interaction.client.get_guild(SUPPORT_GUILD_ID.id)
if server is not None:
member = server.get_member(user.id)
member = server.get_member(interaction.user.id)
role = server.get_role(412340503229497361)
if member is not None and role is not None:
return role in member.roles
return False

async def database_connected(ctx: MyContext | discord.Interaction[Axobot]) -> bool:
async def database_connected(interaction: discord.Interaction[Axobot]) -> bool:
"Check if the database is online and accessible"
bot = ctx.client if isinstance(ctx, discord.Interaction) else ctx.bot
if bot.database_online:
if interaction.client.database_online:
return True
raise commands.CommandError("Database offline")

Expand Down
40 changes: 0 additions & 40 deletions modules/admin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def __init__(self, bot: Axobot):
self.update = {"fr": None, "en": None}
self._last_result = None
self._upvote_emojis = ()
self.god_mode = []

@property
def upvote_emojis(self):
Expand All @@ -74,15 +73,6 @@ def upvote_emojis(self):
async def check_if_admin(self, interaction: discord.Interaction):
return await checks.is_bot_admin(interaction)

async def check_if_god(self, ctx: discord.User | discord.Guild | MyContext):
"Check if a user is in God mode for a given context"
if isinstance(ctx, discord.User):
return await checks.is_bot_admin(ctx)
elif isinstance(ctx.guild, discord.Guild) and ctx.guild is not None:
return await checks.is_bot_admin(ctx) and ctx.guild.id in self.god_mode
else:
return await checks.is_bot_admin(ctx)

async def add_success_reaction(self, msg: discord.Message):
"Add a check reaction to a message"
if self.bot.zombie_mode:
Expand Down Expand Up @@ -134,36 +124,6 @@ async def sync_app_commands(self, interaction: discord.Interaction,
await self.bot.send_embed(emb)
await interaction.followup.send(txt + '!')

@admin_main.command(name="god")
async def enable_god_mode(self, interaction: discord.Interaction, enable: bool = True):
"""Get full powaaaaaa
Donne les pleins-pouvoirs aux admins du bot sur ce serveur (accès à toutes les commandes de modération)"""
if enable:
if interaction.guild_id not in self.god_mode:
self.god_mode.append(interaction.guild_id)
await interaction.response.send_message(
"<:nitro:548569774435598346> Mode superadmin activé sur ce serveur",
ephemeral=True
)
else:
await interaction.response.send_message(
"Mode superadmin déjà activé sur ce serveur",
ephemeral=True
)
else:
if interaction.guild_id in self.god_mode:
self.god_mode.remove(interaction.guild_id)
await interaction.response.send_message(
"Mode superadmin désactivé sur ce serveur",
ephemeral=True
)
else:
await interaction.response.send_message(
"Ce mode n'est pas actif ici",
ephemeral=True
)

@admin_main.command(name="faq")
async def send_faq(self, interaction: discord.Interaction):
"Update the FAQ channels from the private preparation channels"
Expand Down
6 changes: 1 addition & 5 deletions modules/info/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,9 @@ async def get_members_repartition(self, members: list[discord.Member]):
description="Help the bot staff to find things",
guild_ids=[PRIVATE_GUILD_ID.id]
)
find_main.interaction_check = checks.is_support_staff

@find_main.command(name="user")
@discord.app_commands.check(checks.is_support_staff)
async def find_user(self, interaction: discord.Interaction, user: discord.User):
"Find any user visible by the bot"
# Servers list
Expand Down Expand Up @@ -1026,7 +1026,6 @@ async def find_user(self, interaction: discord.Interaction, user: discord.User):
await interaction.followup.send(embed=embed)

@find_main.command(name="guild")
@discord.app_commands.check(checks.is_support_staff)
@discord.app_commands.describe(guild="The server name or ID")
async def find_guild(self, interaction: discord.Interaction, guild: str):
"Find any guild where the bot is"
Expand Down Expand Up @@ -1084,7 +1083,6 @@ async def find_guild(self, interaction: discord.Interaction, guild: str):
await interaction.followup.send(embed=emb)

@find_main.command(name="channel")
@discord.app_commands.check(checks.is_support_staff)
@discord.app_commands.describe(channel="The ID/name of the channel to look for")
async def find_channel(self, interaction: discord.Interaction, channel: str):
"Find any channel from any server where the bot is"
Expand All @@ -1106,7 +1104,6 @@ async def find_channel(self, interaction: discord.Interaction, channel: str):
await interaction.followup.send(embed=emb)

@find_main.command(name="role")
@discord.app_commands.check(checks.is_support_staff)
@discord.app_commands.describe(role_name="The ID/name of the role to look for")
async def find_role(self, interaction: discord.Interaction, role_name: str):
"Find any role from any server where the bot is"
Expand All @@ -1130,7 +1127,6 @@ async def find_role(self, interaction: discord.Interaction, role_name: str):
await interaction.followup.send(embed=emb)

@find_main.command(name="rss")
@discord.app_commands.check(checks.is_support_staff)
async def find_rss(self, interaction: discord.Interaction, feed_id: int):
"Find any active or inactive RSS feed"
await interaction.response.defer()
Expand Down
7 changes: 4 additions & 3 deletions modules/languages/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import i18n
from asyncache import cached
from cachetools import TTLCache
from discord.ext import commands

from core.bot_classes import Axobot, MyContext
from core.bot_classes import Axobot
from core.translator import AxobotTranslator

SourceType = (
Expand All @@ -17,7 +18,7 @@
| discord.DMChannel
| discord.Interaction
| discord.PartialMessageable
| MyContext
| commands.Context
)


Expand Down Expand Up @@ -71,7 +72,7 @@ async def tr(self, source: SourceType, string_id: str, **kwargs):
source = source.user
else:
source = None
elif isinstance(source, MyContext):
elif isinstance(source, commands.Context):
# get ID from guild
if source.guild:
source = source.guild.id
Expand Down
2 changes: 0 additions & 2 deletions modules/serverconfig/serverconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ async def check_member_config_permission(self, member: discord.Member, option_na
raise ValueError(f"Option {option_name} does not exist")
if (await self.get_options_list())[option_name]["type"] != "roles_list":
raise ValueError(f"Option {option_name} is not a roles list")
if await self.bot.get_cog("Admin").check_if_god(member):
return True
if not self.bot.database_online or not isinstance(member, discord.Member):
return False
raw_roles = await self.get_raw_option(member.guild.id, option_name)
Expand Down

0 comments on commit 024650b

Please sign in to comment.