Check for ban on register (#9)

Check for ban on register

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Etzelia/sedbot/pulls/9
master^2
Etzelia 2020-07-20 05:11:22 +02:00
parent f204ee6e37
commit 0f40d84b68
3 changed files with 57 additions and 4 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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"