Untitled

 avatar
unknown
python
2 years ago
7.2 kB
10
Indexable
from BotAmino import *
import requests
from os import path
import re, urllib.parse, urllib.request, sys
import agorartc
import subprocess
import json 
from time import sleep
import socket
from functools import wraps
from contextlib import closing

print("wait...")

from pymongo import MongoClient
vct={}
client.prefix = "."
client.admin_user="25663bd3-3688-4f73-a2ab-8548b0b0cd29"
block=["0d728809-2c54-4d21-9539-0f3ed268affc","0f9ee782-8454-4c9d-9eaf-f4946a236a1d"]
save={}


client.wait=4 # set the prefix to /
def find_free_port():
    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
        s.bind(('', 0))
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        return s.getsockname()[1]

def chatii(data):
    return data.chatId !="226725f7-db44-4b22-9447-50abd47ef4df"

def pre(data):
    return data.authorId not in block
live_vc = False

from yt_dlp import YoutubeDL
import os 
def telecharger(url,music_name):
    music = None
    
    ydl_opts = {
            'format': 'bestaudio/best',
            'postprocessors': [{
                'key': 'FFmpegExtractAudio',
                'preferredcodec': 'mp3',
                'preferredquality': '192',
                }],
            'extract-audio': True,
            'outtmpl': f"audio/{music_name}.mp3",
            }

    with YoutubeDL(ydl_opts) as ydl:
        video_length = ydl.extract_info(url, download=True).get('duration')
        ydl.cache.remove()

        
    return music_name




def send_joinn(token):
    t=token['threadId']
    #print(t)
    port=find_free_port()
    save[t]=port
    print(save)
    subprocess.Popen(f'start /wait python server.py {port}', shell=True)
    dat={"data":json.dumps(token)}
    sleep(3)
    requests.post(f"http://127.0.0.1:{port}/join",data=dat)
    return "yo"

def search_internet_music(music_name):
    query_string = urllib.parse.urlencode({"search_query": music_name})
    formatUrl = urllib.request.urlopen("https://www.youtube.com/results?" + query_string)

    search_results = re.findall(r"watch\?v=(\S{11})", formatUrl.read().decode())
    clip2 = "https://www.youtube.com/watch?v=" + "{}".format(search_results[0])
    return clip2

def send_type(types,chatId):
    port=save[chatId]
    
    dat={"type":types}
    requests.post(f"http://127.0.0.1:{port}/type",data=dat)
    return "ii"

def bot_chat(prompt):
    response = openai.Completion.create(
    model="text-davinci-002",
    prompt=f"{prompt}.",
    temperature=0.9,
    max_tokens=150,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
    stop=[" Human:", " AI:"])
    return response.choices[0].text


@client.command("ai",condition=pre)
def ai(data):
    if len(data.message)<11:
        data.subClient.send_message(data.chatId,message="type more than 10 words",replyTo=data.messageId)
    else:
        text=bot_chat(data.message)
        #text2=text.replace("\n","")
        data.subClient.send_message(data.chatId,message=text,replyTo=data.messageId)


@client.command("vc", condition=chatii)
def vcc(data):
    if data.authorId not in block:
        if data.message=="":
            chatid=data.chatId
        else:
            chatid=client.get_from_code(data.message).objectId
        #vct[chatid]=0
        client.start_vc(data.comId,chatid)
        sleep(10)
        client.send(json.dumps({"o":{"ndcId":data.comId,"threadId":chatid,"id":"337496"},"t":200}))
        sleep(2)
    #while True:
        #if vct[chatid] ==0:
            #liv = json.dumps({"o":{"ndcId":data.comId,"threadId":chatid,"joinRole":1,"id":"2249844"},"t":112})
            #client.send(liv)
            #print("loop")
            #sleep(20)
        #else: break
@client.command("end_vc",condition=pre)
def vlcc(data):
    if data.message=="":
        chatid=data.chatId
    else:
        chatid=client.get_from_code(data.message).objectId
    
    
    #vct[chatid]=1
    try: client.end_vc(data.comId,chatid)
    
    except Exception as e:
        print(e)
    try: send_type("leave",chatid)
    except: pass
    
def send_play(url,chatId):
    
    port=save[chatId]
    
    dat={"url":url}
    res=requests.post(f"http://127.0.0.1:{port}/play",data=dat).text
    print(res)
    return res

@client.command("pause",condition=pre)
def typte(data):
    if data.message=="":
        chatid=data.chatId
    else:
        chatid=client.get_from_code(data.message).objectId
    send_type("pause",chatid)
    
@client.command("resume",condition=pre)
def typtte(data):
    if data.message=="":
        chatid=data.chatId
    else:
        chatid=client.get_from_code(data.message).objectId
    send_type("resume",chatid)



@client.command("mute",condition=pre)
def typtte(data):
    if data.message=="":
        chatid=data.chatId
    else:
        chatid=client.get_from_code(data.message).objectId
    send_type("mute",chatid)

@client.command("unmute",condition=pre)
def typtte(data):
    if data.message=="":
        chatid=data.chatId
    else:
        chatid=client.get_from_code(data.message).objectId
    send_type("unmute",chatid)
    
@client.command("volume",condition=pre)
def vol(data):
    port=save[data.chatId]
    dat={"volume":data.message}
    requests.post(f"http://127.0.0.1:{port}/volume",data=dat)


@client.command("play_alt",condition=pre)
def pp(data):
    chatid="226725f7-db44-4b22-9447-50abd47ef4df"
    #dat={"url":search_internet_music(data.message),"threadId":chatid}
    send_play(search_internet_music(data.message),chatid)
    
@client.command("play",condition=pre)
def play(data):
    if data.message.startswith("https://") is True:
        url=data.message
    else:
        url=search_internet_music(data.message)
    res=send_play(url,data.chatId)
    if res!="done":
        data.subClient.send_message(data.chatId,message=res)

       
    
@client.command("restart",condition=pre)
def pkp(data):
    
    os.execv(sys.executable, ["None", os.path.basename(sys.argv[0])])
@client.event("on_fetch_channel")
def on_chatvie(data):
    print("get")
    d=json.dumps(data.json)
    #print(d)
    send_joinn(data.json)                                        

@client.event("on_voice_chat_end")
def on_chat_invi(data):
    try:
        commuId = int(data.json["ndcId"])
        subClient = client.get_community(commuId)
    except Exception:
        return

    args = Parameters(data, subClient)
    
    try: send_type("leave",args.chatId)
    except: pass



import os
from time import sleep
import threading
import sys
def maintenance():
    print("launch maintenance")
    i = 0
    while i < 7200:
        i += 10
        sleep(10)
    os.execv(sys.executable, ["None", os.path.basename(sys.argv[0])])
client.launch(False)
threading.Thread(target=maintenance).start()
def reconsocketloop():
	while True:
		client.close()
		client.run_amino_socket()
		sleep(120)


socketloop = threading.Thread(target=reconsocketloop, daemon=True)
socketloop.start()

print("Ready")
Editor is loading...