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 { return len(cmd.message.Member.Roles) == 0 }, run: func(cmd commandInit) (string, error) { args := strings.Fields(cmd.message.Content) if len(args) < 2 { return "You must give this command your application username", nil } username := args[1] sendTyping(cmd.session, cmd.message.ChannelID) manager := mcm.NewMCM(cmd.config.MCM.Token, cmd.config.MCM.URL) models := manager.NewModel() players, err := models.Player(models.NewDjangoBuilder().IExact(django.PlayerUsername, username)) if err != nil { return "", err } apps, err := models.Application(models.NewDjangoBuilder().IExact(django.ApplicationUsername, username)) if err != nil { return "", err } var nickname string if len(players) > 0 { 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().Exact(django.ApplicationID, player.ApplicationID)) if err != nil { return "Something went wrong, please contact staff", nil } if len(apps) == 0 { return "No application found for that player", nil } } } else if len(apps) > 0 { if apps[0].Accepted != nil && *apps[0].Accepted { return "Please join the server and then re-try this command", nil } } else { return "No player or applications found for that username", nil } accepted := apps[0].Accepted if accepted == nil { return "Your application is still being reviewed, hang tight", nil } else if !*accepted { return "Your application was denied, good luck finding a new server", nil } // Accepted if err := cmd.session.GuildMemberNickname(cmd.message.GuildID, cmd.message.Author.ID, nickname); err != nil { return "", err } if err := cmd.session.GuildMemberRoleAdd(cmd.message.GuildID, cmd.message.Author.ID, cmd.config.RegisterRole); err != nil { return "", err } // Don't return feedback because this goes in a different channel sendMessage(cmd.session, cmd.config.RegisteredChannel, fmt.Sprintf("Welcome, **%s**!", cmd.message.Author.Mention()), false) return "", nil }, 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 }