This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
/
main.go
71 lines (61 loc) · 1.85 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
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/camd67/moebot/moebot_bot/bot"
"github.com/camd67/moebot/moebot_bot/util"
"github.com/camd67/moebot/moebot_bot/util/db"
"github.com/camd67/moebot/moebot_bot/util/reddit"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
// read in configuration information
configPath := os.Getenv("MOEBOT_CONFIG_PATH")
configFile, err := ioutil.ReadFile(configPath)
if err != nil {
log.Fatal("Error reading config from file. Path: " + configPath)
}
configText := util.NormalizeNewlines(string(configFile))
for _, line := range strings.Split(configText, "\n") {
splitLine := strings.Split(line, "~")
if len(splitLine) == 0 || splitLine[0] == "" {
continue
}
bot.Config[splitLine[0]] = splitLine[1]
}
bot.ComPrefix = bot.Config["prefix"]
// setup discord with that information
discord, err := discordgo.New("Bot " + bot.Config["token"])
if err != nil {
log.Fatal("Error starting discord...", err)
}
redditHandle, err := reddit.NewHandle(bot.Config["redditClientID"], bot.Config["redditClientSecret"], bot.Config["redditUserName"],
bot.Config["redditPassword"])
if err != nil {
log.Println("Error getting reddit session, related functionality won't work", err)
}
log.Println("Sucessfully connected to reddit...")
bot.SetupMoebot(discord, redditHandle)
defer db.DisconnectAll()
// start up a connection with discord
err = discord.Open()
if err != nil {
log.Fatal("Error starting discord: ", err)
return
}
defer discord.Close()
// halt until we get a SIGTERM or similar
log.Println("Moebot's up and running! Press CTRL + C to exit...")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
fmt.Println("Exited moebot! Seeya later!")
}