/
main.go
109 lines (88 loc) · 2.44 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
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"io"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/Dids/hackchat/discord"
"github.com/Dids/hackchat/hackmud"
_ "github.com/joho/godotenv/autoload"
)
var (
// Internal version of the application
version string = "dev"
// Hackmud API client
Hackmud *hackmud.Client
// Discord client
Discord *discord.Client
)
func main() {
// Handle arguments
args := os.Args[1:]
if len(args) > 0 {
// Handle "hackchat version" command
if args[0] == "version" {
fmt.Println("hackchat " + version)
os.Exit(0)
} else {
if len(args) == 1 {
fmt.Println("Invalid argument:", args[0])
} else {
fmt.Println("Invalid arguments:", args)
}
os.Exit(-1)
}
}
log.Println("--- hackchat "+version, "---")
log.Println("Starting up")
// TODO: Does logrus close the file or do we need to close it ourselves?
// Log to both console and file
logFile, logFileErr := os.OpenFile("hackchat.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if logFileErr != nil {
panic(logFileErr)
}
defer logFile.Close()
logWriter := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logWriter)
// Run shutdown logic no matter what
defer shutdown()
// Create a new API client
Hackmud = hackmud.NewClient(os.Getenv("HACKMUD_CHAT_API_TOKEN"))
// Enable request debugging
Hackmud.Debug = true
Discord = discord.NewClient(os.Getenv("DISCORD_API_TOKEN"))
log.Info("Starting Discord client..")
if err := Discord.Start(); err != nil {
panic(err)
}
// FIXME: We need to have an event handler/delegate
// which controls the data flow between hackmud
// and Discord, otherwise this won't really work!
// TODO: Implement all other API endpoints as well
// Attempt to get the account data
// accountData, err := Hackmud.GetAccountData()
// if err != nil {
// panic(err)
// }
// log.Println("Got account data:", account)
// TODO: This works, but should be formatted better etc.
// if err := Discord.Send("```json\n" + accountData.ToJSON() + "\n```"); err != nil {
// log.Error(err)
// }
// Wait for CTRL-C
log.Info("Hackchat bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}
func shutdown() {
log.Info("Preparing to shutdown..")
// Stop Discord client
log.Info("Stopping Discord client..")
if err := Discord.Stop(); err != nil {
panic(err)
}
log.Info("Exiting..")
}