-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
67 lines (53 loc) · 1.67 KB
/
app.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
"""A simple FastAPI server/Discord client that returns users' roles."""
import asyncio
import json
import discord
import fastapi
server = fastapi.FastAPI()
client: discord.Client = None
with open("config.json") as config_file:
config = json.load(config_file)
TOKEN = config["bot_token"]
GUILD_ID = config["guild_id"]
@server.on_event("startup")
async def startup():
"""Connect the Discord client."""
global client
loop = asyncio.get_running_loop()
client = discord.Client(loop=loop)
loop.create_task(client.start(TOKEN))
@server.get("/users/{user_id}/roles")
async def test_endpoint(user_id: int) -> dict:
"""Get a user's roles."""
guild = client.get_guild(GUILD_ID)
try:
member = await guild.fetch_member(user_id)
except discord.NotFound:
raise fastapi.HTTPException(status_code=404, detail="User not found.")
return {
"roles": [
{"name": role.name, "id": role.id}
for role in member.roles
if not role.is_default()
]
}
@server.get("/bans")
async def ban_endpoint():
"""Get a guilds bans."""
guild = client.get_guild(GUILD_ID)
try:
ban_list = await guild.bans()
except discord.errors.Forbidden:
raise fastapi.HTTPException(status_code=403, detail="Missing Permissions")
return {
"bans": [
{
"user_id": ban_entry.user.id,
"user_name": ban_entry.user.name,
"user_discriminator": ban_entry.user.discriminator,
"user_is_bot": ban_entry.user.bot,
"reason": ban_entry.reason,
}
for ban_entry in ban_list
]
}