-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle.go
110 lines (99 loc) · 2.74 KB
/
handle.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
107
108
109
110
package main
import (
"encoding/hex"
"fmt"
"github.com/hoisie/mustache"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
func handle(upd tgbotapi.Update) {
if upd.Message != nil {
if upd.Message.Sticker != nil && upd.Message.ReplyToMessage != nil && upd.Message.Sticker.SetName == "PoohSocialCredit" {
// get params
points := 0
switch hex.EncodeToString([]byte(upd.Message.Sticker.Emoji)) {
case "f09f989e": // -20
points = -20
case "f09f9884": // +20
points = +20
default:
return
}
// check user is admin
chatmember, err := bot.GetChatMember(tgbotapi.ChatConfigWithUser{
ChatID: upd.Message.Chat.ID,
SuperGroupUsername: upd.Message.Chat.ChatConfig().SuperGroupUsername,
UserID: upd.Message.From.ID,
})
if err != nil ||
(chatmember.Status != "administrator" && chatmember.Status != "creator") {
log.Print("not admin")
return
}
// save scores
_, err = pg.Exec(`
INSERT INTO events
(chat_id, user_id, creator_id, credits, telegram_update)
VALUES ($1, $2, $3, $4, $5)
`,
upd.Message.Chat.ID,
upd.Message.ReplyToMessage.From.ID,
upd.Message.From.ID,
points,
upd.UpdateID,
)
if err != nil {
log.Warn().Err(err).Msg("failed to save event")
sendMessage(upd.Message.Chat.ID, "Error!")
return
}
sendMessage(upd.Message.Chat.ID,
fmt.Sprintf(
"%s credits saved.",
userName(upd.Message.ReplyToMessage.From),
),
)
} else if upd.Message.Text == "/credits" {
var scores []Score
err = pg.Select(&scores, `
SELECT * FROM (
SELECT user_id, sum(credits) AS credits
FROM events
WHERE chat_id = $1
GROUP BY user_id
)x ORDER BY credits DESC
`, upd.Message.Chat.ID)
if err != nil {
log.Warn().Err(err).Msg("error fetching scores")
sendMessage(upd.Message.Chat.ID, "Error!")
return
}
// fetch user names
for i, score := range scores {
// fallback
scores[i].Name = fmt.Sprintf("user:%d", score.UserId)
chatmember, err := bot.GetChatMember(tgbotapi.ChatConfigWithUser{
ChatID: upd.Message.Chat.ID,
SuperGroupUsername: upd.Message.Chat.ChatConfig().SuperGroupUsername,
UserID: int(score.UserId),
})
if err != nil {
continue
}
scores[i].Name = userName(chatmember.User)
}
sendMessage(upd.Message.Chat.ID,
mustache.Render(`<b>Credits</b>
{{#scores}}<code>{{Credits}}</code>: {{Name}}
{{/scores}}
`, map[string]interface{}{
"scores": scores,
}),
)
}
}
}
type Score struct {
UserId int64 `db:"user_id"`
Credits int64 `db:"credits"`
Name string
}