import express , { Router } from "express" ; import moment from "moment" ; import { TundraBot } from "../../base/TundraBot" ; import Deps from "../../utils/deps" ; import Middleware from "../modules/middleware" ; import { Route } from "./Route" ; import { messageModel } from "../../models/Message" ; import { DBGuild , guildInterface , guildModel } from "../../models/Guild" ; import Logger from "../../utils/logger" ; export default class DashboardRoutes extends Route { router : Router ; middleware : Middleware ; DBGuildManager : DBGuild ; constructor ( client ) { super ( client ) ; this . router = express . Router ( ) ; this . middleware = Deps . get < Middleware > ( Middleware ) ; this . DBGuildManager = Deps . get < DBGuild > ( DBGuild ) ; this . router . get ( "/dashboard" , ( req , res ) => { res . render ( "dashboard/index" ) ; } ) ; this . router . get ( "/servers/:id/:module" , this . middleware . validateGuild , async ( req , res ) => { const [ guild , hourlyMessages , commandCounts , messageCounts ] = await Promise . all ( [ ( ( ) => { return guildModel . findOne ( { guildID : req . params . id , } ) ; } ) ( ) , ( ( ) => { return messageModel . aggregate ( [ { $match : { guildID : req . params . id , createdAt : { $gte : moment ( Date . now ( ) ) . subtract ( 7 , "days" ) . toDate ( ) , } , } , } , { $project : { y : { $year : "$createdAt" } , m : { $month : "$createdAt" } , d : { $dayOfMonth : "$createdAt" } , h : { $hour : "$createdAt" } , } , } , { $group : { _id : { year : "$y" , month : "$m" , day : "$d" , hour : "$h" , } , count : { $sum : 1 } , } , } , ] ) ; } ) ( ) , ( ( ) => { return messageModel . aggregate ( [ { $match : { guildID : req . params . id , command : { $ne : "" , } , } , } , { $group : { _id : "$command" , count : { $sum : 1 , } , } , } , ] ) ; } ) ( ) , ( ( ) => { return messageModel . aggregate ( [ { $match : { guildID : req . params . id , } , } , { $group : { _id : { $dayOfWeek : "$updatedAt" , } , count : { $sum : 1 , } , } , } , { $sort : { _id : 1 , } , } , ] ) ; } ) ( ) , ] ) ; const data = { guild : guild , hourlyMessages : hourlyMessages , commandCounts : commandCounts , messageCounts : messageCounts , } ; res . render ( "dashboard/show" , { module : req . params . module || "overview" , savedGuild : data . guild , hourlyMessages : data . hourlyMessages , commandCounts : data . commandCounts , messageCounts : data . messageCounts , } ) ; } ) ; this . router . put ( "/servers/:id/:module/save" , this . middleware . validateGuild , async ( req , res ) => { try { const { id , module } = req . params ; if ( module == "general" ) { const settings = { } as Partial < guildInterface > ; settings . prefix = req . body . prefix ; if ( req . body . blacklistedChannelIDs ) { settings . blacklistedChannelIDs = req . body . blacklistedChannelIDs ; } else { settings . blacklistedChannelIDs = [ ] ; } settings . logMessages = { } as any ; if ( req . body . logMessagesEnabled ) { settings . logMessages . enabled = true ; settings . logMessages . channelID = req . body . logMessagesChannel ; } else { settings . logMessages . enabled = false ; } settings . welcomeMessage = { } as any ; if ( req . body . welcomeMessageEnabled == "on" ) { settings . welcomeMessage . enabled = true ; settings . welcomeMessage . welcomeMessage = req . body . welcomeMessage ; settings . welcomeMessage . channelID = req . body . welcomeMessageChannel ; } else { settings . welcomeMessage . enabled = false ; } settings . joinMessages = { } as any ; if ( req . body . joinMessagesEnabled == "on" ) { settings . joinMessages . enabled = true ; settings . joinMessages . channelID = req . body . joinMessagesChannel ; } else { settings . joinMessages . enabled = false ; } if ( req . body . joinMessagesTrackInvites == "on" ) { settings . joinMessages . trackInvites = true ; } else { settings . joinMessages . trackInvites = false ; } settings . leaveMessages = { } as any ; if ( req . body . joinMessagesEnabled == "on" ) { settings . leaveMessages . enabled = true ; settings . leaveMessages . channelID = req . body . leaveMessagesChannel ; } else { settings . leaveMessages . enabled = false ; } const guild = await req . app . get ( "client" ) . guilds . fetch ( id ) ; await this . DBGuildManager . update ( guild , settings ) ; } res . redirect ( `/servers/${ id }/${ module }` ) ; } catch ( err ) { res . render ( "errors/400" ) ; Logger . log ( "error" , `Error saving settings from dashboard (guildID: ${ req . params . id }):\n${ err }` ) ; } } ) ; } }