From f9fdd496bf980dbfc467998e18eb1a44fea1432d Mon Sep 17 00:00:00 2001 From: Etzelia Date: Sun, 19 Jul 2020 22:04:45 -0500 Subject: [PATCH] Check for ban on register Signed-off-by: Etzelia --- config/config.go | 1 + discord/register.go | 57 +++++++++++++++++++++++++++++++++++++++++---- sedbot.example.toml | 3 +++ 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 8f45f89..3a4a1e5 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,7 @@ type Config struct { MCMToken string `toml:"mcm_token"` MCMURL string `toml:"mcm_url"` DBPath string `toml:"db_path"` + MCPath string `toml:"mc_path"` StaffRoles []string `toml:"staff_roles"` Echoes []Echo `toml:"echoes"` diff --git a/discord/register.go b/discord/register.go index 9f5ff63..6d4c7a4 100644 --- a/discord/register.go +++ b/discord/register.go @@ -1,13 +1,20 @@ package discord import ( + "encoding/json" "fmt" + "io/ioutil" + "path/filepath" "strings" + "go.etztech.xyz/sedbot/config" + "go.etztech.xyz/go-mcm" "go.etztech.xyz/go-mcm/model/django" ) +const bannedPlayersFile = "banned-players.json" + func init() { commands["register"] = command{ validate: func(cmd commandInit) bool { @@ -19,17 +26,19 @@ func init() { return "You must give this command your application username", nil } + username := args[1] + sendTyping(cmd.session, cmd.message.ChannelID) manager := mcm.NewMCM(cmd.config.MCMToken, cmd.config.MCMURL) models := manager.NewModel() - players, err := models.Player(models.NewDjangoBuilder().IExact(django.PlayerUsername, args[1])) + players, err := models.Player(models.NewDjangoBuilder().IExact(django.PlayerUsername, username)) if err != nil { return "", err } - apps, err := models.Application(models.NewDjangoBuilder().IExact(django.ApplicationUsername, args[1])) + apps, err := models.Application(models.NewDjangoBuilder().IExact(django.ApplicationUsername, username)) if err != nil { return "", err } @@ -37,9 +46,20 @@ func init() { var nickname string var accepted *bool if len(players) > 0 { - nickname = players[0].Username + player := players[0] + + // Check for a ban + ban, err := findBan(cmd.config, player.UUID) + if err != nil { + return "", err + } + if ban != nil { + return fmt.Sprintf("You are currently banned: **%s**", ban.Reason), nil + } + + nickname = player.Username if len(apps) == 0 { - apps, err = models.Application(models.NewDjangoBuilder().Eq(django.ApplicationID, players[0].ApplicationID)) + apps, err = models.Application(models.NewDjangoBuilder().Eq(django.ApplicationID, player.ApplicationID)) if len(apps) == 0 { return "Something went wrong, please contact staff", nil } @@ -74,3 +94,32 @@ func init() { help: "Register yourself with the Discord", } } + +type Ban struct { + UUID string `json:"uuid"` + Name string `json:"name"` + Created string `json:"created"` + Source string `json:"source"` + Expires string `json:"expires"` + Reason string `json:"reason"` +} + +func findBan(cfg *config.Config, uuid string) (*Ban, error) { + banData, err := ioutil.ReadFile(filepath.Join(cfg.MCPath, bannedPlayersFile)) + if err != nil { + return nil, err + } + + var bans []*Ban + if err := json.Unmarshal(banData, &bans); err != nil { + return nil, err + } + + for _, ban := range bans { + if uuid == ban.UUID { + return ban, nil + } + } + + return nil, nil +} diff --git a/sedbot.example.toml b/sedbot.example.toml index da547da..37d4bbf 100644 --- a/sedbot.example.toml +++ b/sedbot.example.toml @@ -13,6 +13,9 @@ mcm_url = "" # db_path is the path to the database (default is next to binary) db_path = "sedbot.db" +# mc_path is the path to the root directory of the minecraft server +mc_path = "/home/minecraft/server/" + # fired_role is to check how many time Carolyn has been fired fired_role = "0"