File size: 4,147 Bytes
68f3e34
 
b04b92e
 
d10d21b
b04b92e
d10d21b
68f3e34
 
7e1cce5
 
 
 
56b445b
7e1cce5
f5ce5e6
b04b92e
 
68f3e34
 
 
 
 
 
b04b92e
 
35a90d3
b04b92e
 
d10d21b
0f92878
a5cbc50
b04b92e
68f3e34
 
 
 
 
b04b92e
68f3e34
 
 
 
 
 
 
 
b04b92e
68f3e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b04b92e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68f3e34
 
 
 
e20294a
d10d21b
68f3e34
b04b92e
 
68f3e34
 
d10d21b
b04b92e
d10d21b
b04b92e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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"