Spaces:
Sleeping
Sleeping
| import os | |
| import threading | |
| import requests | |
| from flask import Flask, request, jsonify | |
| import discord | |
| from discord.ext import commands | |
| # --- ЗАГРУЗКА СЕКРЕТОВ (БЕЗОПАСНО) --- | |
| # Если секрет не найден, вернет None, поэтому важно всё заполнить в настройках HF | |
| DISCORD_BOT_TOKEN = os.environ.get("DISCORD_BOT_TOKEN") | |
| DISCORD_WEBHOOK_URL = os.environ.get("DISCORD_WEBHOOK_URL") | |
| # Превращаем ID в числа (int), так как переменные окружения всегда строки | |
| try: | |
| ADMIN_CHANNEL_ID = int(os.environ.get("ADMIN_CHANNEL_ID")) | |
| ADMIN_USER_ID = int(os.environ.get("ADMIN_USER_ID")) | |
| except (TypeError, ValueError): | |
| print("❌ ОШИБКА: Не заданы ID в секретах Hugging Face!") | |
| ADMIN_CHANNEL_ID = 0 | |
| ADMIN_USER_ID = 0 | |
| app = Flask(__name__) | |
| # Хранилище | |
| current_display_message = "Связь с Админом установлена..." | |
| pending_commands = [] | |
| # --- БОТ --- | |
| intents = discord.Intents.default() | |
| intents.message_content = True | |
| bot = commands.Bot(command_prefix="!", intents=intents) | |
| async def on_ready(): | |
| print(f'✅ Бот {bot.user} запущен! Слушаю канал {ADMIN_CHANNEL_ID}') | |
| async def on_message(message): | |
| global current_display_message | |
| if message.author == bot.user: return | |
| # Сравниваем ID | |
| if message.channel.id != ADMIN_CHANNEL_ID: return | |
| if message.author.id != ADMIN_USER_ID: return | |
| text = message.content | |
| if text.startswith("/"): | |
| print(f"⚙️ Команда: {text}") | |
| pending_commands.append(text) | |
| await message.add_reaction("⚙️") | |
| else: | |
| print(f"📺 Чат: {text}") | |
| current_display_message = text | |
| await message.add_reaction("✅") | |
| # --- FLASK --- | |
| def send_to_admin(): | |
| data = request.json | |
| # Используем секретный URL вебхука | |
| if DISCORD_WEBHOOK_URL: | |
| requests.post(DISCORD_WEBHOOK_URL, json={ | |
| "username": "Игрок с Клавиатуры", | |
| "content": f"📨 **{data.get('user')}**: {data.get('text')}" | |
| }) | |
| return "Sent", 200 | |
| else: | |
| return "Webhook not configured", 500 | |
| def sync_roblox(): | |
| global pending_commands | |
| response = { | |
| "screen_text": current_display_message, | |
| "commands": pending_commands[:] | |
| } | |
| pending_commands = [] | |
| return jsonify(response) | |
| # --- ЗАПУСК --- | |
| def run_discord(): | |
| if DISCORD_BOT_TOKEN: | |
| bot.run(DISCORD_BOT_TOKEN) | |
| else: | |
| print("❌ ОШИБКА: Нет токена бота!") | |
| if __name__ == '__main__': | |
| threading.Thread(target=run_discord).start() | |
| app.run(host='0.0.0.0', port=7860) |