Music / main.py
darkvibe314's picture
Update main.py
e58210a verified
Raw
History Blame Contribute Delete
8.61 kB
import os
import sys
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
import asyncio
import urllib.parse
# ==========================================
# πŸͺ„ THE HUGGING FACE TRICK
# ==========================================
class DummyHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b"Bot is online and running on Hugging Face!")
def keep_alive():
server = HTTPServer(('0.0.0.0', 7860), DummyHandler)
server.serve_forever()
threading.Thread(target=keep_alive, daemon=True).start()
# ==========================================
# πŸ€– BOT LIBRARIES (MODERN PY-TGCALLS)
# ==========================================
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
# πŸ› MONKEY PATCH
import pyrogram.errors
if not hasattr(pyrogram.errors, 'GroupcallForbidden'):
class GroupcallForbidden(Exception):
pass
pyrogram.errors.GroupcallForbidden = GroupcallForbidden
from pytgcalls import PyTgCalls
from pytgcalls.types import MediaStream, AudioQuality, VideoQuality
import requests
import sqlite3
import random
# ==========================================
# πŸ”‘ YOUR CREDENTIALS
# ==========================================
API_ID = 2040
API_HASH = "b18441a1ff607e10a989891a5462e627"
BOT_TOKEN = "8402411770:AAFvqCzvHwKRX0ScCzbGO3jGsDoD6LJphg4"
app = Client("MusicQuizBot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
call_py = PyTgCalls(app)
# ==========================================
# πŸ’Ύ DATABASE SETUP
# ==========================================
db = sqlite3.connect("bot_database.db", check_same_thread=False)
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS local_quizzes (id INTEGER PRIMARY KEY, question TEXT, correct TEXT, wrong1 TEXT, wrong2 TEXT, wrong3 TEXT)")
db.commit()
# ==========================================
# 🎡 DAVID CYRIL API DOWNLOADERS
# ==========================================
def download_audio_api(query):
# Hit your API to get the audio download URL
api_url = f"https://apis.davidcyril.name.ng/play?query={urllib.parse.quote(query)}"
res = requests.get(api_url).json()
if not res.get("status") or not res.get("result"):
raise Exception("API could not find the song.")
data = res["result"]
title = data.get("title", "Unknown_Audio").replace("/", "_")
download_url = data.get("download_url")
# Download the MP3 to the server so PyTgCalls can stream it smoothly
file_path = f"{title}.mp3"
audio_data = requests.get(download_url).content
with open(file_path, "wb") as f:
f.write(audio_data)
return file_path, title
def download_video_api(query):
# Hit your API to get the video link
search_res = requests.get(f"https://apis.davidcyril.name.ng/play?query={urllib.parse.quote(query)}").json()
if not search_res.get("status"):
raise Exception("API could not find the video.")
yt_url = search_res["result"]["video_url"]
title = search_res["result"]["title"].replace("/", "_")
video_res = requests.get(f"https://apis.davidcyril.name.ng/download/ytmp4?url={urllib.parse.quote(yt_url)}").json()
if not video_res.get("success"):
raise Exception("API failed to generate video link.")
download_url = video_res["result"]["download_url"]
# Download the MP4 to the server
file_path = f"{title}.mp4"
video_data = requests.get(download_url).content
with open(file_path, "wb") as f:
f.write(video_data)
return file_path, title
# ==========================================
# πŸŽ™οΈ VOICE CHAT STREAMING
# ==========================================
@app.on_message(filters.command("play") & filters.group)
async def play_audio(client, message):
if len(message.command) < 2: return await message.reply_text("🩸 `/play <song>`")
query = message.text.split(" ", 1)[1]
status = await message.reply_text("πŸ” Fetching Audio from API...")
try:
# Download in background so bot doesn't freeze
file_path, title = await asyncio.to_thread(download_audio_api, query)
await status.edit_text(f"πŸš€ Joining VC & Playing: **{title}**")
# Stream the file directly into the VC!
await call_py.play(message.chat.id, MediaStream(file_path, audio_parameters=AudioQuality.HIGH))
except Exception as e:
await status.edit_text(f"🩸 **VC Error:** {str(e)}")
@app.on_message(filters.command("vplay") & filters.group)
async def play_video(client, message):
if len(message.command) < 2: return await message.reply_text("🩸 `/vplay <video>`")
query = message.text.split(" ", 1)[1]
status = await message.reply_text("πŸ” Fetching Video from API (This might take a minute)...")
try:
file_path, title = await asyncio.to_thread(download_video_api, query)
await status.edit_text(f"🎬 Joining VC & Streaming Video: **{title}**")
await call_py.play(message.chat.id, MediaStream(file_path, audio_parameters=AudioQuality.HIGH, video_parameters=VideoQuality.HD_720p))
except Exception as e:
await status.edit_text(f"🩸 **VC Error:** {str(e)}")
@app.on_message(filters.command("stop") & filters.group)
async def stop_stream(client, message):
try:
await call_py.leave_call(message.chat.id)
await message.reply_text("πŸ›‘ Left the Voice Chat.")
except:
await message.reply_text("I am not in a Voice Chat right now.")
# ==========================================
# 🧠 INTERACTIVE QUIZ SYSTEM
# ==========================================
async def send_quiz(client, chat_id):
status = await client.send_message(chat_id, "🌍 Generating trivia...")
try:
res = await asyncio.to_thread(requests.get, "https://opentdb.com/api.php?amount=1&type=multiple")
data = res.json()["results"][0]
q = data["question"].replace("&quot;", '"').replace("&#039;", "'").replace("&amp;", "&")
correct = data["correct_answer"].replace("&quot;", '"').replace("&#039;", "'").replace("&amp;", "&")
wrongs = [w.replace("&quot;", '"').replace("&#039;", "'").replace("&amp;", "&") for w in data["incorrect_answers"]]
options = wrongs + [correct]
random.shuffle(options)
buttons = [[InlineKeyboardButton(opt, callback_data="ans_T" if opt == correct else f"ans_F_{correct[:20]}")] for opt in options]
await status.delete()
await client.send_message(chat_id, f"🧠 **Trivia Time!**\n\n{q}", reply_markup=InlineKeyboardMarkup(buttons))
except:
await status.edit_text("🩸 API Error.")
@app.on_message(filters.command("quiz") & filters.group)
async def trigger_quiz(client, message):
await send_quiz(client, message.chat.id)
@app.on_callback_query()
async def callback_handler(client, query: CallbackQuery):
data = query.data
if data == "cmd_music":
await query.answer("Type /play <song> or /vplay <video> in the group!", show_alert=True)
elif data == "cmd_quiz":
await query.answer()
await send_quiz(client, query.message.chat.id)
elif data.startswith("ans_"):
if data == "ans_T":
await query.message.edit_text(f"{query.message.text}\n\nβœ… **{query.from_user.first_name} got it right!**")
else:
await query.answer(f"❌ Wrong! Correct answer: {data.replace('ans_F_', '')}", show_alert=True)
# ==========================================
# 🟒 START COMMAND
# ==========================================
@app.on_message(filters.command(["start", "help"]))
async def start_command(client, message):
buttons = [
[InlineKeyboardButton("🎡 Music Guide", callback_data="cmd_music"),
InlineKeyboardButton("🧠 Play Quiz", callback_data="cmd_quiz")]
]
await message.reply_text(
"πŸ‘‹ Welcome to the **Live VC Music & Quiz Bot**!\n\n"
"**πŸŽ™οΈ VC Streaming:**\n"
"`/play <song>` - Stream Audio in VC\n"
"`/vplay <video>` - Stream Video in VC\n"
"`/stop` - Leave VC\n\n"
"**🧠 Quizzes:**\n"
"`/quiz` - Online Trivia",
reply_markup=InlineKeyboardMarkup(buttons)
)
if __name__ == "__main__":
print("πŸš€ Booting Engine...")
app.start()
print("πŸ“ž Booting Voice Chat Module...")
call_py.start()
print("βœ… ALL SYSTEMS ONLINE! READY FOR VOICE CHATS!")
from pyrogram import idle
idle()