Discord/cogs/admin.py

164 lines
5.2 KiB
Python
Raw Normal View History

2023-05-06 18:56:15 +03:00
import logging
2024-12-16 00:21:41 +02:00
import sys
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
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
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-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,
) -> None:
"""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="Здається, це команда лише для модераторів",
color=Color.FAIL,
2023-05-06 18:56:15 +03:00
)
2024-12-16 00:21:41 +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")
)
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"),
)
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
)
async def reboot_cmd(self, ctx: ApplicationContext) -> None:
"""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))
2023-05-06 18:56:15 +03:00
await ctx.respond(
embed=Embed(
title="Вимикаюсь...",
description="Спробую перезавантажитись за 5 секунд",
)
)
2023-05-06 18:56:15 +03:00
scheduler.shutdown()
2023-05-06 18:56:15 +03:00
await self.client.close()
await waifu_pics._client_session.close()
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="Здається, це команда лише для модераторів",
color=Color.FAIL,
2023-05-06 18:56:15 +03:00
)
2024-12-16 00:21:41 +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")
)
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"),
)
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))