file description module import type { Collection , Guild } from "eris" ; import type { NextFunction , Request , Response } from "express" ; import type { Liquid } from "liquidjs" ; import type { Profile } from "passport-discord" ; import type { HibikiClient } from "../classes/Client" ; import { readdirSync } from "fs" ; import { defaultAvatar } from "./constants" ; import path from "path" ; const mergedLocales = { } ; export const checkAuth = ( req , res , next ) => { if ( req . isAuthenticated ( ) ) return next ( ) ; return res . redirect ( 301 , "../../auth/" ) ; } ; export function getAuthedUser ( user ) { return { username : user . username , discriminator : user . discriminator , guilds : user . guilds , id : user . id , avatar : user . avatar ? `https://cdn.discordapp.com/avatars/${ user . id }/${ user . avatar }.png?size=256` : defaultAvatar , } ; } function isObject ( item ) { return item && typeof item === "object" && ! Array . isArray ( item ) ; } function mergeDeep ( target , source ) { const output = Object . assign ( { } , target ) ; if ( isObject ( target ) && isObject ( source ) ) { Object . keys ( source ) . forEach ( ( key ) => { if ( isObject ( source [ key ] ) ) { if ( ! ( key in target ) ) Object . assign ( output , { [ key ] : source [ key ] } ) ; else output [ key ] = mergeDeep ( target [ key ] , source [ key ] ) ; } else { Object . assign ( output , { [ key ] : source [ key ] } ) ; } } ) ; } return output ; } export function getWebLocale ( bot , locale ) { if ( bot . config . defaultLocale === locale ) return bot . localeSystem . locales [ bot . config . defaultLocale ] ; else { if ( ! mergedLocales [ locale ] ) { const merged = mergeDeep ( bot . localeSystem . locales [ bot . config . defaultLocale ] as any , bot . localeSystem . locales [ locale ] as any ) ; mergedLocales [ locale ] = merged ; } return mergedLocales [ locale ] ; } } export async function destroySession ( req ) { return new Promise < void > ( ( resolve , reject ) => req . session . destroy ( ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ) ; } export function getManagableGuilds ( req , user , guilds ) { const managableGuilds = user . guilds . filter ( ( g ) => ( g . permissions & 32 ) === 32 || ( ( g . permissions & 8 ) === 8 && guilds . get ( g . id ) ) ) ; const guild = managableGuilds . find ( ( g ) => g . id === req . params . id ) ; return guild ; } export function loadIcons ( engine ) { const icons = { } ; const MODULES_DIRECTORY = path . resolve ( __dirname , process . env . NODE_ENV === "production" ? "../../../node_modules" : "../../node_modules" ) ; const iconPackages = [ "@fortawesome/free-solid-svg-icons" , "@fortawesome/free-brands-svg-icons" ] ; iconPackages . forEach ( ( pkg ) => { const iconDirectory = path . join ( MODULES_DIRECTORY , pkg ) ; const files = readdirSync ( iconDirectory , { withFileTypes : true } ) ; files . forEach ( ( file ) => { if ( file . isDirectory ( ) ) return ; if ( ! file . name . startsWith ( "fa" ) || ! file . name . endsWith ( ".js" ) ) return ; const icon = require ( path . join ( pkg , file . name ) ) ; icons [ icon . iconName ] = { svg : icon . svgPathData , width : icon . width , height : icon . height , } ; } ) ; } ) ; engine . registerFilter ( "icon" , ( arg ) => `<svg width="100%" height="100%" fill=currentColor preserveAspectRatio="xMidYMid meet" viewBox="0 0 ${ icons [ arg ] . width } ${ icons [ arg ] . height }"><path d="${ icons [ arg ] . svg }" /></svg>` ) ; }