import asyncio import os import sys import threading import discord from discord.ext import commands from discord.ext.commands import CommandNotFound from dotenv import load_dotenv from commands import ALL_COMMANDS from console_commands import ALL_CONSOLE_COMMANDS load_dotenv() intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) stop_event = threading.Event() @bot.event async def on_ready(): print(f"Бот вошёл как {bot.user}") for cog_class in ALL_COMMANDS: cog = cog_class() await bot.add_cog(cog) for cog in bot.cogs: print(f" Загружен: {cog}") @bot.event async def on_command_error(ctx, error): if isinstance(error, CommandNotFound): return print(f"Ошибка команды: {error}") @bot.command(name="msg") async def msg(ctx, *, text: str): """Повторяет текст после !msg""" await ctx.send(text) def console_input(): while not stop_event.is_set(): try: cmd = input().strip().lower() if cmd == "stop": print("\nОстановка бота...") if "stop" in ALL_CONSOLE_COMMANDS: ALL_CONSOLE_COMMANDS["stop"](stop_event, bot) break elif cmd: if cmd in ALL_CONSOLE_COMMANDS: ALL_CONSOLE_COMMANDS[cmd](stop_event, bot) else: print(f"Неизвестная команда: {cmd}") except (EOFError, KeyboardInterrupt): stop_event.set() try: asyncio.run_coroutine_threadsafe(bot.close(), bot.loop).result(timeout=5) except Exception as e: print(f"Ошибка при остановке бота: {e}") break if __name__ == "__main__": print("Введите 'stop' для остановки бота") thread = threading.Thread(target=console_input, daemon=True) thread.start() try: token = os.getenv("DISCORD_TOKEN") if not token: print("Ошибка: токен не найден в .env") sys.exit(1) bot.run(token) except KeyboardInterrupt: print("\nОстановка бота...") stop_event.set() asyncio.run_coroutine_threadsafe(bot.close(), bot.loop).result() sys.exit(0)