Skip to content

Commit

Permalink
feat: repair disocrd bot
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Jul 5, 2020
1 parent 54c1fed commit 7e68c39
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 64 deletions.
2 changes: 2 additions & 0 deletions cmd/sgtm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func app(args []string) error {
rootFlags.DurationVar(&svcOpts.ServerRequestTimeout, "server-request-timeout", svcOpts.ServerRequestTimeout, "server request timeout")
rootFlags.DurationVar(&svcOpts.ServerShutdownTimeout, "server-shutdown-timeout", svcOpts.ServerShutdownTimeout, "server shutdown timeout")
rootFlags.BoolVar(&svcOpts.ServerWithPprof, "server-with-pprof", svcOpts.ServerWithPprof, "enable pprof on HTTP server")
rootFlags.StringVar(&svcOpts.DiscordClientID, "discord-client-id", svcOpts.DiscordClientID, "discord client ID (oauth)")
rootFlags.StringVar(&svcOpts.DiscordClientSecret, "discord-client-secret", svcOpts.DiscordClientSecret, "discord client secret (oauth)")

root := &ffcli.Command{
FlagSet: rootFlags,
Expand Down
8 changes: 5 additions & 3 deletions config.txt.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
## Discord
#enable-discord true
#discord-token REPLACE_ME
#discord-admin-channel REPLACE_ME
discord-token REPLACE_ME
discord-admin-channel REPLACE_ME
discord-client-id REPLACE_ME
discord-client-secret REPLACE_ME

## Server
enable-server true
#enable-server true
server-bind 0.0.0.0:7676
11 changes: 11 additions & 0 deletions pkg/sgtm/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sgtm

import (
"fmt"
"net/http"
)

func httpAuthCallback(w http.ResponseWriter, r *http.Request) {
code := r.URL.Query().Get("code")
fmt.Fprintf(w, "yo%s !\n", code)
}
60 changes: 38 additions & 22 deletions pkg/sgtm/driver_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,52 @@ func (svc *Service) StartDiscord() error {
if err != nil {
return err
}
msg := "**Hello World!**"
if svc.opts.DevMode {
msg += " (dev)"
}
_, err = dg.ChannelMessageSend(svc.opts.DiscordAdminChannel, msg)
if err != nil {
return err
}
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return

// send intro msg
{
msg := "**Hello World!**"
if svc.opts.DevMode {
msg += " (dev)"
}
log.Println(godev.JSON(m))
_, err := s.ChannelMessageSend(m.ChannelID, ">>> "+m.Content)
svc.logger.Debug("send intro msg", zap.String("msg", msg), zap.String("channel", svc.opts.DiscordAdminChannel))
_, err = dg.ChannelMessageSend(svc.opts.DiscordAdminChannel, msg)
if err != nil {
svc.logger.Error("discord.ChannelMessageSend", zap.Error(err))
svc.logger.Warn("failed to send intro msg", zap.Error(err))
}
})
err = dg.Open()
if err != nil {
return err
}
svc.discord.session = dg

<-svc.ctx.Done()
// handlers
{
dg.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if m.Author.Bot {
return
}
log.Println(godev.JSON(m))
_, err := s.ChannelMessageSend(m.ChannelID, ">>> "+m.Content)
if err != nil {
svc.logger.Error("discord.ChannelMessageSend", zap.Error(err))
}
})
}

// start
{
err = dg.Open()
if err != nil {
return err
}
svc.discord.session = dg

<-svc.ctx.Done()
}
return nil
}

func (svc *Service) CloseDiscord(error) {
svc.logger.Debug("closing discord", zap.Bool("was-started", svc.discord.session != nil))
func (svc *Service) CloseDiscord(err error) {
svc.logger.Debug("closing discord", zap.Bool("was-started", svc.discord.session != nil), zap.Error(err))
if svc.discord.session != nil {
svc.discord.session.Close()
svc.logger.Debug("discord closed")
Expand Down
13 changes: 9 additions & 4 deletions pkg/sgtm/driver_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (svc *Service) StartServer() error {
return gr.Run()
}

func (svc *Service) CloseServer(error) {
svc.logger.Debug("closing server", zap.Bool("was-started", svc.server.listener != nil))
func (svc *Service) CloseServer(err error) {
svc.logger.Debug("closing server", zap.Bool("was-started", svc.server.listener != nil), zap.Error(err))
if svc.server.listener != nil {
svc.server.listener.Close()
}
Expand Down Expand Up @@ -197,10 +197,15 @@ func (svc *Service) httpServer() (*http.Server, error) {
})
}

// auth
{
r.Get("/auth/callback", httpAuthCallback)
}

// static files & 404
{
fs := http.FileServer(box)
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, ".tmpl.html") { // hide sensitive files
r.URL.Path = "/404.html"
}
Expand All @@ -211,7 +216,7 @@ func (svc *Service) httpServer() (*http.Server, error) {
}
}
fs.ServeHTTP(w, r)
}))
})
}

// pprof
Expand Down
2 changes: 2 additions & 0 deletions pkg/sgtm/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Opts struct {
EnableDiscord bool
DiscordToken string
DiscordAdminChannel string
DiscordClientID string
DiscordClientSecret string

/// DB
DBPath string
Expand Down
2 changes: 1 addition & 1 deletion pkg/sgtm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Service struct {

func New(db *gorm.DB, opts Opts) Service {
opts.applyDefaults()
fmt.Fprintln(os.Stderr, banner.Inline("moul-bot"))
fmt.Fprintln(os.Stderr, banner.Inline("sgtm"))
ctx, cancel := context.WithCancel(opts.Context)
svc := Service{
db: db,
Expand Down
24 changes: 0 additions & 24 deletions tool/docker-protoc/Dockerfile

This file was deleted.

10 changes: 0 additions & 10 deletions tool/docker-protoc/Makefile

This file was deleted.

0 comments on commit 7e68c39

Please sign in to comment.