-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapp.py
More file actions
71 lines (47 loc) · 1.86 KB
/
app.py
File metadata and controls
71 lines (47 loc) · 1.86 KB
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
#$ load stuff
import json
with open('config.json', 'r') as f:
data = json.load(f)
token = data.get("token")
guild = data.get("guild")
print(guild)
#$ we'll use aiohttp for async.
import aiohttp
import fastapi
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI()
#$ func
async def get_discord_invite_url():
headers = {"Authorization": f"Bot {token}"}
invite_data = {"max_age": 0, "max_uses": 0, "temporary": False}
async with aiohttp.ClientSession() as session:
#$ fetch channels
async with session.get(f"https://discord.com/api/v10/guilds/{guild}/channels", headers=headers) as r:
channels = await r.json()
#print(channels)
text_channels = [ch for ch in channels if int(ch["type"]) == 0]
if not text_channels:
return None
channel_id = text_channels[0]["id"]
#$ create invite
async with session.post(f"https://discord.com/api/v10/channels/{channel_id}/invites",
headers=headers, json=invite_data) as r:
res = await r.json()
return f"https://discord.gg/{res['code']}"
@app.get("/inv")
async def main_page():
url = await get_discord_invite_url()
if url is None:
return {"error": "No channels or failed to create invite"}
return {"invite_url": url}
#$ define template folder
templates = Jinja2Templates(directory='templates')
#$ finally!
@app.get("/", response_class=HTMLResponse)
async def index_page(request : Request):
return templates.TemplateResponse("index.html",{"request": request})
# Made by Rubin B. (rubinexe), also known as $Pygod139.
# This project is open-source; feel free to modify and use it.
# If you reuse or redistribute, please give proper credit.