Spaces:
Running
Running
import requests | |
import gradio as gr | |
import aiohttp | |
import os | |
import pre | |
import asyncio | |
def id_check(request: gr.Request): | |
param = str(request.query_params) | |
if 'from' in param: | |
code = param.split('from=')[1] | |
if code == 'haus': | |
gr.Info("""Now can use 'https://objektify.xyz'""") | |
if not "code=" in param: | |
return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown( | |
info='Login to create preset') | |
code = param.split('code=') | |
print(code[1]) | |
user_id, username, avatar_url = get_discord_user_profile(str(code[1])) | |
if user_id == None: | |
return "Login to Discord", gr.Tab(visible=False), gr.Tab(visible=False), '', gr.Tab(visible=False), gr.Dropdown( | |
info='Login to create preset') | |
if user_id == "412827396027187211": | |
return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=True), gr.Tab( | |
visible=True), user_id, gr.Tab(visible=True), gr.Dropdown(info='') | |
print(user_id) | |
return gr.Button(value=username, icon=avatar_url, link=redirect_uri), gr.Tab(visible=False), gr.Tab(visible=False), user_id, gr.Tab(visible=True), gr.Dropdown(info='') | |
def get_discord_user_profile(code): | |
# Discordμ API μλν¬μΈνΈ | |
token_url = "https://discord.com/api/oauth2/token" | |
user_url = "https://discord.com/api/users/@me" | |
guild_join_url = f"https://discord.com/api/guilds/1207625911709597737/members/{{user_id}}" | |
try: | |
# ν ν° μμ²μ μν λ°μ΄ν° | |
data = { | |
'client_id': client_id, # client_id μ€μ | |
'client_secret': client_secret, # client_secret μ€μ | |
'grant_type': 'authorization_code', | |
'code': code, | |
'redirect_uri': redirect_uri, # redirect_uri μ€μ | |
} | |
headers = { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
# μ‘μΈμ€ ν ν° μμ² | |
token_response = requests.post(token_url, data=data, headers=headers) | |
token_response.raise_for_status() # μ€λ₯ λ°μμ μμΈ λ°μ | |
access_token = token_response.json().get('access_token') | |
# μ¬μ©μ μ 보 μμ² | |
auth_headers = { | |
'Authorization': f'Bearer {access_token}' | |
} | |
user_response = requests.get(user_url, headers=auth_headers) | |
user_response.raise_for_status() # μ€λ₯ λ°μμ μμΈ λ°μ | |
user_info = user_response.json() | |
user_id = user_info.get('id') | |
username = user_info.get('username') + '#' + user_info.get('discriminator') | |
avatar_hash = user_info.get('avatar') | |
avatar_url = f"https://cdn.discordapp.com/avatars/{user_id}/{avatar_hash}.png" if avatar_hash else None | |
# μ¬μ©μ μλ² μ΄λ | |
async def invite_user_to_guild(): | |
async with aiohttp.ClientSession() as session: | |
async with session.put( | |
guild_join_url.format(user_id=user_id), | |
json={'access_token': access_token}, | |
headers={"Authorization": f"Bot {bot_token}", "Content-Type": "application/json"} | |
) as response: | |
if response.status == 201: | |
print("User invited to the guild successfully.") | |
else: | |
response_text = await response.text() | |
print(f"Response text: {response_text}") | |
print("Failed to invite user to the guild.") | |
asyncio.run(invite_user_to_guild()) | |
return user_id, username, avatar_url | |
except requests.exceptions.RequestException as e: | |
print("Error to login") | |
print(str(e)) # μ€λ₯ λ΄μ© μΆλ ₯μ μνλ€λ©΄ μ£Όμ ν΄μ | |
return None, None, None | |
# νκ²½ λ³μ μ€μ | |
client_id = "1239473790732472370" | |
client_secret = "C7RLdB4YArSM9jVk8tsznQnKOzwGKwsO" | |
redirect_uri = pre.login_uri | |
bot_token = 'MTIzOTQ3Mzc5MDczMjQ3MjM3MA.GwNiLO.8n_GGfTdP0IWAFfh4ENzUTxI37Ix6yG0jjclYI' | |
# μ¬μ© μ: | |
# code = "THE_CODE_FROM_REDIRECT_URI" | |