-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwilhelm.go
89 lines (79 loc) · 1.97 KB
/
wilhelm.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
package main
import (
"fmt"
"log"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
dgo "github.com/bwmarrin/discordgo"
)
type messageHandler func(*dgo.Session, *dgo.MessageCreate, []string) error
var (
token = os.Getenv("TOKEN")
mediaRoot *url.URL
commands = map[string]messageHandler{
"consent": cmdConsent,
"witness": cmdWitness,
"adjourn": cmdAdjourn,
"check": cmdCheckConsent,
}
)
func init() {
if token == "" {
log.Fatalln("TOKEN environment variable not specified")
}
if mr := os.Getenv("MEDIA_ROOT"); mr == "" {
log.Fatalln("MEDIA_ROOT environment variable not specified")
} else {
var err error
mediaRoot, err = url.Parse(mr)
if err != nil {
log.Fatalln("failed to parse mediaRoot as URL: ", err)
}
switch s := mediaRoot.Scheme; s {
case "file":
default:
log.Fatalf("scheme '%s' not supported for MEDIA_ROOT", s)
}
}
}
func onMessage(s *dgo.Session, m *dgo.MessageCreate) {
if prefix := dbBotPrefix(m.GuildID); strings.HasPrefix(m.Content, prefix) {
rawcmd := m.Content[len(prefix):]
fields := strings.Fields(rawcmd)
if cmdFunc, ok := commands[fields[0]]; ok {
if err := cmdFunc(s, m, fields[1:]); err != nil {
s.ChannelMessageSendReply(m.ChannelID, err.Error(), m.Reference())
}
} else {
log.Printf("Invalid command '%s' from %s\n", rawcmd, m.Author)
s.ChannelMessageSendReply(
m.ChannelID,
fmt.Sprintf("Command not recognized: '%s'", fields[0]),
m.Reference(),
)
}
}
}
func main() {
defer db.Close()
discord, err := dgo.New("Bot " + token)
if err != nil {
log.Fatalln(err)
}
discord.StateEnabled = true
discord.Identify.Intents = dgo.IntentsGuildVoiceStates |
dgo.IntentsGuildMessages
if err := discord.Open(); err != nil {
log.Panicln(err)
}
defer discord.Close()
stopHandling := discord.AddHandler(onMessage)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
log.Println(<-quit)
stopHandling()
listening <- true // Wait for voice to cleanup
}