-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (43 loc) · 1002 Bytes
/
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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/aqyuki/cham/discord"
"github.com/aqyuki/cham/logging"
)
type exitCode int
const (
ExitCodeOK exitCode = iota
ExitCodeError
)
func main() {
ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
code := run(ctx)
defer exit(code)
}
func run(ctx context.Context) exitCode {
logger := logging.DefaultLogger()
bot := discord.NewBot(loadTokenFromEnv(), discord.WithLogger(logger))
if err := bot.Start(); err != nil {
logger.Errorf("failed to start the bot\n\t%v\n", err)
return ExitCodeError
}
<-ctx.Done()
fmt.Printf("received signal to stop the bot\nshutting down...\n")
if err := bot.Stop(); err != nil {
logger.Errorf("failed to stop the bot\n\t%v\n", err)
return ExitCodeError
}
return ExitCodeOK
}
// exit is a wrapper of os.Exit.
func exit[T ~int](code T) {
os.Exit(int(code))
}
func loadTokenFromEnv() string {
return os.Getenv("DISCORD_TOKEN")
}