Improved type-hinting and overall sanity checks implemented.
This commit is contained in:
59
cogs/data.py
59
cogs/data.py
@@ -1,9 +1,10 @@
|
||||
import logging
|
||||
from os import makedirs
|
||||
from pathlib import Path
|
||||
from typing import Union, List, Dict, Any
|
||||
from uuid import uuid4
|
||||
|
||||
from discord import ApplicationContext, Embed, File, option
|
||||
from discord import ApplicationContext, Embed, File, option, Role, TextChannel
|
||||
from discord import utils as ds_utils
|
||||
from discord.commands import SlashCommandGroup
|
||||
from discord.ext import commands
|
||||
@@ -13,7 +14,7 @@ from libbot.sync import config_get as sync_config_get
|
||||
from libbot.sync import json_write as sync_json_write
|
||||
|
||||
from classes.holo_user import HoloUser
|
||||
from enums.colors import Color
|
||||
from enums import Color
|
||||
from modules.database import col_users
|
||||
from modules.utils_sync import guild_name
|
||||
|
||||
@@ -22,9 +23,9 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class Data(commands.Cog):
|
||||
def __init__(self, client: PycordBot):
|
||||
self.client = client
|
||||
self.client: PycordBot = client
|
||||
|
||||
data = SlashCommandGroup("data", "Керування даними користувачів")
|
||||
data: SlashCommandGroup = SlashCommandGroup("data", "Керування даними користувачів")
|
||||
|
||||
@data.command(
|
||||
name="export",
|
||||
@@ -34,9 +35,8 @@ class Data(commands.Cog):
|
||||
@option(
|
||||
"kind", description="Тип даних, які треба експортувати", choices=["Користувачі"]
|
||||
)
|
||||
async def data_export_cmd(self, ctx: ApplicationContext, kind: str):
|
||||
async def data_export_cmd(self, ctx: ApplicationContext, kind: str) -> None:
|
||||
await ctx.defer()
|
||||
holo_user = HoloUser(ctx.author)
|
||||
|
||||
# Return if the user is not an owner and not in the council
|
||||
if (ctx.user.id not in self.client.owner_ids) and not (
|
||||
@@ -51,24 +51,24 @@ class Data(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}",
|
||||
content="" if mod_role is None else mod_role.mention,
|
||||
embed=Embed(
|
||||
title="Неавторизований запит",
|
||||
description=f"Користувач {ctx.user.mention} запитав у каналі {ctx.channel.mention} команду, до якої не повинен мати доступу/бачити.",
|
||||
color=Color.fail,
|
||||
color=Color.FAIL,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -78,10 +78,10 @@ class Data(commands.Cog):
|
||||
|
||||
makedirs("tmp", exist_ok=True)
|
||||
|
||||
uuid = str(uuid4())
|
||||
uuid: str = str(uuid4())
|
||||
|
||||
if kind == "Користувачі":
|
||||
users = []
|
||||
users: List[Dict[str, Any]] = []
|
||||
|
||||
for member in ctx.guild.members:
|
||||
users.append(
|
||||
@@ -105,7 +105,7 @@ class Data(commands.Cog):
|
||||
@option(
|
||||
"kind", description="Тип даних, які треба експортувати", choices=["Користувачі"]
|
||||
)
|
||||
async def data_migrate_cmd(self, ctx: ApplicationContext, kind: str):
|
||||
async def data_migrate_cmd(self, ctx: ApplicationContext, kind: str) -> None:
|
||||
await ctx.defer()
|
||||
|
||||
# Return if the user is not an owner and not in the council
|
||||
@@ -121,26 +121,27 @@ class Data(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,
|
||||
),
|
||||
)
|
||||
|
||||
return
|
||||
|
||||
@@ -155,8 +156,8 @@ class Data(commands.Cog):
|
||||
continue
|
||||
|
||||
if (await col_users.find_one({"user": member.id})) is None:
|
||||
user = {}
|
||||
defaults = await config_get("user", "defaults")
|
||||
user: Dict[str, Any] = {}
|
||||
defaults: Dict[str, Any] = await config_get("user", "defaults")
|
||||
|
||||
user["user"] = member.id
|
||||
|
||||
@@ -173,10 +174,10 @@ class Data(commands.Cog):
|
||||
embed=Embed(
|
||||
title="Міграцію завершено",
|
||||
description="Всім користувачам сервера було створено записи в базі даних.",
|
||||
color=Color.success,
|
||||
color=Color.SUCCESS,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def setup(client: PycordBot):
|
||||
def setup(client: PycordBot) -> None:
|
||||
client.add_cog(Data(client))
|
||||
|
Reference in New Issue
Block a user