This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (168 loc) · 5.81 KB
/
main.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
// 🌱 miku: Tiny, stateless microservice to notify that your Discord bot is going under maintenance, made in Go
// Copyright (c) 2022 Nino
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"bytes"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"strings"
"syscall"
"text/template"
)
var (
// version returns the current version of Miku.
version = "master"
// commitSha returns the commit hash of when Miku was built.
commitSha = "unknown"
// buildDate returns the build date of when Miku was last built.
buildDate = "???"
)
// MessageData represents the structure of using Go templates to customize
// the "undergoing maintenance" message that is sent once in all guilds.
type MessageData struct {
// DiscordServer is the discord server to redirect users, this can be nil
// if none was specified.
DiscordServer string
// Bot returns the bots username#discriminator
Bot string
}
func init() {
// Setup .env file is there is any
if _, err := os.Stat("./.env"); err == nil || !os.IsNotExist(err) {
if err := godotenv.Load("./.env"); err != nil {
panic(err)
}
}
// If the debug variable exists, let's put it into debug mode. :)
if _, ok := os.LookupEnv("MIKU_DEBUG"); ok {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
logrus.Infof("Using v%s (commit=%s, built=%s) of Miku", version, commitSha, buildDate)
}
func main() {
logrus.Info("Starting up miku...")
message := "Bot **{{.Bot}}** is undergoing maintenance, please wait a bit!\nIf you need to know more, you can visit the Discord server: {{.DiscordServer}}!"
var tmpl *template.Template
var messageCache []string
var currentUser string
if m, ok := os.LookupEnv("MIKU_MESSAGE_TEMPLATE"); ok {
message = m
t := template.New("miku template")
t, err := t.Parse(message)
if err != nil {
logrus.Fatalf("Unable to parse Go template from `MIKU_MESSAGE_TEMPLATE` environment variable. %v", err)
}
tmpl = t
} else {
t := template.New("miku default template")
t, err := t.Parse(message)
if err != nil {
logrus.Fatalf("This should never happen; the default template was unable to be parsed: %v", err)
}
tmpl = t
}
// Check if we can grab the Discord token
token, ok := os.LookupEnv("MIKU_DISCORD_TOKEN")
if !ok {
logrus.Fatalf("Missing `MIKU_DISCORD_TOKEN` environment variable.")
}
session, err := discordgo.New("Bot " + token)
if err != nil {
logrus.Fatal("Unable to create a Discord session:", err)
}
session.AddHandler(func(s *discordgo.Session, ready *discordgo.Ready) {
logrus.Debugf("Using Discord Gateway v%d", ready.Version)
logrus.Infof("Successfully connected to Discord as %s.", fmt.Sprintf("%s#%s (%s)", ready.User.Username, ready.User.Discriminator, ready.User.ID))
currentUser = fmt.Sprintf("%s#%s", ready.User.Username, ready.User.Discriminator)
if err := s.UpdateStatusComplex(discordgo.UpdateStatusData{
Status: "dnd",
Activities: []*discordgo.Activity{
{
Name: "the servers go whirrrr...",
Type: 3,
},
},
}); err != nil {
logrus.Error("Unable to set presence:", err)
}
})
session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bots
if m.Author.Bot {
return
}
// Skip if we do not have the `MIKU_PREFIX` environment variable,
// so we do not spam guilds
prefix, ok := os.LookupEnv("MIKU_PREFIX")
if !ok {
return
}
if !strContains(messageCache, m.GuildID) {
if strings.HasPrefix(m.Content, prefix) {
// Execute the Go template on the reader
data := &MessageData{
Bot: currentUser,
DiscordServer: os.Getenv("MIKU_DISCORD_SERVER"),
}
writer := bytes.NewBufferString("")
if err := tmpl.Execute(writer, data); err != nil {
logrus.Error("Unable to execute Go template:", err)
return
}
if _, err := s.ChannelMessageSend(m.ChannelID, strings.Trim(writer.String(), " ")); err != nil {
logrus.Error("Unable to send the message in channel:", err)
return
}
messageCache = append(messageCache, m.GuildID)
}
}
})
// we only need guild messages
session.Identify.Intents = discordgo.IntentsGuildMessages
err = session.Open()
if err != nil {
logrus.Fatal("Unable to open a WebSocket connection:", err)
}
logrus.Info("We are now running! You should get logs if you're in debug mode! Press CTRL-C to exit~")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
logrus.Warn("We are now closing the connection...")
if err := session.Close(); err != nil {
logrus.Fatal("Unable to close session:", err)
} else {
logrus.Info("Goodbye... :(")
}
}
func strContains(haystack []string, needle string) bool {
for _, v := range haystack {
if needle == v {
return true
}
}
return false
}