Initial commit: Discord bot with !msg command
This commit is contained in:
commit
83fc714b8e
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
||||
DISCORD_TOKEN=your_bot_token_here
|
||||
BIN
.gitignore
vendored
Normal file
BIN
.gitignore
vendored
Normal file
Binary file not shown.
18
AGENTS.md
Normal file
18
AGENTS.md
Normal file
@ -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-списки для каждого запроса с несколькими шагами.
|
||||
62
bot.py
Normal file
62
bot.py
Normal file
@ -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()
|
||||
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
discord.py>=2.3.2
|
||||
python-dotenv>=1.0.0
|
||||
Loading…
x
Reference in New Issue
Block a user