-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
60 lines (44 loc) · 1016 Bytes
/
run.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
package snart
import (
"fmt"
"log"
"os"
"os/signal"
)
// sigError occurs when a process signal kills the bot.
type sigError struct {
Signal os.Signal
}
// Error implements error.
func (s sigError) Error() string {
return "killed by signal " + s.Signal.String()
}
// Run performs the Bot's startup functions, and waits for an error.
func (b *Bot) Run() error {
log.Println("running bot")
sigs := make(chan os.Signal, 1)
go func() {
b.Errs <- sigError{
Signal: <-sigs,
}
}()
signal.Notify(sigs, os.Interrupt)
log.Println("listening for sigs")
b.State.AddHandler(b.Route.Handle)
log.Println("router handler added")
b.State.Gateway.Identifier.Intents = b.Intents
log.Println("intents injected")
err := b.State.Open()
if err != nil {
return fmt.Errorf("open state: %w", err)
}
log.Println("state opened")
defer b.State.Close()
go b.CycleGamers()
log.Println("gamers cycling")
err = <-b.Errs
if err != nil {
return fmt.Errorf("from errs chan: %w", err)
}
return nil
}