Improved type-hinting and overall sanity checks implemented.

This commit is contained in:
kku
2024-12-16 20:34:37 +01:00
parent 5c763fc02e
commit c05cf64ae0
14 changed files with 193 additions and 165 deletions

View File

@@ -1,14 +1,23 @@
import logging
import sys
from typing import Union
from discord import ApplicationContext, Embed, User, option, slash_command
from discord import (
ApplicationContext,
Embed,
User,
option,
slash_command,
Role,
TextChannel,
)
from discord import utils as ds_utils
from discord.ext import commands
from libbot import config_get
from libbot.pycord.classes import PycordBot
from libbot.sync import config_get as sync_config_get
from enums.colors import Color
from enums import Color
from modules.scheduled import scheduler
from modules.utils_sync import guild_name
from modules.waifu_pics import waifu_pics
@@ -20,7 +29,7 @@ class Admin(commands.Cog):
"""Cog with utility commands for admins."""
def __init__(self, client: PycordBot):
self.client = client
self.client: PycordBot = client
# Disabled because warning functionality is temporarily not needed
# @slash_command(
@@ -117,7 +126,7 @@ class Admin(commands.Cog):
ctx: ApplicationContext,
amount: int,
user: User,
):
) -> None:
if ctx.user.id in self.client.owner_ids:
logging.info(
"User %s removed %s message(s) in %s",
@@ -148,46 +157,51 @@ class Admin(commands.Cog):
embed=Embed(
title="Відмовлено в доступі",
description="Здається, це команда лише для модераторів",
color=Color.fail,
color=Color.FAIL,
)
)
mod_role = ds_utils.get(
mod_role: Union[Role, None] = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles")
)
admin_chan = ds_utils.get(
admin_chan: Union[TextChannel, None] = ds_utils.get(
ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"),
)
await admin_chan.send(
content=f"{mod_role.mention}",
embed=Embed(
title="Неавторизований запит",
description=f"Користувач {ctx.user.mention} запитав у каналі {ctx.channel.mention} команду, до якої не повинен мати доступу/бачити.",
color=Color.fail,
),
)
if admin_chan is not None:
await admin_chan.send(
content="" if mod_role is None else mod_role.mention,
embed=Embed(
title="Неавторизований запит",
description=f"Користувач {ctx.user.mention} запитав у каналі {ctx.channel.mention} команду, до якої не повинен мати доступу/бачити.",
color=Color.FAIL,
),
)
@slash_command(
name="reboot",
description="Перезапустити бота",
guild_ids=[sync_config_get("guild")],
)
async def reboot_cmd(self, ctx: ApplicationContext):
async def reboot_cmd(self, ctx: ApplicationContext) -> None:
await ctx.defer(ephemeral=True)
if ctx.user.id in self.client.owner_ids:
logging.info("Calling shutdown initiated by %s", guild_name(ctx.user))
await ctx.respond(
embed=Embed(
title="Вимикаюсь...",
description="Спробую перезавантажитись за 5 секунд",
)
)
scheduler.shutdown()
await self.client.close()
await waifu_pics._client_session.close()
sys.exit()
logging.warning(
@@ -199,27 +213,28 @@ class Admin(commands.Cog):
embed=Embed(
title="Відмовлено в доступі",
description="Здається, це команда лише для модераторів",
color=Color.fail,
color=Color.FAIL,
)
)
mod_role = ds_utils.get(
mod_role: Union[Role, None] = ds_utils.get(
ctx.user.guild.roles, id=await config_get("moderators", "roles")
)
admin_chan = ds_utils.get(
admin_chan: Union[TextChannel, None] = ds_utils.get(
ctx.user.guild.channels,
id=await config_get("adminchat", "channels", "text"),
)
await admin_chan.send(
content=f"{mod_role.mention}",
embed=Embed(
title="Неавторизований запит",
description=f"Користувач {ctx.user.mention} запитав у каналі {ctx.channel.mention} команду, до якої не повинен мати доступу/бачити.",
color=Color.fail,
),
)
if admin_chan is not None:
await admin_chan.send(
content="" if mod_role is None else mod_role.mention,
embed=Embed(
title="Неавторизований запит",
description=f"Користувач {ctx.user.mention} запитав у каналі {ctx.channel.mention} команду, до якої не повинен мати доступу/бачити.",
color=Color.FAIL,
),
)
def setup(client: PycordBot):
def setup(client: PycordBot) -> None:
client.add_cog(Admin(client))