forked from jakevoytko/crbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crbot.go
105 lines (84 loc) · 3.22 KB
/
crbot.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
package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis"
"github.com/jakevoytko/crbot/api"
"github.com/jakevoytko/crbot/app"
"github.com/jakevoytko/crbot/config"
"github.com/jakevoytko/crbot/log"
"github.com/jakevoytko/crbot/model"
stringmap "github.com/jakevoytko/go-stringmap"
)
///////////////////////////////////////////////////////////////////////////////
// CRBot is a call-and-response bot. It is taught by users to learn a call and
// response. When it sees the call, it replays the response. Look at the ?help
// documentation for a full list of commands.
//
// Licensed under MIT license, at project root.
///////////////////////////////////////////////////////////////////////////////
func main() {
var filename = flag.String("filename", "secret.json", "Filename of configuration json")
var localhost = flag.String("localhost", "localhost", "Configurable localhost hostname, to allow for Docker's weirdness on OSX")
flag.Parse()
// Parse config.
config, err := config.ParseConfig(*filename)
if err != nil {
log.Fatal("Config parsing failed", err)
}
// Initialize redis.
redisClient := redis.NewClient(&redis.Options{
Addr: *localhost + ":6379",
Password: "", // no password set
DB: 0, // use default DB
})
if _, err := redisClient.Ping().Result(); err != nil {
log.Fatal("Unable to initialize Redis", err)
}
commandMap := stringmap.NewRedisStringMap(redisClient, RedisCommandHash)
karmaMap := stringmap.NewRedisStringMap(redisClient, RedisKarmaHash)
voteMap := stringmap.NewRedisStringMap(redisClient, RedisVoteHash)
gist := api.NewRemoteHastebin()
// Set up Discord API.
discord, err := discordgo.New("Bot " + config.BotToken)
if err != nil {
log.Fatal("Error initializing Discord client library", err)
}
clock := model.NewSystemUTCClock()
timer := model.NewSystemUTCTimer()
// A command channel large enough to process a few commands without needing to
// block.
commandChannel := make(chan *model.Command, 10)
featureRegistry := app.InitializeRegistry(
commandMap, karmaMap, voteMap, gist, config, clock, timer, commandChannel)
// Run any initial load handlers up front.
for _, fn := range featureRegistry.GetInitialLoadFns() {
err := fn(discord)
if err != nil {
log.Info("Error running initial load function", err)
}
}
go app.HandleCommands(featureRegistry, discord, commandChannel)
// Open communications with Discord.
handler := app.GetHandleMessage(commandMap, featureRegistry, commandChannel)
// Wrapper is needed so the discordgo registry recognizes the input types.
wrappedHandler := func(s *discordgo.Session, c *discordgo.MessageCreate) {
handler(s, c)
}
discord.AddHandler(wrappedHandler)
if err := discord.Open(); err != nil {
log.Fatal("Error opening Discord session", err)
}
fmt.Println("CRBot running.")
<-make(chan interface{})
}
///////////////////////////////////////////////////////////////////////////////
// Constants
///////////////////////////////////////////////////////////////////////////////
// NOTE: These cannot change without a migration, since they are mapped to storage.
const (
RedisCommandHash = "crbot-custom-commands"
RedisKarmaHash = "crbot-feature-karma"
RedisVoteHash = "crbot-feature-vote"
)