Я сделал дашборд для дискорд бота, но при выводе моих серверов, на которых у меня есть права администратра выводится только один, а у меня их несколько!
Вот мой скрипт:
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('/about')
def about():
return render_template('about.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("user"))
@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=user)
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")
@app.errorhandler(Unauthorized)
def select_server():
guilds = discord.fetch_guilds()
for guild in guilds:
if guild.permissions.administrator:
return render_template("select-server.html", guild=guild)
@app.route("/dashboard/<int:guild_id>")
@requires_authorization
def dashboard(guild_id):
guilds = discord.fetch_guilds()
for guild in guilds:
if guild.permissions.administrator and guild.id == guild_id:
return render_template("dashboard.html", guild=guild)
@app.route("/dashboard/<int:guild_id>/help")
@requires_authorization
def plugin_help(guild_id):
guilds = discord.fetch_guilds()
for guild in guilds:
if guild.permissions.administrator and guild.id == guild_id:
return render_template("plugin-help.html", guild=guild)
if __name__ == '__main__':
app.run(
host='0.0.0.0',
debug=True,
port=8080
)