-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunison.go
106 lines (89 loc) · 2.62 KB
/
unison.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
package unison
import (
"errors"
"os"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/s1kx/unison/constant"
"github.com/s1kx/unison/state"
"github.com/sirupsen/logrus"
)
var logFormatter = logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
}
// Run start the bot. Connect to discord, setup commands, hooks and services.
func Run(settings *Config) error {
// Configure logger format.
logrus.SetFormatter(&logFormatter)
// TODO: Validate commands
// three steps are done before setting up a connection.
// 1. Make sure the discord token exists.
// 2. Set a prefered way of triggering commands.
// This must be done after establishin a discord socket. See bot.onReady()
// 3. Decide the bot state.
// 1. Make sure the discord token exists.
//
token := settings.Token
// if it was not specified in the Settings struct, check the environment variable
if token == "" {
token = os.Getenv(constant.EnvUnisonDiscordToken)
// if the env var was empty as well, crash the bot as this is required.
if token == "" {
return errors.New("Missing env var " + constant.EnvUnisonDiscordToken + ". This is required. Specify in either Settings struct or env var.")
}
logrus.Info("Using bot token from environment variable.")
}
// discordgo requires "Bot " prefix for Bot applications
if !strings.HasPrefix(token, constant.DiscordGoBotTokenPrefix) {
token = constant.DiscordGoBotTokenPrefix + token
}
// Initialize discord client
ds, err := discordgo.New(token)
if err != nil {
return err
}
// 2. Set a prefered way of triggering commands
//
envPrefix := os.Getenv(constant.EnvUnisonCommandPrefix)
if envPrefix != "" {
settings.CommandPrefix = append(settings.CommandPrefix, envPrefix)
}
var cmdPrefixes string
for _, prefix := range settings.CommandPrefix {
cmdPrefixes += "; " + prefix
}
if !settings.DisableMentionTrigger {
cmdPrefixes += "; And by @mention."
}
logrus.Info("Commands are triggered by" + cmdPrefixes)
// 3. Decide the bot state.
//
uState := settings.BotState
// check if valid state
if uState == state.MissingState {
// chjeck environment variable
uStateStr := os.Getenv(constant.EnvUnisonState)
if uStateStr == "" {
uState = state.Normal // uint8(1)
} else {
i, e := strconv.ParseInt(uStateStr, 10, 16)
if e != nil {
return e
}
uState = state.Type(i)
// fallback
if uState == state.MissingState {
uState = state.Normal
}
}
}
settings.BotState = uState
// Initialize and start bot
bot, err := newBot(settings, ds)
if err != nil {
return err
}
return bot.Run() // returns nil on successfull shutdown
}