-
Notifications
You must be signed in to change notification settings - Fork 1
/
userTracking.go
106 lines (85 loc) · 2.56 KB
/
userTracking.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"database/sql"
"fmt"
"log"
"os"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-co-op/gocron"
)
var (
userTrackChannel *discordgo.Channel
insertUserTrackData *sql.Stmt
queryUserTrackDataByGuildAndDate *sql.Stmt
)
func initUserTracking(s *discordgo.Session, db *sql.DB, scheduler *gocron.Scheduler) {
channelId := os.Getenv("VPBOT_USERTRACK_CHANNEL")
if len(channelId) > 0 {
var err error
userTrackChannel, err = s.Channel(channelId)
if err != nil {
log.Printf("Couldn't find the usertrack channel with ID: %s", channelId)
}
}
_, err := db.Exec("CREATE TABLE IF NOT EXISTS user_track_data (id INTEGER PRIMARY KEY, guild_id TEXT, week_number INT, year INT, user_count INT)")
if err != nil {
log.Panic(err)
}
insertUserTrackData = dbPrepare(db,
"INSERT INTO user_track_data (guild_id, week_number, year, user_count) VALUES ($1, $2, $3, $4)")
queryUserTrackDataByGuildAndDate = dbPrepare(db,
"SELECT user_count FROM user_track_data WHERE guild_id = $1 AND week_number = $2 AND year = $3")
_, err = scheduler.Every(1).Sunday().At("15:00").Do(postUserTrackingInfo)
if err != nil {
log.Panic(err)
}
}
func userCountCommandHandler(session *discordgo.Session, msg *discordgo.MessageCreate) {
guild, _ := session.State.Guild(msg.GuildID)
session.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Current user count: %d", guild.MemberCount))
}
func postUserTrackingInfo() {
guild, err := discord.State.Guild(guildID)
if err != nil {
log.Println("ERR TRYING TO GET GUILD!", guildID, err)
return
}
now := time.Now().UTC()
year, week := now.ISOWeek()
_, err = insertUserTrackData.Exec(guild.ID, week, year, guild.MemberCount)
if err != nil {
log.Printf("Error trying to insert user count data: %s", err)
}
if userTrackChannel == nil {
return
}
lastYear := year
lastWeek := week
if lastWeek-1 <= 0 {
lastYear--
} else {
lastWeek--
}
var lastWeekUserCount int
row := queryUserTrackDataByGuildAndDate.QueryRow(guild.ID, lastWeek, lastYear)
err = row.Scan(&lastWeekUserCount)
if err == sql.ErrNoRows {
discord.ChannelMessageSend(userTrackChannel.ID, fmt.Sprintf("User count in week %v: %v", week, guild.MemberCount))
return
}
diff := guild.MemberCount - lastWeekUserCount
percent := float32(diff) / float32(lastWeekUserCount) * 100
symbol := "up"
if percent < 0 {
symbol = "down"
}
discord.ChannelMessageSend(userTrackChannel.ID,
fmt.Sprintf("User count in week %v %v: %v (%s %v%%) (last week: %v)",
week,
year,
guild.MemberCount,
symbol,
percent,
lastWeekUserCount))
}