1
+ import { SlashCommandBuilder } from "@discordjs/builders"
2
+ import { PrismaClient } from "@prisma/client"
3
+ import { format } from "date-fns"
4
+ import { CommandInteraction } from "discord.js"
5
+ import { mean , weeklyAmount } from "../utils"
6
+
7
+ const prisma = new PrismaClient ( )
8
+
9
+ export const data = new SlashCommandBuilder ( )
10
+ . setName ( "stats" )
11
+ . setDescription ( "Berechne die :beer: Statistiken für einen Benutzer." )
12
+ . addUserOption ( option => option . setName ( "user" ) . setDescription ( "Ein Benutzer" ) )
13
+
14
+ export async function execute ( interaction : CommandInteraction ) {
15
+ const targetUser = interaction . options . getUser ( "user" )
16
+ const filter = targetUser ? { userId : targetUser . id } : { }
17
+ const results = await prisma . consumption . aggregate ( {
18
+ where : filter ,
19
+ _sum : { amount : true } ,
20
+ _min : { timestamp : true }
21
+ } )
22
+
23
+ if ( ! results . _min . timestamp ) {
24
+ throw new Error ( "invalid date" )
25
+ }
26
+
27
+ const weekly = await weeklyAmount ( filter )
28
+ const weeklyAverage = mean ( weekly . map ( d => d . amount ) )
29
+
30
+ let reply : string ;
31
+ const date = format ( results . _min . timestamp , "dd.MM.yyyy" )
32
+ if ( targetUser ) {
33
+ reply = `<@${ targetUser . id } > hat seit ${ date } insgesamt ${ results . _sum . amount } :beers: getrunken!`
34
+ } else {
35
+ reply = `Seit ${ date } wurden insgesamt ${ results . _sum . amount } :beers: getrunken!`
36
+ }
37
+ reply += `\nIm Durchschnitt sind das ${ Math . round ( weeklyAverage * 10 ) / 10 } :beers: pro Stammtisch.`
38
+ await interaction . reply ( reply )
39
+ }
0 commit comments