165 lines
5.3 KiB
Python
165 lines
5.3 KiB
Python
import logging
|
||
import sys
|
||
from logging import Logger
|
||
|
||
from discord import (
|
||
ApplicationContext,
|
||
Embed,
|
||
User,
|
||
option,
|
||
slash_command,
|
||
Role,
|
||
TextChannel,
|
||
)
|
||
from discord import utils as ds_utils
|
||
from discord.ext import commands
|
||
from libbot.utils import config_get
|
||
|
||
from classes.holo_bot import HoloBot
|
||
from enums import Color
|
||
from modules.scheduler import scheduler
|
||
from modules.utils_sync import guild_name
|
||
from modules.waifu_pics import waifu_pics
|
||
|
||
logger: Logger = logging.getLogger(__name__)
|
||
|
||
|
||
class Admin(commands.Cog):
|
||
"""Cog with utility commands for admins."""
|
||
|
||
def __init__(self, client: HoloBot):
|
||
self.client: HoloBot = client
|
||
|
||
@slash_command(
|
||
name="clear",
|
||
description="Видалити деяку кількість повідомлень в каналі",
|
||
guild_ids=[config_get("guild")],
|
||
)
|
||
@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.
|
||
"""
|
||
if ctx.user.id in self.client.owner_ids:
|
||
logging.info(
|
||
"User %s removed %s message(s) in %s",
|
||
ctx.user.id,
|
||
amount,
|
||
ctx.channel.id,
|
||
)
|
||
|
||
await ctx.respond(
|
||
embed=Embed(description="Видаляю..."), ephemeral=True, delete_after=2.0
|
||
)
|
||
|
||
if user is None:
|
||
await ctx.channel.purge(limit=amount)
|
||
else:
|
||
await ctx.channel.purge(
|
||
limit=amount, check=lambda msg: msg.author == user
|
||
)
|
||
|
||
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,
|
||
)
|
||
)
|
||
|
||
mod_role: Role | None = ds_utils.get(
|
||
ctx.user.guild.roles, id=await config_get("moderators", "roles")
|
||
)
|
||
admin_chan: TextChannel | None = ds_utils.get(
|
||
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,
|
||
),
|
||
)
|
||
|
||
@slash_command(
|
||
name="reboot",
|
||
description="Перезапустити бота",
|
||
guild_ids=[config_get("guild")],
|
||
)
|
||
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.
|
||
"""
|
||
await ctx.defer(ephemeral=True)
|
||
|
||
if ctx.user.id in self.client.owner_ids:
|
||
logging.info("Calling shutdown initiated by %s", guild_name(ctx.user))
|
||
|
||
await ctx.respond(
|
||
embed=Embed(
|
||
title="Вимикаюсь...",
|
||
description="Спробую перезавантажитись за 5 секунд",
|
||
)
|
||
)
|
||
|
||
scheduler.shutdown()
|
||
|
||
await self.client.close()
|
||
await waifu_pics._client_session.close()
|
||
|
||
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,
|
||
)
|
||
)
|
||
|
||
mod_role: Role | None = ds_utils.get(
|
||
ctx.user.guild.roles, id=await config_get("moderators", "roles")
|
||
)
|
||
admin_chan: TextChannel | None = ds_utils.get(
|
||
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,
|
||
),
|
||
)
|
||
|
||
|
||
def setup(client: HoloBot) -> None:
|
||
client.add_cog(Admin(client))
|