Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import discord
|
| 2 |
+
from google import genai
|
| 3 |
+
import os
|
| 4 |
+
import asyncio
|
| 5 |
+
import json
|
| 6 |
+
import threading
|
| 7 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
| 8 |
+
|
| 9 |
+
# --- CẤU HÌNH ---
|
| 10 |
+
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
| 11 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
|
| 13 |
+
client_ai = genai.Client(api_key=GEMINI_API_KEY)
|
| 14 |
+
|
| 15 |
+
MODERATION_PROMPT = """
|
| 16 |
+
Bạn là kiểm duyệt viên Discord. Phân tích tin nhắn tiếng Việt (kể cả teencode, viết lách luật).
|
| 17 |
+
Nếu vi phạm (Toxic, Sex, Scam, Hate Speech), trả về JSON:
|
| 18 |
+
{"violation": true, "reason": "Lý do ngắn gọn", "confidence": 0.9}
|
| 19 |
+
Nếu không vi phạm: {"violation": false, "reason": "", "confidence": 1.0}
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# --- LOGIC AI ---
|
| 23 |
+
async def analyze_message(content):
|
| 24 |
+
try:
|
| 25 |
+
# Ưu tiên Gemini 3 Flash cho tốc độ
|
| 26 |
+
response = client_ai.models.generate_content(
|
| 27 |
+
model="gemini-3-flash",
|
| 28 |
+
contents=content,
|
| 29 |
+
config={'system_instruction': MODERATION_PROMPT, 'response_mime_type': 'application/json'}
|
| 30 |
+
)
|
| 31 |
+
result = json.loads(response.text)
|
| 32 |
+
|
| 33 |
+
# Nếu chưa chắc chắn, dùng Gemma 3 27B để thẩm định sâu
|
| 34 |
+
if not result['violation'] and result.get('confidence', 1.0) < 0.8:
|
| 35 |
+
gemma_res = client_ai.models.generate_content(
|
| 36 |
+
model="gemma-3-27b",
|
| 37 |
+
contents=f"Kiểm tra kỹ câu này xem có chửi thề ẩn ý hoặc lách luật không: {content}",
|
| 38 |
+
config={'response_mime_type': 'application/json'}
|
| 39 |
+
)
|
| 40 |
+
result = json.loads(gemma_res.text)
|
| 41 |
+
return result
|
| 42 |
+
except:
|
| 43 |
+
return {"violation": False, "reason": "", "confidence": 0}
|
| 44 |
+
|
| 45 |
+
# --- CẤU HÌNH DISCORD BOT ---
|
| 46 |
+
intents = discord.Intents.default()
|
| 47 |
+
intents.message_content = True
|
| 48 |
+
bot = discord.Client(intents=intents)
|
| 49 |
+
|
| 50 |
+
async def get_webhook(channel):
|
| 51 |
+
# Tìm webhook có sẵn do bot tạo hoặc tạo mới
|
| 52 |
+
webhooks = await channel.webhooks()
|
| 53 |
+
for wh in webhooks:
|
| 54 |
+
if wh.name == "GeminiGuard":
|
| 55 |
+
return wh
|
| 56 |
+
return await channel.create_webhook(name="GeminiGuard")
|
| 57 |
+
|
| 58 |
+
@bot.event
|
| 59 |
+
async def on_ready():
|
| 60 |
+
print(f'✅ Bot Webhook đã sẵn sàng: {bot.user}')
|
| 61 |
+
|
| 62 |
+
@bot.event
|
| 63 |
+
async def on_message(message):
|
| 64 |
+
if message.author == bot.user or not message.content:
|
| 65 |
+
return
|
| 66 |
+
|
| 67 |
+
analysis = await analyze_message(message.content)
|
| 68 |
+
|
| 69 |
+
if analysis['violation']:
|
| 70 |
+
try:
|
| 71 |
+
# 1. Lấy thông tin người dùng
|
| 72 |
+
username = message.author.display_name
|
| 73 |
+
avatar_url = message.author.display_avatar.url
|
| 74 |
+
|
| 75 |
+
# 2. Xóa tin nhắn gốc
|
| 76 |
+
await message.delete()
|
| 77 |
+
|
| 78 |
+
# 3. Gửi tin nhắn thay thế qua Webhook
|
| 79 |
+
webhook = await get_webhook(message.channel)
|
| 80 |
+
await webhook.send(
|
| 81 |
+
content=f"#### *(Tin nhắn đã bị ẩn do: {analysis['reason']})*",
|
| 82 |
+
username=username,
|
| 83 |
+
avatar_url=avatar_url
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
print(f"Đã xử lý tin nhắn của {username} qua Webhook.")
|
| 87 |
+
|
| 88 |
+
except discord.Forbidden:
|
| 89 |
+
print("Thiếu quyền 'Manage Messages' hoặc 'Manage Webhooks'.")
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Lỗi: {e}")
|
| 92 |
+
|
| 93 |
+
# --- KEEP ALIVE SERVER ---
|
| 94 |
+
class HealthCheckHandler(BaseHTTPRequestHandler):
|
| 95 |
+
def do_GET(self):
|
| 96 |
+
self.send_response(200); self.end_headers()
|
| 97 |
+
self.wfile.write(b"Bot is online!")
|
| 98 |
+
|
| 99 |
+
def run_web_server():
|
| 100 |
+
HTTPServer(('0.0.0.0', 7860), HealthCheckHandler).serve_forever()
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
threading.Thread(target=run_web_server, daemon=True).start()
|
| 104 |
+
bot.run(DISCORD_TOKEN)
|