async def discordQuotes(cls:"WebIndex", WebRequest:Request) -> Response: """ Default url: /discord/quotes/{guild_id:\d+} """ PhaazeDiscord:"PhaazebotDiscord" = cls.Web.BASE.Discord if not PhaazeDiscord: return await notAllowed(cls, WebRequest, msg="Discord module is not active") guild_id:str = WebRequest.match_info.get("guild_id", "") Guild:discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id if guild_id.isdigit() else 0)) if not Guild: return await discordInvite(cls, WebRequest, msg=f"Phaaze is not on this Server", guild_id=guild_id) DiscordCommand:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Discord/quotes.html") DiscordCommand.replace( guild_name = html.escape(Guild.name), guild_id = str(Guild.id), ) site:str = cls.HTMLRoot.replace( replace_empty = True, title = f"Phaaze | Discord - Quotes: {Guild.name}", header = getNavbar(active="discord"), main = DiscordCommand ) return cls.response( body=site, status=200, content_type='text/html' )
async def discordLogin(cls: "WebIndex", WebRequest: Request, msg: str = "") -> Response: """ Default url: /discord/login """ PhaazeDiscord: "PhaazebotDiscord" = cls.Web.BASE.Discord if not PhaazeDiscord: return await notAllowed(cls, WebRequest, msg="Discord module is not active") DiscordUser: DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if DiscordUser.found: return await cls.discordMain(WebRequest) query_error: str = WebRequest.query.get("error", None) if query_error == "missing": msg = "Missing code from Discord" elif query_error == "discord": msg = "Error while getting informations from Discord" elif query_error == "database": msg = "Error while inserting data into database" DiscordLogin: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Discord/login.html") DiscordLogin.replace(replace_empty=True, msg=msg, login_link=cls.Web.BASE.Vars.DISCORD_LOGIN_LINK) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Discord - Login", header=getNavbar(active="discord"), main=DiscordLogin) return cls.response(body=site, status=200, content_type='text/html')
async def twitchInvite(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, msg: str = "", channel_id: str = "") -> Response: """ Default url: /twitch/invite """ PhaazeTwitch: "PhaazebotTwitch" = cls.BASE.Twitch if not PhaazeTwitch: return await cls.Tree.errors.notAllowed( cls, WebRequest, msg="Twitch module is not active") InvitePage: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Twitch/invite.html") InvitePage.replace( msg=msg, channel_id=channel_id, ) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Twitch - Invite", header=getNavbar(active="twitch"), main=InvitePage) return cls.response(body=site, status=200, content_type='text/html')
async def twitchMain(cls:"PhaazebotWeb", WebRequest:ExtendedRequest) -> Response: """ Default url: /twitch """ PhaazeTwitch:"PhaazebotTwitch" = cls.BASE.Twitch if not PhaazeTwitch: return await cls.Tree.errors.notAllowed(cls, WebRequest, msg="Twitch module is not active") # if already logged in, change appearance AuthTwitch:AuthTwitchWebUser = await authTwitchWebUser(cls, WebRequest) TwitchMain:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Twitch/main.html") TwitchMain.replace( replace_empty=True, hide_ask_login=f"true" if AuthTwitch.found else "false", hide_dash_href=f"true" if not AuthTwitch.found else "false", already_logged_in_dashboard_link=f"/twitch/dashboard/{AuthTwitch.User.user_id}" if AuthTwitch.found else "", twitch_login_link=generateTwitchAuthLink(cls.BASE) ) site:str = cls.HTMLRoot.replace( replace_empty=True, title="Phaaze | Twitch", header=getNavbar(active="twitch"), main=TwitchMain ) return cls.response( body=site, status=200, content_type='text/html' )
async def discordLogin(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, msg: str = "") -> Response: """ Default url: /discord/login """ PhaazeDiscord: "PhaazebotDiscord" = cls.BASE.Discord if not PhaazeDiscord: return await cls.Tree.errors.notAllowed( cls, WebRequest, msg="Discord module is not active") AuthDiscord: AuthDiscordWebUser = await authDiscordWebUser(cls, WebRequest) if AuthDiscord.found: return await cls.Tree.Discord.discordmain.discordMain(cls, WebRequest) query_error: str = WebRequest.query.get("error", '') if query_error == "missing": msg = "Missing code from Discord" elif query_error == "discord": msg = "Error while getting information's from Discord" elif query_error == "database": msg = "Error while inserting data into database" DiscordLogin: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Discord/login.html") DiscordLogin.replace(replace_empty=True, msg=msg, login_link=generateDiscordAuthLink(cls.BASE)) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Discord - Login", header=getNavbar(active="discord"), main=DiscordLogin) return cls.response(body=site, status=200, content_type='text/html')
async def discordInvite(cls:"WebIndex", WebRequest:Request, msg:str="", guild_id:str="") -> Response: """ Default url: /discord/invite """ PhaazeDiscord:"PhaazebotDiscord" = cls.Web.BASE.Discord if not PhaazeDiscord: return await notAllowed(cls, WebRequest, msg="Discord module is not active") guild_id:str = WebRequest.query.get("guild", guild_id) Perm:discord.Permissions = discord.Permissions(permissions=8) Guild:discord.Object = discord.Object(id=guild_id) invite_link:str = discord.utils.oauth_url(cls.Web.BASE.Vars.DISCORD_BOT_ID, permissions=Perm, guild=Guild, redirect_uri=None) InvitePage:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Discord/invite.html") InvitePage.replace( invite_link = invite_link, msg = msg ) site:str = cls.HTMLRoot.replace( replace_empty = True, title = "Phaaze | Discord - Invite", header = getNavbar(active="discord"), main = InvitePage ) return cls.response( body=site, status=200, content_type='text/html' )
async def discordView(cls: "PhaazebotWeb", WebRequest: ExtendedRequest) -> Response: """ Default url: /discord/view/{guild_id:\d+} """ PhaazeDiscord: "PhaazebotDiscord" = cls.BASE.Discord if not PhaazeDiscord: return await cls.Tree.errors.notAllowed( cls, WebRequest, msg="Discord module is not active") guild_id: str = WebRequest.match_info.get("guild_id", "") Guild: discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id)) if not Guild: return await cls.Tree.Discord.discordinvite.discordInvite( WebRequest, msg=f"Phaaze is not on this Server", guild_id=guild_id) ViewPage: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Discord/view.html") ViewPage.replace(guild_id=Guild.id, guild_icon_url=Guild.icon_url, guild_name=Guild.name) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Discord - View", header=getNavbar(active="discord"), main=ViewPage) return cls.response(body=site, status=200, content_type='text/html')
async def adminManageUser(cls:"PhaazeWebIndex", WebRequest:ExtendedRequest) -> Response: """ Default url: /admin/manage-user """ WebAuth:AuthWebUser = await authWebUser(cls, WebRequest) if not WebAuth.found: return await cls.Tree.Account.accountlogin.accountLogin(WebRequest) if not WebAuth.User.checkRoles(["admin", "superadmin"]): return await cls.Tree.errors.notAllowed(WebRequest, msg="Admin rights required") AdminManageUser:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Admin/manage-user.html") site:str = cls.HTMLRoot.replace( replace_empty=True, title="Phaaze | Admin - User manager", header=getNavbar(), main=AdminManageUser ) return cls.response( body=site, status=200, content_type='text/html' )
def __init__(self, BASE:"Phaazebot"): super().__init__() self.BASE:"Phaazebot" = BASE self.Tree = WebProcessing self._client_max_size = self.BASE.Limit.web_client_max_size self.port:int = 9001 self.SSLContext:Optional[ssl.SSLContext] = None self.HTMLRoot:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/root.html", template=True) self.middlewares.append(self.middlewareHandler)
async def underDev(cls: "PhaazebotWeb", WebRequest: ExtendedRequest) -> Response: cls.BASE.Logger.debug(f"(Web) 400: {WebRequest.path}", require="web:underDev") SiteDev: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/underDev.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Under development", header=getNavbar(), main=SiteDev.content) return cls.response(body=site, status=400, content_type='text/html')
async def mainSite(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: / """ MainSite: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/main.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze", header=getNavbar(), main=MainSite) return cls.response(body=site, status=200, content_type='text/html')
def getAccountModal() -> HTMLFormatter: """ get the global login form with all applied formated links etc... """ try: discord_login_link: str = GlobalStorage.get( "Phaazebot").Vars.DISCORD_LOGIN_LINK except: discord_login_link: str = "/discord?error" AccountModal: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Modal/account.html") AccountModal.replace(replace_empty=True, discord_login_link=discord_login_link) return AccountModal
async def discordDashboard(cls:"WebIndex", WebRequest:Request) -> Response: """ Default url: /discord/dashboard/{guild_id:\d+} """ PhaazeDiscord:"PhaazebotDiscord" = cls.Web.BASE.Discord if not PhaazeDiscord: return await notAllowed(cls, WebRequest, msg="Discord module is not active") guild_id:str = WebRequest.match_info.get("guild_id", "") Guild:discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id if guild_id.isdigit() else 0)) if not Guild: return await discordInvite(cls, WebRequest, msg=f"Phaaze is not on this Server", guild_id=guild_id) DiscordUser:DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await cls.discordLogin(WebRequest) DiscordDash:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/main.html") DiscordDash.replace( location_home = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_home.html"), location_configs = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs.html"), location_commands = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_commands.html"), location_levels = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_levels.html"), location_quotes = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_quotes.html"), location_twitch_alerts = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_twitch_alerts.html"), location_assign_roles = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_assign_roles.html") ) # make it twice, since some included locations also have replaceable items DiscordDash.replace( guild_name = html.escape(Guild.name), guild_id = Guild.id, web_root = cls.Web.BASE.Vars.WEB_ROOT ) site:str = cls.HTMLRoot.replace( replace_empty = True, title = f"Phaaze | Discord - Dashboard: {Guild.name}", header = getNavbar(active="discord"), main = DiscordDash ) return cls.response( body=site, status=200, content_type='text/html' )
def getNavbar(active: str = "") -> HTMLFormatter: """ get the upper nav bar html, with all applied styles based on current location """ Navbar: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Navbar/default.html") Navbar.replace(account_modal=getAccountModal()) Navbar.setRegex(r"\{selected_(.+?)\}") Navbar.replace(replace_empty=True, **{active: "active"}) return Navbar
async def accountLogin(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /account/login """ WebUser: WebUserInfo = await cls.getWebUserInfo(WebRequest) if WebUser.found: return await cls.accountMain(WebRequest) AccountLogin: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Account/login.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Account - Login", header=getNavbar(), main=AccountLogin) return cls.response(body=site, status=200, content_type='text/html')
async def notAllowed(cls: "PhaazebotWeb", WebRequest: ExtendedRequest, msg: str = "") -> Response: req_str: str = html.escape("Not Allowed: " + WebRequest.path) cls.BASE.Logger.debug(f"(Web) 401: {WebRequest.path}", require="web:401") Site401: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/actionNotAllowed.html") Site401.replace(msg=msg, path=req_str) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Not Allowed", header=getNavbar(), main=Site401.content) return cls.response(body=site, status=401, content_type='text/html')
async def notFound(cls: "WebIndex", WebRequest: Request, msg: str = "") -> Response: req_str: str = html.escape("Not Found: " + WebRequest.path) cls.Web.BASE.Logger.debug(f"(Web) 404: {WebRequest.path}", require="web:404") Site404: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/pageNotFound.html") Site404.replace(msg=msg, path=req_str) site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Not Found", header=getNavbar(), main=Site404.content) return cls.response(body=site, status=404, content_type='text/html')
async def adminManageRole(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /admin/manage-role """ WebUser: WebUserInfo = await cls.getWebUserInfo(WebRequest) if not WebUser.found: return await cls.accountLogin(WebRequest) if not WebUser.checkRoles(["admin", "superadmin"]): return await cls.notAllowed(WebRequest, msg="Admin rights required") AdminManageRole: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Admin/manage-role.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Admin - Role manager", header=getNavbar(), main=AdminManageRole) return cls.response(body=site, status=200, content_type='text/html')
async def accountMain(cls: "PhaazebotWeb", WebRequest: ExtendedRequest) -> Response: """ Default url: /account """ WebUser: AuthWebUser = await authWebUser(cls, WebRequest) if not WebUser.found: return await cls.Tree.Account.accountlogin.accountLogin( cls, WebRequest) AccountPage: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Account/main.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Account", header=getNavbar(), main=AccountPage) return cls.response(body=site, status=200, content_type='text/html')
async def twitchDashboard(cls: "PhaazebotWeb", WebRequest: ExtendedRequest) -> Response: """ Default url: /twitch/dashboard/{channel_id:\d+} """ PhaazeTwitch: "PhaazebotTwitch" = cls.BASE.Twitch if not PhaazeTwitch: return await cls.Tree.errors.notAllowed( cls, WebRequest, msg="Twitch module is not active") channel_id: str = WebRequest.match_info.get("channel_id", "") PhaazeTwitch.getChannel() Channel: Optional[twitch_irc.Channel] = PhaazeTwitch.getChannel( channel_id=channel_id) if not Channel: return await cls.Tree.Twitch.twitchinvite.twitchInvite( cls, WebRequest, msg=f"Twitch Channel not found...", channel_id=channel_id) AuthTwitch: AuthTwitchWebUser = await authTwitchWebUser(cls, WebRequest) if not AuthTwitch.found: return await cls.Tree.Twitch.twitchlogin.twitchLogin(cls, WebRequest) TwitchDash: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Twitch/Dashboard/main.html") TwitchDash.replace(replace_empty=False) # make it twice, since some included locations also have replaceable items TwitchDash.replace(channel_name=html.escape(Channel.name), channel_id=Channel.channel_id, web_root=cls.BASE.Vars.web_root) site: str = cls.HTMLRoot.replace( replace_empty=True, title=f"Phaaze | Twitch - Dashboard: {Channel.name}", header=getNavbar(active="twitch"), main=TwitchDash) return cls.response(body=site, status=200, content_type='text/html')
async def discordCommands(cls: "PhaazebotWeb", WebRequest: ExtendedRequest) -> Response: """ Default url: /discord/commands/{guild_id:\d+} """ PhaazeDiscord: "PhaazebotDiscord" = cls.BASE.Discord if not PhaazeDiscord: return await cls.Tree.errors.notAllowed( cls, WebRequest, msg="Discord module is not active") guild_id: str = WebRequest.match_info.get("guild_id", "") Guild: discord.Guild = discord.utils.get( PhaazeDiscord.guilds, id=int(guild_id if guild_id.isdigit() else 0)) if not Guild: return await cls.Tree.Discord.discordinvite.discordInvite( cls, WebRequest, msg=f"Phaaze is not on this Server", guild_id=guild_id) GuildSettings: DiscordServerSettings = await getDiscordSeverSettings( PhaazeDiscord, guild_id, prevent_new=True) currency_name: str = GuildSettings.currency_name if GuildSettings.currency_name else cls.BASE.Vars.default_discord_currency currency_name_multi: str = GuildSettings.currency_name_multi if GuildSettings.currency_name_multi else cls.BASE.Vars.default_discord_currency_multi DiscordCommand: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Discord/commands.html") DiscordCommand.replace(guild_name=html.escape(Guild.name), guild_id=str(Guild.id), guild_currency=currency_name, guild_currency_multi=currency_name_multi) site: str = cls.HTMLRoot.replace( replace_empty=True, title=f"Phaaze | Discord - Commands: {Guild.name}", header=getNavbar(active="discord"), main=DiscordCommand) return cls.response(body=site, status=200, content_type='text/html')
def getAccountModal() -> HTMLFormatter: """ get the global login form with all applied formatted links etc... """ PhaazeMain: "Phaazebot" = GlobalStorage.get("Phaazebot") try: discord_login_link: str = generateDiscordAuthLink(PhaazeMain) twitch_login_link: str = generateTwitchAuthLink(PhaazeMain) except Exception as E: PhaazeMain.Logger.error(f"getAccountModal - {str(E)}") discord_login_link: str = "/discord?error" twitch_login_link: str = "/twitch?error" AccountModal: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Modal/account.html") AccountModal.replace( replace_empty=True, discord_login_link=discord_login_link, twitch_login_link=twitch_login_link, ) return AccountModal
async def discordMain(cls: "WebIndex", WebRequest: Request) -> Response: """ Default url: /discord """ PhaazeDiscord: "PhaazebotDiscord" = cls.Web.BASE.Discord if not PhaazeDiscord: return await notAllowed(cls, WebRequest, msg="Discord module is not active") DiscordUser: DiscordWebUserInfo = await cls.getDiscordUserInfo(WebRequest) if not DiscordUser.found: return await cls.discordLogin(WebRequest) DiscordMain: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/Discord/main.html") site: str = cls.HTMLRoot.replace(replace_empty=True, title="Phaaze | Discord", header=getNavbar(active="discord"), main=DiscordMain) return cls.response(body=site, status=200, content_type='text/html')
def __init__(self, Web: "PhaazebotWeb"): self.Web: "PhaazebotWeb" = Web self.Web.middlewares.append(self.middlewareHandler) self.HTMLRoot: HTMLFormatter = HTMLFormatter( "Platforms/Web/Content/Html/root.html", template=True)
async def discordDashboard(cls:"PhaazebotWeb", WebRequest:ExtendedRequest) -> Response: """ Default url: /discord/dashboard/{guild_id:\d+} """ PhaazeDiscord:"PhaazebotDiscord" = cls.BASE.Discord if not PhaazeDiscord: return await cls.Tree.errors.notAllowed(cls, WebRequest, msg="Discord module is not active") guild_id:str = WebRequest.match_info.get("guild_id", "") Guild:discord.Guild = discord.utils.get(PhaazeDiscord.guilds, id=int(guild_id if guild_id.isdigit() else 0)) if not Guild: return await cls.Tree.Discord.discordinvite.discordInvite(cls, WebRequest, msg=f"Phaaze is not on this Server", guild_id=guild_id) AuthDiscord:AuthDiscordWebUser = await authDiscordWebUser(cls, WebRequest) if not AuthDiscord.found: return await cls.Tree.Discord.discordlogin.discordLogin(cls, WebRequest) CheckMember:discord.Member = Guild.get_member(int(AuthDiscord.User.user_id)) if not CheckMember: return cls.response(status=302, headers={"Location": f"/discord/view/{guild_id}?error=no_user"}) if not (CheckMember.guild_permissions.administrator or CheckMember.guild_permissions.manage_guild): return cls.response(status=302, headers={"Location": f"/discord/view/{guild_id}?error=missing_permissions"}) DiscordDash:HTMLFormatter = HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/main.html") DiscordDash.replace( location_home=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_home.html"), location_quotes=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_quotes.html"), location_twitch_alerts=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_twitch_alerts.html"), location_regulars=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_regulars.html"), location_levels=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_levels.html"), location_configs_chat=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs_chat.html"), location_configs_event=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs_event.html"), location_configs_level=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs_level.html"), location_configs_channel=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs_channel.html"), location_configs_master=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_configs_master.html"), location_commands_command=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_commands_command.html"), location_commands_help=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_commands_help.html"), location_commands_assign=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_commands_assign.html"), location_logs=HTMLFormatter("Platforms/Web/Content/Html/Discord/Dashboard/location_logs.html"), ) # make it twice, since some included locations also have replaceable items DiscordDash.replace( guild_name=html.escape(Guild.name), guild_id=Guild.id, web_root=cls.BASE.Vars.web_root ) site:str = cls.HTMLRoot.replace( replace_empty=True, title=f"Phaaze | Discord - Dashboard: {Guild.name}", header=getNavbar(active="discord"), main=DiscordDash ) return cls.response( body=site, status=200, content_type='text/html' )