import asyncio import os import sys import threading import discord from discord.ext import commands from discord.ext.commands import CommandNotFound from discord.ext.commands import CommandNotFound from dotenv import load_dotenv from commands import ALL_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.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Остановка бота...") stop_event.set() asyncio.run_coroutine_threadsafe(bot.close(), bot.loop).result() break elif cmd: print(f"Неизвестная команда: {cmd}") except EOFError: stop_event.set() asyncio.run_coroutine_threadsafe(bot.close(), bot.loop).result() 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)