-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
discord.go
217 lines (181 loc) · 6.68 KB
/
discord.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
package main
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/gbl08ma/ankiddie"
"github.com/underlx/disturbancesmlx/resource"
uuid "github.com/satori/go.uuid"
"github.com/underlx/disturbancesmlx/types"
"github.com/underlx/disturbancesmlx/discordbot"
)
// DiscordBot starts the Discord bot if it is enabled in the settings
func DiscordBot() {
discordBox, present := secrets.GetBox("discord")
if !present {
discordLog.Println("Discord Keybox not found, Discord functions disabled")
return
}
webKeybox, present := secrets.GetBox("web")
if !present {
discordLog.Fatal("Web keybox not present in keybox")
}
url, present := webKeybox.Get("websiteURL")
if !present {
discordLog.Fatal("Website URL not present in keybox")
}
err := discordbot.Start(rootSqalxNode, url, discordBox, discordLog,
new(BotCommandReceiver))
if err != nil {
discordLog.Println(err)
return
}
outFn := defaultAnkoOut
adminChannelID, present := discordBox.Get("adminChannel")
if present {
outFn = discordbot.BuildAnkoOutFunction(adminChannelID)
}
err = kiddie.StartAutorun(3, true, outFn)
if err != nil {
mainLog.Fatalln(err)
}
// Wait here until CTRL-C or other term signal is received.
discordLog.Println("Bot is now running.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
discordbot.Stop()
os.Exit(0)
}
// BotCommandReceiver implements discordbot.CommandReceiver
type BotCommandReceiver struct{}
// NewLineStatus is called when the bot wants to add a new line status
func (r *BotCommandReceiver) NewLineStatus(status *types.Status) {
handleNewStatusNotify(status)
}
// ControlScraper is called when the bot wants to start/stop/change a scraper
func (r *BotCommandReceiver) ControlScraper(scraper string, enable bool, messageCallback func(message string)) {
handleControlScraper(scraper, enable, messageCallback)
}
// ControlNotifs is caled when the bot wants to block/unblock sending of push notifications
func (r *BotCommandReceiver) ControlNotifs(notifType string, enable bool) {
handleControlNotifs(notifType, enable)
}
// CastDisturbanceVote is called when the bot wants to cast a disturbance vote
func (r *BotCommandReceiver) CastDisturbanceVote(line *types.Line, weight int) {
err := reportHandler.AddReportManually(types.NewLineDisturbanceReportDebug(line, "discord"), weight)
if err != nil {
discordLog.Println(err)
}
}
// ClearDisturbanceVotes is called when the bot wants to clear disturbance votes
func (r *BotCommandReceiver) ClearDisturbanceVotes(line *types.Line) {
reportHandler.ClearVotesForLine(line)
}
// GetDisturbanceVotes is called when the bot wants to show current disturbance report status
func (r *BotCommandReceiver) GetDisturbanceVotes(messageCallback func(message string)) {
message := ""
lines, err := types.GetLines(rootSqalxNode)
if err != nil {
discordLog.Println(err)
}
for _, line := range lines {
message += fmt.Sprintf("`%s`: %d/%d\n", line.ID, reportHandler.CountVotesForLine(line), reportHandler.GetThresholdForLine(line))
}
messageCallback(message)
}
// GetThresholdMultiplier is called when the bot wants to know the current vote threshold multiplier
func (r *BotCommandReceiver) GetThresholdMultiplier() float32 {
return reportHandler.ThresholdMultiplier()
}
// SetThresholdMultiplier is called when the bot wants to set the current vote threshold multiplier
func (r *BotCommandReceiver) SetThresholdMultiplier(multiplier float32) {
reportHandler.SetThresholdMultiplier(multiplier)
}
// GetThresholdOffset is called when the bot wants to know the current vote threshold offset
func (r *BotCommandReceiver) GetThresholdOffset() int {
return reportHandler.ThresholdOffset()
}
// SetThresholdOffset is called when the bot wants to set the current vote threshold offset
func (r *BotCommandReceiver) SetThresholdOffset(offset int) {
reportHandler.SetThresholdOffset(offset)
}
// GetVersion is called when the bot wants to get the current server version
func (r *BotCommandReceiver) GetVersion() (gitCommit string, buildDate string) {
return GitCommit, BuildDate
}
// GetStats is called when the bot wants to get the current server stats
func (r *BotCommandReceiver) GetStats() (dbOpenConnections, apiTR int) {
return rdb.Stats().OpenConnections, apiTotalRequests
}
// SendNotificationMetaBroadcast sends a FCM message containing a notification to show on some/all clients
func (r *BotCommandReceiver) SendNotificationMetaBroadcast(shardID, shardMax int, versionFilter, localeFilter, title, body, url string) {
id, err := uuid.NewV4()
if err != nil {
return
}
SendMetaBroadcast(shardID, shardMax,
id.String(), "notification", versionFilter, localeFilter,
[2]string{"title", title},
[2]string{"body", body},
[2]string{"url", url})
}
// SendCommandMetaBroadcast sends a FCM message containing a command to run on some/all clients
func (r *BotCommandReceiver) SendCommandMetaBroadcast(shardID, shardMax int, versionFilter, localeFilter, command string, args ...string) {
id, err := uuid.NewV4()
if err != nil {
return
}
SendMetaBroadcast(shardID, shardMax,
id.String(), "command", versionFilter, localeFilter,
[2]string{"command", command},
[2]string{"args", strings.Join(args, "|")})
}
// GetAnkiddie returns a reference to the global Ankiddie system
func (r *BotCommandReceiver) GetAnkiddie() *ankiddie.Ankiddie {
return kiddie
}
// SetMQTTGatewayEnabled enables or disables the MQTT gateway
func (r *BotCommandReceiver) SetMQTTGatewayEnabled(enabled bool) string {
if resource.EnableMQTTGateway == enabled {
return "already"
}
if enabled {
err := mqttGateway.Start()
if err != nil {
mainLog.Println(err)
return err.Error()
}
} else {
err := mqttGateway.Stop()
if err != nil {
mainLog.Println(err)
return err.Error()
}
}
resource.EnableMQTTGateway = enabled
return "ok"
}
// SendMQTTGatewayCommand sends a command to the MQTT subsystem
func (r *BotCommandReceiver) SendMQTTGatewayCommand(command string, args ...string) string {
return mqttGateway.HandleControlCommand(command, args...)
}
// SetAPIMOTDforLocale sets the "message of the day" of the API for the specified locale
func (r *BotCommandReceiver) SetAPIMOTDforLocale(locale, html string) {
resource.SetMOTDHTMLForLocale(locale, html)
}
// SetAPIMOTDpriority sets the "message of the day" priority
func (r *BotCommandReceiver) SetAPIMOTDpriority(priority int) {
resource.SetMOTDPriority(priority)
}
// SetAPIMOTDmainLocale sets the "message of the day" main locale
func (r *BotCommandReceiver) SetAPIMOTDmainLocale(mainLocale string) {
resource.SetMOTDMainLocale(mainLocale)
}
// ClearAPIMOTD clears the API MOTD
func (r *BotCommandReceiver) ClearAPIMOTD() {
resource.ClearMOTD()
}