Skip to content

Commit

Permalink
Add a database engine for guild configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Maartje Eyskens <[email protected]>
  • Loading branch information
meyskens committed Feb 8, 2021
1 parent e9a70a8 commit e7fd624
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 17 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RUN mkdir -p /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot
WORKDIR /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot
COPY ./sounds /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/sounds
COPY ./www /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/www
COPY ./config.json /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/

COPY --from=build /go/src/github.com/itfactory-tm/thomas-bot/thomas /usr/local/bin/

Expand Down
1 change: 1 addition & 0 deletions Dockerfile.alf
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ RUN mkdir -p /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot
WORKDIR /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot

COPY --from=build /go/src/github.com/itfactory-tm/thomas-bot/alf /usr/local/bin/
COPY ./config.json /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/

CMD [ "/usr/local/bin/alf", "clean" ]
1 change: 1 addition & 0 deletions Dockerfile.james
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ RUN mkdir -p /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot
WORKDIR /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot

COPY --from=build /go/src/github.com/itfactory-tm/thomas-bot/james /usr/local/bin/
COPY ./config.json /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/

CMD [ "/usr/local/bin/james", "serve" ]
3 changes: 3 additions & 0 deletions Dockerfile.twitter
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
ARG src
FROM $src

COPY ./config.json /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/
WORKDIR /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/

CMD [ "/usr/local/bin/thomas", "twitter" ]
1 change: 1 addition & 0 deletions Dockerfile.voice
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ COPY ./sounds/clappingmono.wav /go/src/github.com/itfactory-tm/thomas-bot/thomas
COPY ./www /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/www

COPY --from=build /go/src/github.com/itfactory-tm/thomas-bot/john /usr/local/bin/
COPY ./config.json /go/src/github.com/itfactory-tm/thomas-bot/thomas-bot/

CMD [ "/usr/local/bin/john", "voice" ]
27 changes: 23 additions & 4 deletions cmd/thomas/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"syscall"
"time"

"github.com/itfactory-tm/thomas-bot/pkg/db"

"github.com/itfactory-tm/thomas-bot/pkg/commands/shout"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -41,13 +43,18 @@ type serveCmdOptions struct {
HCaptchaSiteKey string `envconfig:"HCAPTCHA_SITE_KEY"`
HCaptchaSiteSecret string `envconfig:"HCAPTCHA_SITE_SECRET"`
BindAddr string `default:":8080" envconfig:"BIND_ADDR"`
MongoDBURL string `envconfig:"MONGODB_URL"`
MongoDBDB string `envconfig:"MONGODB_DB"`
ConfigPath string `default:"./config.json" envconfig:"CONFIG"`
EtcdEndpoints []string `envconfig:"ETCD_ENDPOINTS"`

commandRegex *regexp.Regexp
dg *discordgo.Session
ha discordha.HA
handlers []command.Interface

db db.Database

onMessageCreateHandlers map[string][]func(*discordgo.Session, *discordgo.MessageCreate)
onMessageEditHandlers map[string][]func(*discordgo.Session, *discordgo.MessageUpdate)
onMessageReactionAddHandler []func(*discordgo.Session, *discordgo.MessageReactionAdd)
Expand Down Expand Up @@ -79,8 +86,6 @@ func (s *serveCmdOptions) Validate(cmd *cobra.Command, args []string) error {
return errors.New("No token specified")
}

s.RegisterHandlers()

return nil
}

Expand Down Expand Up @@ -116,7 +121,21 @@ func (s *serveCmdOptions) RunE(cmd *cobra.Command, args []string) error {
return fmt.Errorf("error creating Discord HA: %w", err)
}

// TODO: Register handlers
if s.MongoDBDB != "" {
s.db, err = db.NewMongoDB(s.MongoDBURL, s.MongoDBDB)
if err != nil {
return err
}
} else {
// local fallback
s.db, err = db.NewLocalDB(s.ConfigPath)
if err != nil {
return err
}
}

s.RegisterHandlers()

s.ha.AddHandler(s.onMessage)
s.ha.AddHandler(s.onMessageUpdate)
s.ha.AddHandler(s.onGuildMemberAdd)
Expand All @@ -142,7 +161,7 @@ func (s *serveCmdOptions) RunE(cmd *cobra.Command, args []string) error {
func (s *serveCmdOptions) RegisterHandlers() {
s.handlers = []command.Interface{
hello.NewHelloCommand(),
members.NewMemberCommand(),
members.NewMemberCommand(s.db),
moderation.NewModerationCommands(),
help.NewHelpCommand(),
giphy.NewGiphyCommands(),
Expand Down
22 changes: 22 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"687565213943332875": {
"_JSON_Comment_Hack": "ITF Discord",
"welcomeChannelID": "687588438886842373",
"welcomeText": "Welcome <@{{.User.ID}}> to the **IT Factory Official** Discord server. We will send you a DM in a moment to get you set up!",
"hives": {
"775453791801049119": {

}
}
},
"694621018970390538": {
"_JSON_Comment_Hack": "Trains",
"welcomeText": "Spoor 1, de IC trein met bestemming <@{{.User.ID}}> komt aan, spoor 1. Deze trein stopt in Welkom-Centraal en Geel. Deze trein stopt uitzonderlijk niet in Haat-Noord. De 1ste klas plaatsen zijn overal.",
"welcomeChannelID": "694621018970390541"
},
"808273924600365058": {
"_JSON_Comment_Hack": "Ice Cube",
"welcomeText": "Welkom <@{{.User.ID}}> in de **ICE Cube** Discord server.",
"welcomeChannelID": "808395496556986428"
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ require (
github.com/spf13/viper v1.7.1
go.etcd.io/bbolt v1.3.3 // indirect
go.etcd.io/etcd v3.3.25+incompatible
go.mongodb.org/mongo-driver v1.4.6
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/sys v0.0.0-20201231184435-2d18734c6014 // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc
google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d // indirect
google.golang.org/grpc v1.34.0 // indirect
google.golang.org/protobuf v1.25.0 // indirect
Expand Down
Loading

0 comments on commit e7fd624

Please sign in to comment.