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"