From 0f40d84b68d685a428c90111270b9d5ce060a463 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Mon, 20 Jul 2020 05:11:22 +0200 Subject: [PATCH 1/2] Check for ban on register (#9) Check for ban on register Signed-off-by: Etzelia Reviewed-on: https://git.etztech.xyz/Etzelia/sedbot/pulls/9 --- 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" From 37c6afa8c346b87d82a19f9b5f0705b56e1d17af Mon Sep 17 00:00:00 2001 From: Etzelia Date: Thu, 30 Jul 2020 04:12:33 +0200 Subject: [PATCH 2/2] Update message role emoji --- sedbot.example.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sedbot.example.toml b/sedbot.example.toml index 37d4bbf..0b7d594 100644 --- a/sedbot.example.toml +++ b/sedbot.example.toml @@ -58,4 +58,4 @@ help = "Get the invite link" channel_id = "0" message_id = "0" role_id = "0" -emoji = "thumbsup" \ No newline at end of file +emoji = "👍" \ No newline at end of file