/
botstatus.go
300 lines (247 loc) 路 7.97 KB
/
botstatus.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package plugins
import (
"strings"
"time"
"fmt"
"github.com/Seklfreak/Robyul2/cache"
"github.com/Seklfreak/Robyul2/helpers"
"github.com/Seklfreak/Robyul2/models"
"github.com/Seklfreak/Robyul2/shardmanager"
"github.com/bwmarrin/discordgo"
humanize "github.com/dustin/go-humanize"
"github.com/globalsign/mgo/bson"
"github.com/sirupsen/logrus"
)
type botStatusAction func(args []string, in *discordgo.Message, out **discordgo.MessageSend) (next botStatusAction)
type BotStatus struct{}
func (bs *BotStatus) Commands() []string {
return []string{
"bot-status",
}
}
func (bs *BotStatus) Init(session *shardmanager.Manager) {
go func() {
defer helpers.Recover()
time.Sleep(time.Second * 60)
go bs.gameStatusRotationLoop()
}()
}
func (bs *BotStatus) gameStatusRotationLoop() {
defer helpers.Recover()
defer func() {
go func() {
bs.logger().Error("The gameStatusRotationLoop died. Please investigate! Will be restarted in 60 seconds")
time.Sleep(60 * time.Second)
bs.gameStatusRotationLoop()
}()
}()
var err error
var newStatus string
for {
var entryBucket models.BotStatusEntry
err = helpers.MdbPipeOneWithoutLogging(models.BotStatusTable, []bson.M{{"$sample": bson.M{"size": 1}}}, &entryBucket)
if err != nil {
if !helpers.IsMdbNotFound(err) {
helpers.RelaxLog(err)
}
time.Sleep(60 * time.Second)
continue
}
newStatus = bs.replaceText(entryBucket.Text)
for i, shard := range cache.GetSession().Sessions {
err = shard.UpdateStatusComplex(discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: newStatus + fmt.Sprintf(" | Shard %d", i),
Type: entryBucket.Type,
},
Status: "online",
})
helpers.RelaxLog(err)
}
bs.logger().Infof("set the Bot Status to: \"%s\" using the rotation loop", newStatus)
time.Sleep(45 * time.Minute)
}
}
func (bs *BotStatus) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
session.ChannelTyping(msg.ChannelID)
var result *discordgo.MessageSend
args := strings.Fields(content)
action := bs.actionStart
for action != nil {
action = action(args, msg, &result)
}
}
func (bs *BotStatus) actionStart(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
cache.GetSession().SessionForGuildS(in.GuildID).ChannelTyping(in.ChannelID)
if len(args) < 1 {
*out = bs.newMsg("bot.arguments.too-few")
return bs.actionFinish
}
switch args[0] {
case "add":
return bs.actionAdd
case "remove", "rem", "delete", "del":
return bs.actionRemove
case "set":
return bs.actionSet
case "list":
return bs.actionList
}
*out = bs.newMsg("bot.arguments.invalid")
return bs.actionFinish
}
func (bs *BotStatus) replaceText(text string) (result string) {
users := make(map[string]string)
channels := make(map[string]string)
var guilds int64
for _, shard := range cache.GetSession().Sessions {
for _, guild := range shard.State.Guilds {
guilds++
for _, u := range guild.Members {
users[u.User.ID] = u.User.Username
}
for _, c := range guild.Channels {
channels[c.ID] = c.Name
}
}
}
text = strings.Replace(text, "{GUILD_COUNT}", humanize.Comma(guilds), -1)
text = strings.Replace(text, "{MEMBER_COUNT}", humanize.Comma(int64(len(users))), -1)
text = strings.Replace(text, "{CHANNEL_COUNT}", humanize.Comma(int64(len(channels))), -1)
return text
}
// [p]bot-status set <status text>
func (bs *BotStatus) actionSet(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
if !helpers.IsRobyulMod(in.Author.ID) {
*out = bs.newMsg("robyulmod.no_permission")
return bs.actionFinish
}
if len(args) < 3 {
*out = bs.newMsg("bot.arguments.too-few")
return bs.actionFinish
}
statusMessage := strings.TrimSpace(strings.Replace(strings.Join(args, " "), strings.Join(args[:2], " "), "", 1))
statusType := bs.textToGameType(args[1])
newStatus := bs.replaceText(statusMessage)
var err error
for i, shard := range cache.GetSession().Sessions {
err = shard.UpdateStatusComplex(discordgo.UpdateStatusData{
Game: &discordgo.Game{
Name: newStatus + fmt.Sprintf(" | Shard %d", i),
Type: statusType,
},
Status: "online",
})
}
helpers.Relax(err)
bs.logger().WithField("UserID", in.Author.ID).Infof("Set the Bot Status to: \"%s\" using the set command", newStatus)
*out = bs.newMsg(helpers.GetTextF("plugins.botstatus.set-success", newStatus))
return bs.actionFinish
}
// [p]bot-status add <status text>
func (bs *BotStatus) actionAdd(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
if !helpers.IsRobyulMod(in.Author.ID) {
*out = bs.newMsg("robyulmod.no_permission")
return bs.actionFinish
}
if len(args) < 3 {
*out = bs.newMsg("bot.arguments.too-few")
return bs.actionFinish
}
statusMessage := strings.TrimSpace(strings.Replace(strings.Join(args, " "), strings.Join(args[:2], " "), "", 1))
statusType := bs.textToGameType(args[1])
_, err := helpers.MDbInsert(
models.BotStatusTable,
models.BotStatusEntry{
AddedByUserID: in.Author.ID,
AddedAt: time.Now(),
Text: statusMessage,
Type: statusType,
},
)
helpers.Relax(err)
*out = bs.newMsg(helpers.GetTextF("plugins.botstatus.add-success", statusMessage))
return bs.actionFinish
}
// [p]bot-status remove <status id>
func (bs *BotStatus) actionRemove(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
if !helpers.IsRobyulMod(in.Author.ID) {
*out = bs.newMsg("robyulmod.no_permission")
return bs.actionFinish
}
if len(args) < 2 {
*out = bs.newMsg("bot.arguments.too-few")
return bs.actionFinish
}
var entryBucket models.BotStatusEntry
err := helpers.MdbOne(
helpers.MdbCollection(models.BotStatusTable).Find(bson.M{"_id": helpers.HumanToMdbId(args[1])}),
&entryBucket,
)
if err != nil {
if !helpers.IsMdbNotFound(err) {
helpers.RelaxLog(err)
}
*out = bs.newMsg("bot.arguments.invalid")
return bs.actionFinish
}
err = helpers.MDbDelete(models.BotStatusTable, entryBucket.ID)
helpers.Relax(err)
*out = bs.newMsg(helpers.GetTextF("plugins.botstatus.remove-success", entryBucket.Text))
return bs.actionFinish
}
// [p]bot-status list
func (bs *BotStatus) actionList(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
if !helpers.IsRobyulMod(in.Author.ID) {
*out = bs.newMsg("robyulmod.no_permission")
return bs.actionFinish
}
var entryBucket []models.BotStatusEntry
err := helpers.MDbIter(helpers.MdbCollection(models.BotStatusTable).Find(nil)).All(&entryBucket)
helpers.Relax(err)
if entryBucket == nil || len(entryBucket) <= 0 {
*out = bs.newMsg("plugins.botstatus.list-empty")
return bs.actionFinish
}
var message string
for _, botStatus := range entryBucket {
message += fmt.Sprintf("`%s`: %s `%s`\n",
helpers.MdbIdToHuman(botStatus.ID), bs.gameTypeToText(botStatus.Type), botStatus.Text)
}
message += fmt.Sprintf("_found %d statuses in total_\n", len(entryBucket))
*out = bs.newMsg(message)
return bs.actionFinish
}
func (bs *BotStatus) textToGameType(text string) (gameType discordgo.GameType) {
switch strings.ToLower(text) {
case "playing", "game":
return discordgo.GameTypeGame
case "listening":
return discordgo.GameTypeListening
case "watching":
return discordgo.GameTypeWatching
}
return discordgo.GameTypeGame
}
func (bs *BotStatus) gameTypeToText(gameType discordgo.GameType) (text string) {
switch gameType {
case discordgo.GameTypeGame:
return "playing"
case discordgo.GameTypeListening:
return "listening to"
case discordgo.GameTypeWatching:
return "watching"
}
return ""
}
func (bs *BotStatus) actionFinish(args []string, in *discordgo.Message, out **discordgo.MessageSend) botStatusAction {
_, err := helpers.SendComplex(in.ChannelID, *out)
helpers.Relax(err)
return nil
}
func (bs *BotStatus) newMsg(content string) *discordgo.MessageSend {
return &discordgo.MessageSend{Content: helpers.GetText(content)}
}
func (bs *BotStatus) logger() *logrus.Entry {
return cache.GetLogger().WithField("module", "botstatus")
}