-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
48 lines (40 loc) · 1.05 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
package main
import (
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
)
func botReady(_ *discordgo.Session, m *discordgo.Ready) {
fmt.Println("Logged in as:", m.User)
}
func runBot(discordAuthToken string) chan<- struct{} {
dg, err := discordgo.New("Bot " + discordAuthToken)
if err != nil {
fmt.Println("error creating Discord session,", err)
return nil
}
dg.AddHandler(botReady)
dg.Identify.Intents = discordgo.IntentsDirectMessages
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return nil
}
go func() {
for mq := range msgqueue {
// TODO: Get channel ID from forms table in DB.
// TODO: Send embeds, rather than JSON :/
// TODO: Warn about jumping to links sent through the message.
b, _ := json.MarshalIndent(mq.Obj, "", " ")
dg.ChannelMessageSend(testChannelID, fmt.Sprintf("```json\n%s\n```", string(b)))
}
}()
shutdownBot := make(chan struct{})
go func() {
<-shutdownBot
if err := dg.Close(); err != nil {
fmt.Println("Err shutdownBot:", err)
}
}()
return shutdownBot
}