2023-10-07 22:46:02 -07:00
/ * *
* server . ts
* Web server for the scoreboard page .
* /
import { Database as Database3 } from 'sqlite3' ;
import { Database , open } from 'sqlite' ;
import express = require ( 'express' ) ;
import 'dotenv/config' ;
2023-10-08 19:20:00 -07:00
import { logInfo } from './logging' ;
2023-10-07 22:46:02 -07:00
import { ScoreboardMessageRow , ScoreboardUserRow } from './models' ;
const app = express ( ) ;
app . use ( express . static ( 'public' ) ) ;
app . set ( 'view engine' , 'pug' ) ;
const port = process . env . PORT ;
let db : Database = null ;
async function openDb() {
return open ( {
filename : 'discord/db.sqlite' ,
driver : Database3
2023-10-08 13:48:41 -07:00
} ) ;
2023-10-07 22:46:02 -07:00
}
app . get ( '/' , async ( req , res ) = > {
2023-10-08 22:10:27 -07:00
const msg1 = await db . all < [ ScoreboardMessageRow ] > ( 'SELECT * FROM messages ORDER BY reaction_1_count DESC LIMIT 10' ) ;
const msg2 = await db . all < [ ScoreboardMessageRow ] > ( 'SELECT * FROM messages ORDER BY reaction_2_count DESC LIMIT 10' ) ;
const msg3 = await db . all < [ ScoreboardMessageRow ] > ( 'SELECT * FROM messages ORDER BY reaction_3_count DESC LIMIT 10' ) ;
2023-10-08 13:48:41 -07:00
const bestMsg = await db . all < [ ScoreboardMessageRow ] > ( 'SELECT *, SUM(reaction_1_count)+SUM(reaction_2_count)+SUM(reaction_3_count) AS all_reacts FROM messages GROUP BY id ORDER BY all_reacts DESC LIMIT 5' ) ;
2023-10-07 22:46:02 -07:00
2023-10-08 13:48:41 -07:00
const funniest = await db . all < [ ScoreboardUserRow ] > ( 'SELECT * FROM users ORDER BY reaction_1_total DESC' ) ;
const realest = await db . all < [ ScoreboardUserRow ] > ( 'SELECT * FROM users ORDER BY reaction_2_total DESC' ) ;
const cunniest = await db . all < [ ScoreboardUserRow ] > ( 'SELECT * FROM users ORDER BY reaction_3_total DESC' ) ;
res . render ( 'index' , { funniest , realest , cunniest , msg1 , msg2 , msg3 , bestMsg } ) ;
2023-10-07 22:46:02 -07:00
} ) ;
app . listen ( port , async ( ) = > {
2023-10-08 19:10:47 -07:00
logInfo ( '[web] Opening database...' ) ;
2023-10-07 22:46:02 -07:00
db = await openDb ( ) ;
2023-10-08 19:10:47 -07:00
logInfo ( '[web] Database ready.' ) ;
logInfo ( ` [web] Listening on port ${ port } ` ) ;
2023-10-07 22:46:02 -07:00
} ) ;