import logging from datetime import timedelta from os import getpid, makedirs, system from pathlib import Path from uuid import uuid4 from discord import ApplicationContext, Embed, Option, User, slash_command, File from discord import utils as ds_utils from discord.ext import commands from classes.holo_user import HoloUser from enums.colors import Color 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): def __init__(self, client): self.client = client @slash_command( name="export", description="Експортувати дані", guild_ids=[config_get_sync("guild")], ) async def warn_cmd( self, ctx: ApplicationContext, ): await ctx.defer() if ctx.user.id in await config_get("admins"): logging.info( f"Moderator {guild_name(ctx.user)} exported current users list" ) makedirs("tmp", exist_ok=True) uuid = str(uuid4()) 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}"))) await ctx.respond(file=File(str(Path(f"tmp/{uuid}")), filename="users.json")) else: logging.info( f"User {guild_name(ctx.user)} tried to use /export but permission denied" ) await ctx.respond( embed=Embed( title="Відмовлено в доступі", description="Здається, це команда лише для модераторів", color=Color.fail, ) ) mod_role = ds_utils.get( ctx.user.guild.roles, id=await config_get("moderator", "roles") ) 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, ), )