2023-05-06 18:56:25 +03:00
|
|
|
|
import logging
|
2023-05-06 19:48:04 +03:00
|
|
|
|
from os import makedirs
|
2023-05-06 18:56:25 +03:00
|
|
|
|
from pathlib import Path
|
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
2023-05-06 19:48:04 +03:00
|
|
|
|
from discord import ApplicationContext, Embed, File, option
|
2023-05-06 18:56:25 +03:00
|
|
|
|
from discord import utils as ds_utils
|
2023-05-06 19:48:04 +03:00
|
|
|
|
from discord.commands import SlashCommandGroup
|
2023-05-06 18:56:25 +03:00
|
|
|
|
from discord.ext import commands
|
2024-06-23 13:05:03 +03:00
|
|
|
|
from libbot.pycord.classes import PycordBot
|
2023-05-06 18:56:25 +03:00
|
|
|
|
|
2024-06-23 13:05:03 +03:00
|
|
|
|
from classes.holo_user import HoloUser
|
2023-05-06 18:56:25 +03:00
|
|
|
|
from enums.colors import Color
|
2023-05-06 19:48:04 +03:00
|
|
|
|
from modules.database import col_users
|
2023-05-06 18:56:25 +03:00
|
|
|
|
from modules.utils import config_get
|
|
|
|
|
from modules.utils_sync import config_get_sync, guild_name, json_write_sync
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Data(commands.Cog):
|
2024-06-23 13:05:03 +03:00
|
|
|
|
def __init__(self, client: PycordBot):
|
2023-05-06 18:56:25 +03:00
|
|
|
|
self.client = client
|
|
|
|
|
|
2023-05-06 19:48:04 +03:00
|
|
|
|
data = SlashCommandGroup("data", "Керування даними користувачів")
|
|
|
|
|
|
|
|
|
|
@data.command(
|
2023-05-06 18:56:25 +03:00
|
|
|
|
name="export",
|
|
|
|
|
description="Експортувати дані",
|
|
|
|
|
guild_ids=[config_get_sync("guild")],
|
|
|
|
|
)
|
2023-05-06 19:48:04 +03:00
|
|
|
|
@option(
|
|
|
|
|
"kind", description="Тип даних, які треба експортувати", choices=["Користувачі"]
|
|
|
|
|
)
|
|
|
|
|
async def data_export_cmd(self, ctx: ApplicationContext, kind: str):
|
2023-05-06 18:56:25 +03:00
|
|
|
|
await ctx.defer()
|
2023-05-08 16:45:00 +03:00
|
|
|
|
holo_user = HoloUser(ctx.author)
|
2024-06-23 13:05:03 +03:00
|
|
|
|
if (ctx.user.id in self.client.config["bot"]["owners"]) or (
|
2023-05-08 16:45:00 +03:00
|
|
|
|
await holo_user.is_council(ctx.author)
|
|
|
|
|
):
|
2023-05-06 18:56:25 +03:00
|
|
|
|
logging.info(
|
2024-06-23 13:05:03 +03:00
|
|
|
|
"Moderator %s exported current users list", guild_name(ctx.user)
|
2023-05-06 18:56:25 +03:00
|
|
|
|
)
|
|
|
|
|
makedirs("tmp", exist_ok=True)
|
|
|
|
|
uuid = str(uuid4())
|
|
|
|
|
|
2023-05-06 19:48:04 +03:00
|
|
|
|
if kind == "Користувачі":
|
|
|
|
|
users = []
|
|
|
|
|
|
|
|
|
|
for member in ctx.guild.members:
|
|
|
|
|
users.append(
|
|
|
|
|
{
|
|
|
|
|
"id": member.id,
|
|
|
|
|
"nick": member.nick,
|
|
|
|
|
"username": f"{member.name}#{member.discriminator}",
|
|
|
|
|
"bot": member.bot,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
json_write_sync(users, str(Path(f"tmp/{uuid}")))
|
2023-05-06 18:56:25 +03:00
|
|
|
|
|
2023-05-06 19:48:04 +03:00
|
|
|
|
await ctx.respond(
|
|
|
|
|
file=File(str(Path(f"tmp/{uuid}")), filename="users.json")
|
|
|
|
|
)
|
2023-05-06 18:56:25 +03:00
|
|
|
|
else:
|
|
|
|
|
logging.info(
|
2024-06-23 13:05:03 +03:00
|
|
|
|
"User %s tried to use /export but permission denied",
|
|
|
|
|
guild_name(ctx.user),
|
2023-05-06 18:56:25 +03:00
|
|
|
|
)
|
|
|
|
|
await ctx.respond(
|
|
|
|
|
embed=Embed(
|
2023-05-06 19:48:04 +03:00
|
|
|
|
title="Відмовлено в доступі",
|
|
|
|
|
description="Здається, це команда лише для модераторів",
|
|
|
|
|
color=Color.fail,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
mod_role = ds_utils.get(
|
2023-05-08 16:48:15 +03:00
|
|
|
|
ctx.user.guild.roles, id=await config_get("moderators", "roles")
|
2023-05-06 19:48:04 +03:00
|
|
|
|
)
|
|
|
|
|
admin_chan = 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,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@data.command(
|
|
|
|
|
name="migrate",
|
|
|
|
|
description="Мігрувати всіх користувачів до бази",
|
|
|
|
|
guild_ids=[config_get_sync("guild")],
|
|
|
|
|
)
|
|
|
|
|
@option(
|
|
|
|
|
"kind", description="Тип даних, які треба експортувати", choices=["Користувачі"]
|
|
|
|
|
)
|
|
|
|
|
async def data_migrate_cmd(self, ctx: ApplicationContext, kind: str):
|
|
|
|
|
await ctx.defer()
|
2023-05-08 16:45:00 +03:00
|
|
|
|
holo_user = HoloUser(ctx.author)
|
2024-06-23 13:05:03 +03:00
|
|
|
|
if (ctx.user.id in self.client.config["bot"]["owners"]) or (
|
2023-05-08 16:45:00 +03:00
|
|
|
|
await holo_user.is_council(ctx.author)
|
|
|
|
|
):
|
2023-05-06 19:48:04 +03:00
|
|
|
|
logging.info(
|
2024-06-23 13:05:03 +03:00
|
|
|
|
"Moderator %s started migration of all members to the database",
|
|
|
|
|
guild_name(ctx.user),
|
2023-05-06 19:48:04 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if kind == "Користувачі":
|
|
|
|
|
for member in ctx.guild.members:
|
|
|
|
|
if member.bot:
|
|
|
|
|
continue
|
|
|
|
|
if col_users.find_one({"user": member.id}) is None:
|
|
|
|
|
user = {}
|
|
|
|
|
defaults = await config_get("user", "defaults")
|
|
|
|
|
|
|
|
|
|
user["user"] = member.id
|
|
|
|
|
|
|
|
|
|
for key in defaults:
|
|
|
|
|
user[key] = defaults[key]
|
|
|
|
|
|
|
|
|
|
col_users.insert_one(document=user)
|
|
|
|
|
logging.info(
|
2024-06-23 13:05:03 +03:00
|
|
|
|
"Added DB record for user %s during migration", member.id
|
2023-05-06 19:48:04 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await ctx.respond(
|
|
|
|
|
embed=Embed(
|
|
|
|
|
title="Міграцію завершено",
|
|
|
|
|
description="Всім користувачам сервера було створено записи в базі даних.",
|
|
|
|
|
color=Color.success,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
logging.info(
|
2024-06-23 13:05:03 +03:00
|
|
|
|
"User %s tried to use /migrate but permission denied",
|
|
|
|
|
guild_name(ctx.user),
|
2023-05-06 19:48:04 +03:00
|
|
|
|
)
|
|
|
|
|
await ctx.respond(
|
|
|
|
|
embed=Embed(
|
2023-05-06 18:56:25 +03:00
|
|
|
|
title="Відмовлено в доступі",
|
|
|
|
|
description="Здається, це команда лише для модераторів",
|
|
|
|
|
color=Color.fail,
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
mod_role = ds_utils.get(
|
2023-05-08 16:48:15 +03:00
|
|
|
|
ctx.user.guild.roles, id=await config_get("moderators", "roles")
|
2023-05-06 18:56:25 +03:00
|
|
|
|
)
|
|
|
|
|
admin_chan = 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,
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-06-23 13:05:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup(client: PycordBot):
|
|
|
|
|
client.add_cog(Data(client))
|