-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (54 loc) · 1.12 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/acomagu/musicbot/bot"
"github.com/acomagu/musicbot/discord"
"github.com/acomagu/musicbot/router"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"
)
var token = os.Getenv("DISCORD_TOKEN")
var port = os.Getenv("PORT")
func main() {
if port == "" {
port = "80"
}
http.HandleFunc("/health", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
go func() {
if err := http.ListenAndServe(":"+port, nil); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
if token == "" {
return errors.New("No token provided. Set DISCORD_TOKEN.")
}
session, err := discordgo.New("Bot " + token)
if err != nil {
return err
}
joiner := discord.NewVoiceChannelJoiner(session)
r := &router.Router{
Session: session,
}
bot, err := bot.New(discord.NewSession(session), joiner)
if err != nil {
return err
}
r.Handle(bot)
if err := session.Open(); err != nil {
return err
}
defer session.Close()
bot.Wait()
return nil
}