bot_tg/games/fortune.py
Danil c2e210c4ae Исправление критических багов и потенциальных проблем
- Удалена дублирующая функция handle_uwu_cmd в uwu.py (мёртвый код)

- Исправлен краш UnboundLocalError в /zvetok (next_dt и delta могли быть не определены)

- Баланс penis больше не уходит в минус при проигрыше в казино

- talk_handler теперь использует путь БД из config.py вместо своего (разные файлы БД)

- betting.py больше не крашится при импорте если /db/ недоступен

- Таймаут гадания снижен со 120 до 30 секунд (Telegram таймаутит раньше)
2026-04-13 18:50:37 +03:00

57 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
import aiohttp
import os
logger = logging.getLogger(__name__)
LLAMA_API_URL = os.getenv("LLAMA_API_URL", "https://mirror.porno4free.ru/zovos-ai/")
FORTUNE_PROMPT = (
"Ты — уличный гадатель с района, базаришь на фене. "
"Пользователь хочет погадать на тему. Выдай шуточное гадание, "
"коротко и дерзко, 2-3 предложения максимум. "
"В конце ОБЯЗАТЕЛЬНО напиши на отдельной строке в формате:\n"
"MEM_UP: <текст сверху для мема, максимум 4 слова>\n"
"MEM_DOWN: <текст снизу для мема, максимум 4 слова>\n"
"Тексты для мема должны быть смешными и короткими."
)
async def generate_fortune(topic: str) -> tuple[str, str, str]:
url = f"{LLAMA_API_URL.rstrip('/')}/v1/chat/completions"
payload = {
"messages": [
{"role": "system", "content": FORTUNE_PROMPT},
{"role": "user", "content": f"Погадай мне на: {topic}"},
],
"max_tokens": 200,
"temperature": 0.9,
"top_p": 0.9,
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload, timeout=aiohttp.ClientTimeout(total=30)) as resp:
data = await resp.json()
text = data["choices"][0]["message"]["content"].strip()
up_text = ""
down_text = ""
fortune_lines = []
for line in text.split("\n"):
stripped = line.strip()
if stripped.upper().startswith("MEM_UP:"):
up_text = stripped.split(":", 1)[1].strip()
elif stripped.upper().startswith("MEM_DOWN:"):
down_text = stripped.split(":", 1)[1].strip()
else:
fortune_lines.append(line)
fortune_text = "\n".join(fortune_lines).strip()
if not up_text:
up_text = "Гадание"
if not down_text:
down_text = "по фене"
return fortune_text, up_text, down_text