This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
196 lines (142 loc) · 6.77 KB
/
api.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from aiohttp import web
import asyncio, aiohttp, time, random, string, discord
routes = web.RouteTableDef()
async def generateToken(length=22): return ''.join(random.choice(string.ascii_letters) for i in range(length))
@routes.get('/plugins/webhome')
async def get_root(request): return web.json_response({"success": False, "error_message": "Root access to webhome is prohibited."}, status=200, content_type='application/json')
def fixName(user, name):
fixes = {
"USERNAME" : user.name,
"NICKNAME" : user.display_name
}
if name is None: return user.name
for f in list(fixes.keys()): name = name.replace(f, fixes[f])
return name
def convertUser(bot, person, user):
roles = []
if bot.config.json["plugins"]["webhome"]["role_tags"]["show_roles"]:
for role in user.roles:
if not user.guild.default_role.id == role.id:
if not (bot.config.json["plugins"]["webhome"]["role_tags"]["show_uncoloured_roles"] and int(role.color.value) == 0): roles.append({"name": role.name, "colour": str(role.color)})
roles.reverse() # Reverse for highest first.
return {
"name" : fixName(user, person["name"]),
"id" : person["id"],
"description" : person["description"],
"avatar" : str(user.avatar_url),
"roles" : roles
}
@routes.get('/plugins/webhome/data')
async def get_data(request):
bot = request.app["bot"]
conf = bot.config.json["plugins"]["webhome"]
data = {}; data["success"] = True
if conf["shuffle_people"]: random.shuffle(conf["homepage_people"])
peopleData = []
guild = await bot.config.getServer(bot, bot.config)
for person in conf["homepage_people"]:
user = None
try:
user = discord.utils.get(guild.members, id=int(person["id"]))
except Exception as err:
pass
if user is not None: peopleData.append(convertUser(bot, person, user))
data["default_theme"] = (conf["default_theme"].upper() if conf["default_theme"].upper() in ["DARK", "LIGHT"] else "LIGHT")
data["button"] = conf["button"]
data["meta"] = conf["meta"]
data["people"] = peopleData
# Add configuration
for key in ["community_name", "community_tag", "discord_link"]:
if key == "community_name" and conf[key] is None: data[key] = guild.name # If its none, Use current server name instead.
else: data[key] = conf[key]
data["member_count"] = len(guild.members)
data["custom"] = bot.data["webhome"]["custom"] # Add any custom HTML.
return web.json_response(data, status=200, content_type='application/json')
@routes.get('/plugins/webhome/token/{code}')
async def get_token(request):
# This is used exclusively for the on site login.
# It exchanges the code we get back from oauth for an access_token
# we then simply use the access token to get the ID and log it
# Basically granting the ID access.
bot = request.app["bot"]
code = request.match_info["code"]
conf = bot.config.json["plugins"]["webhome"]
data = {
'client_id': conf["client_id"],
'client_secret': conf["client_secret"],
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': conf["redirect_uri"],
'scope': 'identify email connections'
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
out = {"success": False, "error_message": "Could not get token."}
success = False
async with aiohttp.ClientSession() as session:
async with session.post(conf["endpoint"] + "/oauth2/token", data=data, headers=headers) as resp:
if ((resp.status) == 200):
success = True
out = {}
out = await resp.json()
if success:
async with aiohttp.ClientSession() as session:
async with session.get(conf["endpoint"] + "/users/@me", headers={"Authorization" : ("Bearer " + out["access_token"])}) as resp:
if ((resp.status) == 200):
me = await resp.json()
memberInMainServer = None
for member in (await bot.config.getServer(bot, bot.config)).members:
if str(me["id"]) == str(member.id): memberInMainServer = member
if not memberInMainServer is None:
token = await generateToken()
out = {}
out["token"] = token
out["success"] = True
# Make sure they have permission for login.
if not (await bot.utils["managers"]["permissionManager"].allowed("webhome.login", bot, None, memberInMainServer)):
# Lacking login permission
out = {}
out["success"] = False
out["error_message"] = "You do not have permission to login. Contact server Administrators for more information."
else:
bot.data["webhome"]["valid_users"][str(me["id"])] = token
else:
out = {}
out["error_message"] = "Couldn't find user in server."
out["success"] = False
else:
out = {}
out["error_message"] = "Can not get ID from Token."
out["success"] = False
return web.json_response(out, status=200, content_type='application/json')
@routes.get('/plugins/webhome/info/{token}')
async def get_info(request):
bot = request.app["bot"]
conf = bot.config.json["plugins"]["webhome"]
token = request.match_info["token"]
page = request.rel_url.query.get("page", None)
validTokens = {}
for key in list(bot.data["webhome"]["valid_users"].keys()): validTokens[str(bot.data["webhome"]["valid_users"][str(key)])] = key
if not token in list(validTokens.keys()):
# Token is invalid
return web.json_response({"success": False, "error_message": "Invalid Token"}, status=200, content_type='application/json')
guild = await bot.config.getServer(bot, bot.config)
user_id = validTokens[token]
user = None
for member in guild.members:
if str(user_id) == str(member.id): user = member
if user is None:
# User not in Discord Server
return web.json_response({"success": False, "error_message": "You are not in the Discord Server"}, status=200, content_type='application/json')
return web.json_response(
{
"success" : True,
"id" : user.id,
"name" : user.name,
"avatar_url" : str(user.avatar_url),
"display_name" : user.display_name
},
status=200,
content_type='application/json'
)