/
main.go
98 lines (83 loc) · 1.86 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/oysterriveroverdrive/hud/bluealliance"
"github.com/sirupsen/logrus"
)
var (
Token string
BAToken string
)
func init() {
logrus.SetLevel(logrus.DebugLevel)
}
func parseFlags() {
flag.StringVar(&Token, "discord-token", "", "Discord Bot Token")
flag.StringVar(&BAToken, "blue-alliance-token", "", "Blue Alliance Token")
flag.Parse()
}
func main() {
fmt.Println("starting hud bot")
logrus.SetLevel(logrus.DebugLevel)
parseFlags()
ts := bluealliance.NewService(&http.Client{}, BAToken)
disc, err := discordgo.New("Bot " + Token)
if err != nil {
log.Fatal(err)
}
disc.Client = &http.Client{Timeout: (20 * time.Second)}
// This creates the structure of the commands hud will respond to.
// A "@hud team 8410" command would be proccessed through the
// TeamCmd - for the "team"
// TeamIDCmd - for the "8410"
hb := &HudBot{
ts: ts,
SubCmds: []Cmd{
&TeamCmd{
SubCmds: []Cmd{
&TeamIDCmd{},
},
},
&DistrictCmd{
SubCmds: []Cmd{
&DistrictIDCmd{
SubCmds: []Cmd{
&DistrictIDTeamsCmd{},
},
},
},
},
&RulesCmd{
SubCmds: []Cmd{
&RulesListCmd{},
&RulesNumberCmd{},
&RulesSearchCmd{},
},
},
},
}
disc.AddHandler(hb.Handle)
// Let HudBot read messages.
disc.Identify.Intents = discordgo.IntentsGuildMessages
// Open a websocket connection to Discord and begin listening.
err = disc.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
disc.Close()
}