-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpbot.go
72 lines (61 loc) · 1.92 KB
/
helpbot.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
package helpbot
import (
"context"
"fmt"
"github.com/andersfylling/disgord"
"github.com/google/go-github/v32/github"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
)
type Config struct {
Token string `mapstructure:"token"`
DBPath string `mapstructure:"db-path"`
GHToken string `mapstructure:"gh-token"`
Prefix string `mapstructure:"prefix"`
Guild disgord.Snowflake `mapstructure:"guild"`
StudentRole disgord.Snowflake `mapstructure:"student-role"`
AssistantRole disgord.Snowflake `mapstructure:"assistant-role"`
GitHubOrg string `mapstructure:"gh-org"`
CourseCode string `mapstructure:"course-code"`
CourseYear uint32 `mapstructure:"course-year"`
Autograder bool `mapstructure:"autograder"`
}
type HelpBot struct {
cfg Config
client *disgord.Client
db *gorm.DB
gh *github.Client
ag *Autograder
log *logrus.Logger
// role to command mappings
baseCommands commandMap
studentCommands commandMap
assistantCommands commandMap
}
func (bot *HelpBot) Connect(ctx context.Context) error {
bot.gh = initGithub(ctx, bot.cfg.GHToken)
if bot.client == nil {
return fmt.Errorf("disgord client not initialized for course: %s", bot.cfg.CourseCode)
}
return bot.client.Connect(ctx)
}
func (bot *HelpBot) Disconnect() error {
return bot.client.Disconnect()
}
func New(cfg Config, log *logrus.Logger, ag *Autograder) (bot *HelpBot, err error) {
bot = &HelpBot{cfg: cfg, log: log, ag: ag}
bot.client = disgord.New(disgord.Config{BotToken: cfg.Token})
bot.db, err = OpenDatabase(cfg.DBPath)
if err != nil {
return nil, err
}
bot.initCommands()
bot.initEvents()
bot.client.Ready(func() {
err := bot.client.UpdateStatusString(fmt.Sprintf("DM me %shelp", cfg.Prefix))
if err != nil {
log.Errorln("Failed to update status:", err)
}
})
return bot, nil
}