-
Notifications
You must be signed in to change notification settings - Fork 0
/
karma_bot.go
200 lines (180 loc) · 4.83 KB
/
karma_bot.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"github.com/nlopes/slack"
"strings"
"time"
)
var ValidEmojis = []string{
"+1",
"god",
"sbrr",
}
var IgnoreWords = []string{
"\n",
"`",
" ",
}
func containIgnoreWords(target string) bool {
for _, word := range IgnoreWords {
if strings.Contains(target, word) {
return true
}
}
return false
}
type KarmaBot struct {
userKarmaRepo UserKarmaRepo
slack *slack.Client
userId string // Bot 自身のユーザーID
}
func NewKarmaBot (repo UserKarmaRepo, client *slack.Client) (*KarmaBot, error) {
bot := &KarmaBot{userKarmaRepo: repo, slack: client}
// API を叩いてBot自身のIDを取得
res, err := client.AuthTest()
if err != nil {
return nil, err
}
bot.userId = res.UserID
return bot, nil
}
func (k *KarmaBot) parseUser(nameOrId string) User {
user, err := k.userKarmaRepo.GetById(nameOrId)
if err == nil {
return user
}
user, _ = k.userKarmaRepo.GetByName(nameOrId)
return user
}
// カルマデータ生成処理
func (k *KarmaBot) parseKarma(text string, giver string, channel string) (karmaList []Karma, err error) {
count := float64(strings.Count(text, "+") - 1) // 雑
// ++ → 1、 +++ → 1.1、 ++++ → 1.2 ... のように変換する
if count > 1 {
count = 1.0 + (count-1)/10.0
}
// + の前のテキストを抽出する
sp := strings.Split(text, "+")
receivers := sp[0]
for _, name := range strings.Split(receivers, " ") {
if name == "" {
continue
}
if containIgnoreWords(name) {
continue
}
name = strings.ReplaceAll(name, "@", "")
name = strings.ReplaceAll(name, "<", "")
name = strings.ReplaceAll(name, ">", "")
name = strings.TrimSpace(name)
user := k.parseUser(name)
karmaList = append(karmaList, Karma{
Giver: giver,
Receiver: user.Id,
Count: count,
Channel: channel,
})
}
return
}
// カルマ付与イベント
func (k *KarmaBot) giveKarmaEvent(event *slack.MessageEvent) error {
karmaList, err := k.parseKarma(event.Text, event.User, event.Channel)
if err != nil {
return err
}
err = k.userKarmaRepo.Save(karmaList)
if err != nil {
return err
}
fmt.Println("Get karma", karmaList)
// リアクションをつける
ref := slack.ItemRef{Timestamp: event.Timestamp, Channel: event.Channel}
err = k.slack.AddReaction("thumbsup_all", ref)
return err
}
// ランキング表示イベント
func (k *KarmaBot) getKarmaRankingEvent(event *slack.MessageEvent) error {
from := time.Now().AddDate(0, 0, -7)
to := time.Now()
ranking, err := k.userKarmaRepo.Ranking(KindReceiver, from, to)
if err != nil {
return err
}
msg := ""
for i, r := range ranking.Ranks {
msg += fmt.Sprintf("%d. <@%s> (%.2fpt)\n", i + 1, r.User, r.Count)
}
text := slack.MsgOptionText(msg, false)
ts := slack.MsgOptionTS(event.Timestamp)
emoji := slack.MsgOptionIconEmoji(":karma:")
_, _, err = k.slack.PostMessage(event.Channel, text, ts, emoji)
return err
}
func (k *KarmaBot) showHelpEvent(event *slack.MessageEvent) error {
text := slack.MsgOptionText(`ランキングを見たい場合はメンションのあとに「ランキング」とつけてください。1週間分のランキングがみれます`, false)
ts := slack.MsgOptionTS(event.Timestamp)
emoji := slack.MsgOptionIconEmoji(":karma:")
_, _, err := k.slack.PostMessage(event.Channel, text, ts, emoji)
return err
}
// Botが Join しているチャンネルに投稿されたもの処理する
func handleMessageEvent(bot *KarmaBot, event *slack.MessageEvent) error {
text := event.Text
if strings.Contains(text, "++") {
return bot.giveKarmaEvent(event)
}
// Bot に対するリプライ
if strings.Contains(text, bot.userId) {
if strings.Contains(text, "ランキング") {
return bot.getKarmaRankingEvent(event)
}
return bot.showHelpEvent(event)
}
return nil
}
// スタンプに対する処理をする
func handleReactionEvent(bot *KarmaBot, event *slack.ReactionAddedEvent) error {
var ok bool
for _, w := range ValidEmojis {
if strings.HasPrefix(event.Reaction, w) {
ok = true
}
}
if !ok {
return nil
}
karma := Karma{
Giver: event.User,
Receiver: event.ItemUser,
Count: 0.1,
Channel: event.Item.Channel,
}
if err := bot.userKarmaRepo.Save([]Karma{karma}); err != nil {
return err
}
// リアクションをつける
ref := slack.ItemRef{Timestamp: event.Item.Timestamp, Channel: event.Item.Channel}
err := bot.slack.AddReaction("eyes", ref)
return err
}
func eventReceiver(bot *KarmaBot, msg slack.RTMEvent) error {
switch ev := msg.Data.(type) {
case *slack.ReactionAddedEvent:
err := handleReactionEvent(bot, ev)
if err != nil {
return err
}
case *slack.MessageEvent:
err := handleMessageEvent(bot, ev)
if err != nil {
return err
}
case *slack.RTMError:
return fmt.Errorf("rtm error: %v", ev)
case *slack.InvalidAuthEvent:
return fmt.Errorf("invalid credentials: %v", ev)
default:
}
return nil
}