2023-05-06 18:56:15 +03:00
import logging
2024-12-16 00:21:41 +02:00
import sys
2024-12-16 21:34:37 +02:00
from typing import Union
from discord import (
ApplicationContext ,
Embed ,
User ,
option ,
slash_command ,
Role ,
TextChannel ,
)
2023-05-06 18:56:15 +03:00
from discord import utils as ds_utils
from discord . ext import commands
2024-12-16 00:36:48 +02:00
from libbot import config_get
2024-06-23 13:05:03 +03:00
from libbot . pycord . classes import PycordBot
2024-12-16 00:36:48 +02:00
from libbot . sync import config_get as sync_config_get
2023-05-06 18:56:15 +03:00
2024-12-16 21:34:37 +02:00
from enums import Color
2023-05-06 19:56:01 +03:00
from modules . scheduled import scheduler
2024-12-16 00:36:48 +02:00
from modules . utils_sync import guild_name
2024-12-16 21:00:32 +02:00
from modules . waifu_pics import waifu_pics
2023-05-06 18:56:15 +03:00
logger = logging . getLogger ( __name__ )
class Admin ( commands . Cog ) :
2024-12-16 00:21:41 +02:00
""" Cog with utility commands for admins. """
2024-06-23 13:05:03 +03:00
def __init__ ( self , client : PycordBot ) :
2024-12-16 21:34:37 +02:00
self . client : PycordBot = client
2023-05-06 18:56:15 +03:00
2024-12-16 00:21:41 +02:00
# Disabled because warning functionality is temporarily not needed
2023-05-06 18:56:15 +03:00
# @slash_command(
# name="warning",
# description="Попередити юзера про порушення правил",
# guild_ids=[config_get_sync("guild")],
# )
# @option("user", description="Користувач")
# @option("reason", description="Причина")
# async def warn_cmd(
# self,
# ctx: ApplicationContext,
# user: User,
# reason: str = "Н е вказана",
# ):
# logging.info(f"User {ctx.user.id} warned {user.id} for {reason}")
# await ctx.defer()
# jav_user = HoloUser(user)
# if ctx.user.id in await config_get("admins"):
# logging.info(
# f"Moderator {guild_name(ctx.user)} warned {guild_name(user)} for {reason} (has {jav_user.warnings} warns)"
# )
# if jav_user.warnings >= 5:
# logging.info(
# f"User {guild_name(user)} was banned due to a big amount of warns ({jav_user.warnings})"
# )
# await user.send(
# embed=Embed(
# title="Перманентне блокування",
# description=f"В а с було заблоковано за неодноразове порушення правил сервера.",
# color=Color.fail,
# )
# )
# await user.ban(reason=reason)
# elif jav_user.warnings >= 2:
# logging.info(
# f"User {guild_name(user)} was muted due to a big amount of warns ({jav_user.warnings})"
# )
# jav_user.warn(reason=reason)
# await user.send(
# embed=Embed(
# title="Тимчасове блокування",
# description=f"Причина: `{reason}`\n\nН а вашому рахунку вже {jav_user.warnings} попереджень. В а с було тимчасово заблоковано на **1 годину**.\n\nЯкщо Ви продовжите порушувати правила сервера – згодом В а с заблокують.",
# color=0xDED56B,
# )
# )
# await user.timeout_for(timedelta(hours=1), reason=reason)
# else:
# jav_user.warn()
# await ctx.respond(
# embed=Embed(
# title="Попередження",
# description=f"{user.mention} Будь ласка, не порушуйте правила. Ви отримали попередження з причини `{reason}`.\n\nЯкщо Ви продовжите порушувати правила – це може призвести до блокування в спільноті.",
# color=0xDED56B,
# )
# )
# else:
# logging.warning(
# f"User {guild_name(ctx.user)} tried to use /warn but permission denied"
# )
# await ctx.respond(
# embed=Embed(
# 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:15 +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,
# ),
# )
@slash_command (
name = " clear " ,
description = " Видалити деяку кількість повідомлень в каналі " ,
2024-12-16 00:36:48 +02:00
guild_ids = [ sync_config_get ( " guild " ) ] ,
2023-05-06 18:56:15 +03:00
)
@option ( " amount " , description = " Кількість " )
@option ( " user " , description = " Користувач " , default = None )
async def clear_cmd (
self ,
ctx : ApplicationContext ,
amount : int ,
user : User ,
2024-12-16 21:34:37 +02:00
) - > None :
2024-06-23 13:12:34 +03:00
if ctx . user . id in self . client . owner_ids :
2023-05-06 18:56:15 +03:00
logging . info (
2024-06-23 13:05:03 +03:00
" User %s removed %s message(s) in %s " ,
ctx . user . id ,
amount ,
ctx . channel . id ,
2023-05-06 18:56:15 +03:00
)
2024-12-16 00:21:41 +02:00
2023-05-06 18:56:15 +03:00
await ctx . respond (
embed = Embed ( description = " Видаляю... " ) , ephemeral = True , delete_after = 2.0
)
2024-12-16 00:21:41 +02:00
if user is None :
2023-05-06 18:56:15 +03:00
await ctx . channel . purge ( limit = amount )
else :
await ctx . channel . purge (
limit = amount , check = lambda msg : msg . author == user
)
2024-12-16 00:21:41 +02:00
return
logging . warning (
" User %s tried to use /clear but permission denied " ,
guild_name ( ctx . user ) ,
)
await ctx . respond (
embed = Embed (
title = " Відмовлено в доступі " ,
description = " Здається, це команда лише для модераторів " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-06 18:56:15 +03:00
)
2024-12-16 00:21:41 +02:00
)
2024-12-16 21:34:37 +02:00
mod_role : Union [ Role , None ] = ds_utils . get (
2024-12-16 00:21:41 +02:00
ctx . user . guild . roles , id = await config_get ( " moderators " , " roles " )
)
2024-12-16 21:34:37 +02:00
admin_chan : Union [ TextChannel , None ] = ds_utils . get (
2024-12-16 00:21:41 +02:00
ctx . user . guild . channels ,
id = await config_get ( " adminchat " , " channels " , " text " ) ,
)
2024-12-16 21:34:37 +02:00
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 ,
) ,
)
2023-05-06 18:56:15 +03:00
@slash_command (
name = " reboot " ,
description = " Перезапустити бота " ,
2024-12-16 00:36:48 +02:00
guild_ids = [ sync_config_get ( " guild " ) ] ,
2023-05-06 18:56:15 +03:00
)
2024-12-16 21:34:37 +02:00
async def reboot_cmd ( self , ctx : ApplicationContext ) - > None :
2023-05-06 18:56:15 +03:00
await ctx . defer ( ephemeral = True )
2024-12-16 00:21:41 +02:00
2024-06-23 13:12:34 +03:00
if ctx . user . id in self . client . owner_ids :
2024-06-23 13:05:03 +03:00
logging . info ( " Calling shutdown initiated by %s " , guild_name ( ctx . user ) )
2024-12-16 21:34:37 +02:00
2023-05-06 18:56:15 +03:00
await ctx . respond (
embed = Embed (
title = " Вимикаюсь... " ,
description = " Спробую перезавантажитись за 5 секунд " ,
)
)
2024-12-16 21:34:37 +02:00
2023-05-06 18:56:15 +03:00
scheduler . shutdown ( )
2024-12-16 21:34:37 +02:00
2023-05-06 18:56:15 +03:00
await self . client . close ( )
2024-12-16 21:00:32 +02:00
await waifu_pics . _client_session . close ( )
2024-12-16 21:34:37 +02:00
2024-12-16 00:21:41 +02:00
sys . exit ( )
logging . warning (
" User %s tried to use /reboot but permission denied " ,
guild_name ( ctx . user ) ,
)
await ctx . respond (
embed = Embed (
title = " Відмовлено в доступі " ,
description = " Здається, це команда лише для модераторів " ,
2024-12-16 21:34:37 +02:00
color = Color . FAIL ,
2023-05-06 18:56:15 +03:00
)
2024-12-16 00:21:41 +02:00
)
2024-12-16 21:34:37 +02:00
mod_role : Union [ Role , None ] = ds_utils . get (
2024-12-16 00:21:41 +02:00
ctx . user . guild . roles , id = await config_get ( " moderators " , " roles " )
)
2024-12-16 21:34:37 +02:00
admin_chan : Union [ TextChannel , None ] = ds_utils . get (
2024-12-16 00:21:41 +02:00
ctx . user . guild . channels ,
id = await config_get ( " adminchat " , " channels " , " text " ) ,
)
2024-12-16 21:34:37 +02:00
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 ,
) ,
)
2024-06-23 13:05:03 +03:00
2024-12-16 21:34:37 +02:00
def setup ( client : PycordBot ) - > None :
2024-06-23 13:05:03 +03:00
client . add_cog ( Admin ( client ) )