This commit is contained in:
16
bot/bot.py
16
bot/bot.py
@@ -14,6 +14,7 @@ __all__ = ("Bot", "discbot")
|
||||
class Bot(commands.Bot):
|
||||
"""
|
||||
Основной класс Discord-бота с методами настройки и запуска.
|
||||
Поддерживает префиксные и slash-команды.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -34,6 +35,7 @@ class Bot(commands.Bot):
|
||||
if help_command is None:
|
||||
help_command = MyHelpCommand()
|
||||
|
||||
# ВАЖНО: tree создаёт сам commands.Bot, мы его не переопределяем
|
||||
super().__init__(
|
||||
command_prefix=command_prefix,
|
||||
intents=intents,
|
||||
@@ -70,6 +72,7 @@ class Bot(commands.Bot):
|
||||
"bot.cogs.moderation",
|
||||
"bot.cogs.blacklist",
|
||||
"bot.cogs.reminders",
|
||||
"bot.cogs.slash",
|
||||
]
|
||||
for cog in cogs:
|
||||
try:
|
||||
@@ -78,6 +81,14 @@ class Bot(commands.Bot):
|
||||
except Exception as e:
|
||||
logger.error(text=f"Ошибка загрузки {cog}: {e!r}", log_type="COGS")
|
||||
|
||||
async def setup_hook(self) -> None:
|
||||
"""
|
||||
Хук discord.py 2.x: вызывается перед подключением к Gateway.
|
||||
Здесь синхронизируем slash-команды.
|
||||
"""
|
||||
await self.tree.sync()
|
||||
logger.info(text="Slash-команды синхронизированы", log_type="SYSTEM")
|
||||
|
||||
async def start_bot(self, token: Optional[str] = None) -> None:
|
||||
"""
|
||||
Запуск бота с использованием сохранённого токена или переданного.
|
||||
@@ -91,9 +102,10 @@ class Bot(commands.Bot):
|
||||
logger.info(text="Запуск бота...", log_type="START")
|
||||
await self.start(use_token)
|
||||
|
||||
async def on_command(self, ctx: commands.Context) -> None:
|
||||
@staticmethod
|
||||
async def on_command(ctx: commands.Context) -> None:
|
||||
"""
|
||||
Глобальное логирование всех вызванных команд.
|
||||
Глобальное логирование всех вызванных префиксных команд.
|
||||
"""
|
||||
logger.info(
|
||||
text=(
|
||||
|
||||
45
bot/cogs/slash.py
Normal file
45
bot/cogs/slash.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
from middleware.loggers import logger
|
||||
|
||||
|
||||
class Slash(commands.Cog):
|
||||
def __init__(self, bot: commands.Bot) -> None:
|
||||
self.bot = bot
|
||||
|
||||
@discord.app_commands.command(
|
||||
name="rules",
|
||||
description="Показать правила сервера",
|
||||
)
|
||||
async def rules_slash(self, interaction: discord.Interaction) -> None:
|
||||
rules_text: str = (
|
||||
"**Правила сервера:**\n"
|
||||
"1. Уважайте других участников.\n"
|
||||
"2. Запрещена реклама и спам.\n"
|
||||
"3. Не используйте запрещённые слова.\n"
|
||||
"4. Соблюдайте тематику каналов.\n"
|
||||
"5. Выполняйте указания модераторов.\n"
|
||||
)
|
||||
await interaction.response.send_message(rules_text, ephemeral=False)
|
||||
logger.info(
|
||||
text=f"Slash /rules вызван пользователем {interaction.user}",
|
||||
log_type="COMMAND",
|
||||
user=str(interaction.user),
|
||||
)
|
||||
|
||||
@discord.app_commands.command(
|
||||
name="ping",
|
||||
description="Проверить отклик бота",
|
||||
)
|
||||
async def ping_slash(self, interaction: discord.Interaction) -> None:
|
||||
await interaction.response.send_message("Pong!", ephemeral=True)
|
||||
logger.info(
|
||||
text=f"Slash /ping вызван пользователем {interaction.user}",
|
||||
log_type="COMMAND",
|
||||
user=str(interaction.user),
|
||||
)
|
||||
|
||||
|
||||
async def setup(bot: commands.Bot) -> None:
|
||||
await bot.add_cog(Slash(bot))
|
||||
@@ -16,7 +16,7 @@ class _Settings(BaseSettings):
|
||||
)
|
||||
|
||||
BOT_TOKEN: Optional[str] = None
|
||||
PREFIX: Optional[str] = '!'
|
||||
PREFIX: Optional[str] = '/'
|
||||
|
||||
WELCOME_CHANNEL_ID: int = 0
|
||||
ADMIN_ROLE_NAME: str = "Администратор"
|
||||
|
||||
Reference in New Issue
Block a user