forked from Minecraft/canopeas
143 lines
3.8 KiB
Go
143 lines
3.8 KiB
Go
package discord
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.jojodev.com/Minecraft/go-mcm"
|
|
"git.jojodev.com/Minecraft/go-mcm/model/django"
|
|
|
|
"git.jojodev.com/Minecraft/canopeas/config"
|
|
)
|
|
|
|
const bannedPlayersFile = "banned-players.json"
|
|
|
|
func init() {
|
|
commands = append(commands, &command{
|
|
name: "register",
|
|
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 fmt.Sprintf("Please join the server at `%s` and then re-try this command", cmd.config.Register.URL), 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, check for dupe user
|
|
guild, err := cmd.session.State.Guild(cmd.message.GuildID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for _, member := range guild.Members {
|
|
nick := member.Nick
|
|
if nick == "" {
|
|
}
|
|
nick = member.User.Username
|
|
if strings.EqualFold(nickname, nick) && cmd.message.Author.ID != member.User.ID {
|
|
return "A member with that name already exists in this Discord. Please contact staff.", nil
|
|
}
|
|
}
|
|
|
|
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.Register.Role); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Don't return feedback because this goes in a different channel
|
|
sendMessage(cmd.session, cmd.config.Register.WelcomeChannel, 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
|
|
}
|