2023-05-06 18:56:15 +03:00
|
|
|
|
import logging
|
2024-12-16 00:21:41 +02:00
|
|
|
|
import sys
|
2024-12-27 23:23:41 +02:00
|
|
|
|
from logging import Logger
|
2024-12-16 21:34:37 +02:00
|
|
|
|
|
|
|
|
|
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-26 20:12:50 +02:00
|
|
|
|
from libbot.utils import config_get
|
2023-05-06 18:56:15 +03:00
|
|
|
|
|
2024-12-17 23:14:06 +02:00
|
|
|
|
from classes.holo_bot import HoloBot
|
2024-12-16 21:34:37 +02:00
|
|
|
|
from enums import Color
|
2024-12-16 21:59:10 +02:00
|
|
|
|
from modules.scheduler 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
|
|
|
|
|
2024-12-27 23:23:41 +02:00
|
|
|
|
logger: Logger = logging.getLogger(__name__)
|
2023-05-06 18:56:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Admin(commands.Cog):
|
2024-12-16 00:21:41 +02:00
|
|
|
|
"""Cog with utility commands for admins."""
|
|
|
|
|
|
2024-12-17 23:14:06 +02:00
|
|
|
|
def __init__(self, client: HoloBot):
|
|
|
|
|
self.client: HoloBot = client
|
2023-05-06 18:56:15 +03:00
|
|
|
|
|
|
|
|
|
@slash_command(
|
|
|
|
|
name="clear",
|
|
|
|
|
description="Видалити деяку кількість повідомлень в каналі",
|
2024-12-26 20:12:50 +02:00
|
|
|
|
guild_ids=[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-12-27 21:30:32 +02:00
|
|
|
|
"""Command /clear <amount> [<user>]
|
|
|
|
|
|
|
|
|
|
Removes last <amount> messages in the current channel. Optionally from a specific user.
|
|
|
|
|
"""
|
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-27 01:18:54 +02:00
|
|
|
|
mod_role: 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-27 01:18:54 +02:00
|
|
|
|
admin_chan: 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-26 20:12:50 +02:00
|
|
|
|
guild_ids=[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:
|
2024-12-27 21:30:32 +02:00
|
|
|
|
"""Command /reboot
|
|
|
|
|
|
|
|
|
|
Stops the bot. Is called "reboot" because it's assumed that the bot has automatic restart.
|
|
|
|
|
"""
|
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-27 01:18:54 +02:00
|
|
|
|
mod_role: 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-27 01:18:54 +02:00
|
|
|
|
admin_chan: 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-17 23:14:06 +02:00
|
|
|
|
def setup(client: HoloBot) -> None:
|
2024-06-23 13:05:03 +03:00
|
|
|
|
client.add_cog(Admin(client))
|