commit 83fc714b8ee1909b83b43e56693545548fdb1ee8 Author: deadzilla Date: Sun May 24 13:35:12 2026 +0500 Initial commit: Discord bot with !msg command diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..fc6629d --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +DISCORD_TOKEN=your_bot_token_here diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e49e1a Binary files /dev/null and b/.gitignore differ diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000..a9dbd0f --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,18 @@ +# AGENTS.md + +## Проект +Discord-бот на Python (discord.py). Команда `!msg <текст>` повторяет текст. + +## Запуск +``` +pip install -r requirements.txt +python bot.py +``` + +Введите `stop` в терминал для остановки. + +## Токен +Токен бота хранится в `.env` (переменная `DISCORD_TOKEN`). Получи токен на [Discord Developer Portal](https://discord.com/developers/applications). Шаблон в `.env.example`. + +## Конвенции +Используй TODO-списки для каждого запроса с несколькими шагами. diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..d1615bb --- /dev/null +++ b/bot.py @@ -0,0 +1,62 @@ +import asyncio +import os +import sys +import threading + +import discord +from discord.ext import commands +from dotenv import load_dotenv + +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}") + + +@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) + break + elif cmd: + print(f"Неизвестная команда: {cmd}") + except EOFError: + stop_event.set() + asyncio.run_coroutine_threadsafe(bot.close(), bot.loop) + 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Остановка бота...") + bot.loop.stop() + stop_event.set() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..34347e3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +discord.py>=2.3.2 +python-dotenv>=1.0.0