-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (50 loc) · 1.73 KB
/
main.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
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI, Response
from dotenv import load_dotenv
import os
from bot_base.nextcord_bot import NextcordBot
from jipp.utils.logging_utils import log
# Set up logging
# Load environment variables
load_dotenv()
# Get Discord token from environment variables
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
REDIRECT_URI = os.getenv("REDIRECT_URI")
# Initialize the NextcordBot
bot = NextcordBot()
@asynccontextmanager
async def lifespan(app: FastAPI):
log.info("Application is starting up...")
# Start the Discord bot
asyncio.create_task(bot.start(DISCORD_TOKEN))
yield
log.info("Application is shutting down...")
# Close the Discord bot connection
await bot.close()
# Initialize FastAPI app
app = FastAPI(lifespan=lifespan)
@app.get("/")
async def read_root():
return {
"status": "Application is running",
"discord_bot": (
f"Connected as {bot.user}" if bot.is_ready() else "Not connected"
),
}
def generate_oauth2_url():
scopes = "identify%20guilds%20bot" # Customize the scopes as needed
permissions = 8 # Set permissions for the bot (admin in this case)
return (
f"https://discord.com/api/oauth2/authorize?"
f"client_id={DISCORD_CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code&scope={scopes}&permissions={permissions}"
)
@app.get("/oauth2")
async def oauth2_redirect():
oauth2_url = generate_oauth2_url()
return Response(status_code=302, headers={"Location": oauth2_url})
if __name__ == "__main__":
import uvicorn
log.info("Starting the application...")
uvicorn.run(app, host="0.0.0.0", port=8000)