-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (58 loc) · 1.69 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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/jonathroth/temp-chat/bot"
"github.com/jonathroth/temp-chat/state"
)
var (
discordToken = os.Getenv("DISCORD_TOKEN")
postgresAddr = os.Getenv("DATABASE_URL")
)
func main() {
if discordToken == "" {
log.Fatalf("A discord token is required to run")
}
if postgresAddr == "" {
log.Fatalf("A PostgreSQL database URL is required to run")
}
session, err := discordgo.New("Bot " + discordToken)
if err != nil {
log.Fatalf("Failed initializing discord connection: %v", err)
}
serversProvider, err := state.NewPostgresServersProvider(postgresAddr)
if err != nil {
log.Fatalf("Failed connecting to the database: %v", err)
}
store, err := state.NewSyncServerStore(serversProvider)
if err != nil {
log.Fatalf("Failed initializing server store: %v", err)
}
tempChannelBot, err := bot.NewTempChannelBot(session, store)
if err != nil {
log.Fatalf("Failed initializing bot: %v", err)
}
waitForBot(session, tempChannelBot)
err = session.Close()
if err != nil {
log.Fatalf("Bot ClosE() failed: %v", err)
}
}
func waitForBot(session *discordgo.Session, tempChannelBot *bot.TempChannelBot) {
defer tempChannelBot.CleanChannels()
session.AddHandler(tempChannelBot.MessageCreate)
session.AddHandler(tempChannelBot.ChannelDelete)
session.AddHandler(tempChannelBot.VoiceStatusUpdate)
err := session.Open()
if err != nil {
log.Fatalf("Failed connecting to discord: %v", err)
}
log.Println("Bot is now running...")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
exitSignal := <-sc
log.Printf("Got signal %v, exiting", exitSignal.String())
}