Я пишу дашборд для своего дискорд бота. Столкнулся с проблемой, что не могу вывести название сервера при изменении ссылки. При вводе
/dashboard/*id_сервера_1*
выводится название 1-го сервера, но после изменения ссылки на
/dashboard/*id_сервера_2*
название остается как в
/dashboard/*id_сервера_1*
. Что делать?
Вот мой код
import os
from flask import Flask, render_template, redirect, url_for
from flask_discord import DiscordOAuth2Session, requires_authorization, Unauthorized
app = Flask(
__name__,
template_folder='templates',
static_folder='static'
)
app.secret_key = b"gray_cat_&_8uttjff%bhcf&bhfbf*jfncv@xbfb!fdf"
# OAuth2 must make use of HTTPS in production environment.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "true" # !! Only in development environment.
app.config["DISCORD_CLIENT_ID"] = os.getenv("CLIENT_ID") # Discord client ID.
app.config["DISCORD_CLIENT_SECRET"] = os.getenv("CLIENT_SECRET")
# Discord client secret.
app.config["DISCORD_REDIRECT_URI"] = os.getenv("REDIRECT_URI") # URL to your callback endpoint.
app.config["DISCORD_BOT_TOKEN"] = os.getenv("TOKEN") # Required to access BOT resources.
discord = DiscordOAuth2Session(app)
def welcome_user(user):
dm_channel = discord.bot_request("/users/@me/channels", "POST", json={"recipient_id": user.id})
return discord.bot_request(
f"/channels/{dm_channel['id']}/messages", "POST", json={"content": "Thanks for authorizing the app!"}
)
@app.route('/')
def index():
return render_template('index.html')
@app.route("/login/")
def login():
return discord.create_session(scope=["identify", "guilds"])
@app.route("/callback/")
def callback():
discord.callback()
user = discord.fetch_user()
welcome_user(user)
return redirect(url_for("dashboard"))
@app.errorhandler(Unauthorized)
def redirect_unauthorized(e):
return redirect(url_for("login"))
@app.route("/user")
@requires_authorization
def user():
user = discord.fetch_user()
return render_template("user.html", user_name=user.name, user_avatar_url=user.avatar_url, user_discriminator=user.discriminator)
def get_user_servers(guild):
guilds = discord.fetch_guilds()
for guild in guilds:
return list(filter(lambda g: g['administrator'] is True, guild))
@app.route("/dashboard")
def select_server():
guilds = discord.fetch_guilds()
for guild in guilds:
if guild.permissions.administrator:
return render_template("select-server.html", guild_name=guild.name, guild_id=guild.id)
@app.route("/dashboard/<int:guild_id>")
def dashboard(guild_id):
guilds = discord.fetch_guilds()
for guild in guilds:
if guild.permissions.administrator:
return render_template("dashboard.html", guild_name=guild.name, guild_id=guild.id)
else:
return redirect(url_for("dashboard"))
if __name__ == '__main__':
app.run(
host='0.0.0.0',
debug=True,
port=8080
)