-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
79 lines (66 loc) · 1.98 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
package main
import (
"context"
"os"
"github.com/andersfylling/disgord"
"github.com/andersfylling/disgord/std"
"github.com/sirupsen/logrus"
)
var log = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
var noCtx = context.Background()
// checkErr logs errors if not nil, along with a user-specified trace
func checkErr(err error, trace string) {
if err != nil {
log.WithFields(logrus.Fields{
"trace": trace,
}).Error(err)
}
}
// handleMsg is a basic command handler
func handleMsg(s disgord.Session, data *disgord.MessageCreate) {
msg := data.Message
switch msg.Content {
case "ping": // whenever the message written is "ping", the bot replies "pong"
_, err := msg.Reply(noCtx, s, "pong")
checkErr(err, "ping command")
default: // unknown command, bot does nothing.
return
}
}
func main() {
const prefix = "!"
client := disgord.New(disgord.Config{
ProjectName: "MyBot",
BotToken: os.Getenv("DISCORD_TOKEN"),
Logger: log,
Intents: disgord.IntentGuildMessages,
// ! Non-functional due to a current bug, will be fixed.
Presence: &disgord.UpdateStatusPayload{
Game: &disgord.Activity{
Name: "write " + prefix + "ping",
},
},
})
defer client.Gateway().StayConnectedUntilInterrupted()
logFilter, _ := std.NewLogFilter(client)
filter, _ := std.NewMsgFilter(context.Background(), client)
filter.SetPrefix(prefix)
// create a handler and bind it to new message events
// thing about the middlewares are whitelists or passthrough functions.
client.Gateway().WithMiddleware(
filter.NotByBot, // ignore bot messages
filter.HasPrefix, // message must have the given prefix
logFilter.LogMsg, // log command message
filter.StripPrefix, // remove the command prefix from the message
).MessageCreate(handleMsg)
// create a handler and bind it to the bot init
// dummy log print
client.Gateway().BotReady(func() {
log.Info("Bot is ready!")
})
}