This repository was archived by the owner on Aug 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.go
195 lines (169 loc) · 4.45 KB
/
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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/PulseDevelopmentGroup/0x626f74/command"
"github.com/PulseDevelopmentGroup/0x626f74/config"
"github.com/PulseDevelopmentGroup/0x626f74/log"
"github.com/PulseDevelopmentGroup/0x626f74/multiplexer"
"github.com/bwmarrin/discordgo"
goenv "github.com/caarlos0/env/v6"
_ "github.com/joho/godotenv/autoload"
"github.com/patrickmn/go-cache"
)
type environment struct {
Token string `env:"BOT_TOKEN"`
PerspectiveKey string `env:"PERSPECTIVE_KEY"`
Debug bool `env:"DEBUG" envDefault:"false"`
DataDir string `env:"DATA_DIR" envDefault:"data/"`
ConfigURL string `env:"CONFIG_URL"`
Fuzzy bool `env:"USE_FUZZY" envDefault:"false"`
}
var (
env = environment{}
cfg *config.BotConfig
logs *log.Logs
prefix = "!"
)
func init() {
/* Parse enviorment variables */
if err := goenv.Parse(&env); err != nil {
fmt.Println(err)
os.Exit(1)
}
/* Check if URL is being specified */
path := env.DataDir + "config.json"
if len(env.ConfigURL) > 0 {
path = env.ConfigURL
}
/* Parse config */
var err error
cfg, err = config.Get(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
/* Define logging setup */
logs = log.New(env.Debug, cfg.ErrorChannel)
}
func main() {
/* Initialize DiscordGo */
logs.Primary.Info("Starting Bot...")
dg, err := discordgo.New("Bot " + env.Token)
if err != nil {
logs.Primary.WithError(err).Error("Problem starting bot")
}
logs.Primary.Info("Bot started")
/* Initialize Mux */
mux, err := multiplexer.New(prefix)
if err != nil {
logs.Primary.WithError(err).Fatalf("Unable to create multixplexer")
}
/* Use the logging middleware with the multiplexer */
mux.UseMiddleware(logs.MuxMiddleware)
/* Set Permissions */
mux.SetPermissions(cfg.Permissions)
/* Setup Errors */
mux.SetErrors(&multiplexer.ErrorTexts{
CommandNotFound: "Command not found.",
NoPermissions: "You do not have permissions to execute that command.",
RateLimited: "You've used this command too many times, wait a bit and try again.",
})
/* === Register all the things === */
mux.Register(
command.Wiki{
Command: "wikirace",
HelpText: "Start a wikirace",
RateLimitMax: 3,
RateLimitDB: cache.New(5*time.Minute, 5*time.Minute),
Logger: logs,
},
command.Gatekeeper{
Command: "role",
HelpText: "Manage your access to roles, and their related channels",
Logger: logs,
},
command.Help{
Command: "help",
HelpText: "Displays help information regarding the bot's commands",
Logger: logs,
},
command.Inspire{
Command: "inspire",
HelpText: "Get an inspirational quote from inspirobot.me",
RateLimitMax: 3,
RateLimitDB: cache.New(5*time.Minute, 5*time.Minute),
Logger: logs,
},
command.JPEG{
Command: "jpeg",
HelpText: "More JPEG for the last image. 'nuff said",
Logger: logs,
},
command.LMGTFY{
Command: "googlehelp",
HelpText: "In case someone isn't familiar with Google",
RateLimitMax: 2,
RateLimitDB: cache.New(30*time.Minute, 30*time.Minute),
},
command.Toxic{
Command: "toxic",
HelpText: "Someone really acting up? Get a toxicity rating.",
Logger: logs,
Key: env.PerspectiveKey,
RateLimitDB: cache.New(5*time.Minute, 5*time.Minute),
RateLimitMax: 5,
},
)
for k := range cfg.SimpleCommands {
k := k
mux.RegisterSimple(multiplexer.SimpleCommand{
Command: k,
Content: cfg.SimpleCommands[k],
HelpText: "This is a simple command",
})
}
/* Configure multiplexer options */
mux.SetOptions(&multiplexer.Options{
IgnoreDMs: true,
IgnoreBots: true,
IgnoreNonDefault: true,
IgnoreEmpty: true,
})
/* Initialize the commands */
mux.Initialize()
if env.Fuzzy {
mux.UseFuzzy()
}
/* === End Register === */
/* Handle commands and start DiscordGo */
dg.AddHandler(mux.Handle)
err = dg.Open()
if err != nil {
logs.Primary.WithError(err).Error(
"Problem opening websocket connection.",
)
return
}
idle := 0
dg.UpdateStatusComplex(discordgo.UpdateStatusData{
IdleSince: &idle,
Game: &discordgo.Game{
Name: "you",
Type: discordgo.GameTypeWatching,
Assets: discordgo.Assets{
LargeImageID: "watching",
LargeText: "Watching...",
},
},
Status: "online",
})
defer dg.Close()
/* Wait for interrupt */
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}