-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathdiscord.go
153 lines (135 loc) · 4.1 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
package runner
import (
"context"
"fmt"
"strings"
"time"
sampquery "github.com/Southclaws/go-samp-query"
"github.com/bwmarrin/discordgo"
"github.com/cenkalti/backoff/v4"
"github.com/cskr/pubsub"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func RunDiscord(ctx context.Context, ps *pubsub.PubSub, cfg Config) {
panic(backoff.Retry(func() (err error) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.Errorf("Unknown panic: %v", r)
}
}
}()
runDiscord(ctx, ps, cfg)
return nil
}, &backoff.ExponentialBackOff{
InitialInterval: backoff.DefaultInitialInterval,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
MaxInterval: 0,
MaxElapsedTime: backoff.DefaultMaxElapsedTime,
Stop: backoff.Stop,
Clock: backoff.SystemClock,
}))
}
func runDiscord(ctx context.Context, ps *pubsub.PubSub, cfg Config) {
discord, err := discordgo.New("Bot " + cfg.DiscordToken)
if err != nil {
panic(err)
}
discord.ChannelMessageSend(cfg.DiscordChannelInfo, "Scavenge and Survive server starting!") //nolint:errcheck
discord.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
switch m.Message.Content {
case "/status":
server, err := sampquery.GetServerInfo(ctx, "play.scavengesurvive.com:7777", false)
if err != nil {
discord.ChannelMessageSend(cfg.DiscordChannelInfo, "Failed to query :("+err.Error()) //nolint:errcheck
} else {
discord.ChannelMessageSendEmbed(cfg.DiscordChannelInfo, &discordgo.MessageEmbed{ //nolint:errcheck
Title: "Server Status",
Type: discordgo.EmbedTypeRich,
Description: fmt.Sprintf(
"Players: %d/%d\nPing: %d",
server.Players,
server.MaxPlayers,
server.Ping,
),
Color: 0xff4700,
})
}
}
})
go func() {
if err := discord.Open(); err != nil {
panic(err)
}
}()
// these must be pre-created in order to prevent messages being missed
// during initialisation.
errorsBacktrace := ps.Sub("errors.backtrace")
infoRestart := ps.Sub("info.restart")
infoUpdate := ps.Sub("info.update")
errorsSingle := ps.Sub("errors.single")
for {
select {
case <-infoRestart:
// if _, err := discord.ChannelMessageSend(cfg.DiscordChannelInfo, "Server restart!"); err != nil {
// zap.L().Error("failed to send discord message", zap.Error(err))
// }
case d := <-infoUpdate:
if _, err := discord.ChannelMessageSend(cfg.DiscordChannelInfo, fmt.Sprintf("A server update is on the way in %s", d.(time.Duration))); err != nil {
zap.L().Error("failed to send discord message", zap.Error(err))
}
case obj := <-errorsSingle:
data, ok := obj.(map[string]string)
if !ok {
zap.L().Error("failed to get error fields", zap.Any("obj", obj))
}
title, ok := data[sampLoggerMessageKey]
if !ok {
title = "Error"
}
fields := []*discordgo.MessageEmbedField{}
for k, v := range data {
if k == sampLoggerMessageKey {
continue
}
fields = append(fields, &discordgo.MessageEmbedField{
Name: k,
Value: v,
})
}
if _, err := discord.ChannelMessageSendEmbed(cfg.DiscordChannelErrors, &discordgo.MessageEmbed{
Type: discordgo.EmbedTypeRich,
Title: title,
Fields: fields,
Color: 0xFF0000,
}); err != nil {
zap.L().Error("failed to send discord message", zap.Error(err))
}
case obj := <-errorsBacktrace:
message, ok := obj.(string)
if !ok {
zap.L().Error("failed to get error fields", zap.Any("obj", obj))
}
// filter out dialogue responses as they contain raw passwords
lines := strings.Split(message, "\n")
n := 0
for _, line := range lines {
if !strings.Contains(line, "OnDialogResponse") {
lines[n] = line
n++
}
}
lines = lines[:n]
if _, err := discord.ChannelMessageSend(cfg.DiscordChannelErrors, fmt.Sprintf("Error backtrace:\n```\n%s\n```", strings.Join(lines, "\n"))); err != nil {
zap.L().Error("failed to send discord message", zap.Error(err))
}
}
}
}