-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.go
90 lines (75 loc) · 1.84 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"database/sql"
"strconv"
"strings"
"github.com/Thelvaen/gobot/models"
"github.com/gempir/go-twitch-irc/v2"
"github.com/kataras/iris/v12"
"gorm.io/gorm"
)
var (
statement *sql.Stmt
)
func initStats() {
filters = append(filters, filter{
filterFunc: pushStats,
filterRegEx: ".*",
})
filters = append(filters, filter{
filterFunc: getCliStats,
filterRegEx: "^!score$",
})
}
func pushStats(message twitch.PrivateMessage) string {
if message.Channel != conf.Cred.Channel {
return ""
}
if len(message.Message) < 10 {
return ""
}
if strings.HasPrefix(message.Message, "!") {
return ""
}
var user models.TwitchUser
err := dataStore.Preload("Statistique").Where("twitch_id = ?", message.User.ID).First(&user).Error
if err == gorm.ErrRecordNotFound {
updateTwitchUser(message.User)
}
if err == nil {
user.Statistique.Score++
dataStore.Session(&gorm.Session{FullSaveAssociations: true}).Save(&user)
}
return ""
}
func getCliStats(message twitch.PrivateMessage) string {
// Outputting stats to the channel
if message.Channel != conf.Cred.Channel {
return ""
}
var user models.TwitchUser
err := dataStore.Preload("Statistique").Where("twitch_id = ?", message.User.ID).First(&user).Error
if err != nil {
return ""
}
return "Ton score est : " + strconv.Itoa(user.Statistique.Score)
}
func getStats(ctx iris.Context) {
var users []models.TwitchUser
data := map[string]map[string]string{
"Statistiques": {},
}
result := dataStore.Preload("Statistique").Find(&users)
if result.Error == nil {
for _, row := range users {
if row.Statistique.Score > 0 {
data["Statistiques"][row.DisplayName] = strconv.Itoa(row.Statistique.Score)
}
}
}
ctx.ViewData("Data", data)
if err := ctx.View("stats.html"); err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
ctx.Writef(err.Error())
}
}