i was working on a discord bot that let you interact from outside discord. i used a simple flask application as a test, so i wanna make a bot that uses a list array of users objects so the user can sign in from the browser flask application with his discord account so the flask application identify him with his discord id and so on shows him the collected information from the discord bot.
so i am using this code.
import discord
import os
from discord.ext import commands, tasks
from flask import Flask, redirect, url_for
from flask_discord import DiscordOAuth2Session, requires_authorization, Unauthorized
class p:
def __init__(self, name, id):
self.name = name
self.id = id
self.age = 17
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(intents=intents, command_prefix="-", case_insensitive=True)
@bot.event
async def on_ready():
globals()["server"] = bot.guilds[0]
globals()["users"] = [] # the array of users that the bot adn the flask app should use.
for member in server.members: # looping trough users adding each users to the users array
globals()["U_" + str(member.id)] = p(member.name, member.id)
users.append(globals()["U_" + str(member.id)])
print("Bot is Online !")
@bot.command()
async def get_data(ctx):
u = ctx.author.id
usr = globals()["U_" + str(u)]
await ctx.send(f"age = {usr.age}")
@bot.command()
async def edit_data(ctx, arg1):
u = ctx.author.id
usr = globals()["U_" + str(u)]
usr.age = int(arg1)
await ctx.send(f"age = {usr.age}")
app = Flask(__name__) # the flask app
app.secret_key = b"secret key"
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "true"
app.config["DISCORD_CLIENT_ID"] = 0
app.config["DISCORD_CLIENT_SECRET"] = ""
app.config["DISCORD_REDIRECT_URI"] = "http://127.0.0.1/callback"
app.config["DISCORD_BOT_TOKEN"] = ""
disc = DiscordOAuth2Session(app)
@app.route("/login/")
def login():
return disc.create_session()
@app.errorhandler(Unauthorized)
def redirect_unauthorized(e):
return redirect(url_for("login"))
@app.route("/callback/")
def callback():
disc.callback()
return redirect(url_for(".me"))
@app.route("/me/")
@requires_authorization
def me():
user = disc.fetch_user()
return f"""
<html>
<head>
<title>{user.name}</title>
</head>
<body>
<img src='{user.avatar_url}' />
<p>user age = {globals()[f"U_{user.id}"]}
</body>
</html>"""
app.run(port=80, host="127.0.0.1") # to run the flask app
bot.run("") # to run the discord bot
so i want the flask app and the discord bot to be running at the same time and to use the Users
list.
any help ?