Skip to content

Commit

Permalink
Add !link command
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Mar 24, 2024
1 parent 169081f commit 0b6a5cc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
5 changes: 2 additions & 3 deletions cmd/tf2bdd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func run() error {
appCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()

listenAddr := fmt.Sprintf("%s:%d", config.ListenHost, config.ListenPort)
httpServer := tf2bdd.CreateHTTPServer(tf2bdd.CreateRouter(database, config), listenAddr)
httpServer := tf2bdd.CreateHTTPServer(tf2bdd.CreateRouter(database, config), config.ListenAddr())

discordBot, errBot := tf2bdd.NewBot(config.DiscordBotToken)
if errBot != nil {
Expand All @@ -66,7 +65,7 @@ func run() error {

slog.Info("Add bot", slog.String("link", tf2bdd.DiscordAddURL(config.DiscordClientID)))
slog.Info("Make sure you enable \"Message Content Intent\" on your discord config under the Bot settings via discord website")
slog.Info("Listening on", slog.String("addr", fmt.Sprintf("http://%s", listenAddr)))
slog.Info("Listening on", slog.String("addr", config.ListenAddr()))

go func() {
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
Expand Down
16 changes: 16 additions & 0 deletions tf2bdd/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func messageCreate(ctx context.Context, database *sql.DB, config Config) func(*d
"!steamid": 2,
"!import": 1,
"!count": 1,
"!link": 1,
"!addproof": 3,
}

Expand Down Expand Up @@ -366,6 +367,8 @@ func messageCreate(ctx context.Context, database *sql.DB, config Config) func(*d
switch strings.ToLower(msg[0]) {
case "!del":
response, cmdErr = deleteEntry(ctx, database, sid)
case "!link":
response, cmdErr = getLink(config)
case "!check":
response, cmdErr = checkEntry(ctx, database, sid)
case "!addproof":
Expand Down Expand Up @@ -396,7 +399,20 @@ func messageCreate(ctx context.Context, database *sql.DB, config Config) func(*d
}
}

func getLink(config Config) (string, error) {
link, errLink := config.UpdateURL()
if errLink != nil {
return "", errLink
}

return fmt.Sprintf("<%s>", link), nil
}

func addProof(ctx context.Context, database *sql.DB, sid steamid.SteamID, proof string) (string, error) {
if proof == "" {
return "", errors.New("empty proof value")
}

player, errPlayer := getPlayer(ctx, database, sid)
if errPlayer != nil {
return "", errPlayer
Expand Down
25 changes: 25 additions & 0 deletions tf2bdd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package tf2bdd
import (
"errors"
"fmt"
"net"
"net/url"
"strings"

"github.com/mitchellh/go-homedir"
Expand All @@ -26,6 +28,29 @@ type Config struct {
ExportedAttrs []string `mapstructure:"exported_attrs"`
}

func (config Config) ListenAddr() string {
return net.JoinHostPort(config.ListenHost, fmt.Sprintf("%d", config.ListenPort))
}

func (config Config) UpdateURL() (string, error) {
extUrl := config.ExternalURL

Check warning on line 36 in tf2bdd/config.go

View workflow job for this annotation

GitHub Actions / lint-golangci

var-naming: var extUrl should be extURL (revive)
if extUrl == "" {
host := config.ListenHost
if host == "" {
host = "localhost"
}
extUrl = fmt.Sprintf("http://%s", net.JoinHostPort(host, fmt.Sprintf("%d", config.ListenPort)))
}

parsed, errParse := url.Parse(extUrl)
if errParse != nil {
return "", errParse
}
parsed.Path = "/v1/steamids"

return parsed.String(), nil
}

func ReadConfig() (Config, error) {
if home, errHomeDir := homedir.Dir(); errHomeDir != nil {
viper.AddConfigPath(home)
Expand Down
9 changes: 3 additions & 6 deletions tf2bdd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"encoding/json"
"fmt"
"log/slog"
"net"
"net/http"
"slices"
"strings"
"time"

"github.com/leighmacdonald/steamid/v4/steamid"
Expand Down Expand Up @@ -45,10 +43,9 @@ type Player struct {
}

func handleGetSteamIDs(database *sql.DB, config Config) http.HandlerFunc {
hostPort := net.JoinHostPort(config.ListenHost, fmt.Sprintf("%d", config.ListenPort))
updateURL := fmt.Sprintf("http://%s/v1/steamids", hostPort)
if config.ExternalURL != "" {
updateURL = fmt.Sprintf("%s/v1/steamids", strings.TrimSuffix(config.ExternalURL, "/"))
updateURL, errUpdateURL := config.UpdateURL()
if errUpdateURL != nil {
panic(fmt.Errorf("failed to create valid update url: %w", errUpdateURL))
}

return func(writer http.ResponseWriter, request *http.Request) {
Expand Down

0 comments on commit 0b6a5cc

Please sign in to comment.