2020-06-09 13:04:44 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-07-20 03:11:22 +00:00
|
|
|
"encoding/json"
|
2020-06-09 13:04:44 +00:00
|
|
|
"fmt"
|
2020-07-20 03:11:22 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
2020-06-09 13:04:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-07-20 03:11:22 +00:00
|
|
|
"go.etztech.xyz/sedbot/config"
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
"go.etztech.xyz/go-mcm"
|
|
|
|
"go.etztech.xyz/go-mcm/model/django"
|
|
|
|
)
|
|
|
|
|
2020-07-20 03:11:22 +00:00
|
|
|
const bannedPlayersFile = "banned-players.json"
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
func init() {
|
|
|
|
commands["register"] = command{
|
|
|
|
validate: func(cmd commandInit) bool {
|
|
|
|
return len(cmd.message.Member.Roles) == 0
|
|
|
|
},
|
2020-06-12 05:17:58 +00:00
|
|
|
run: func(cmd commandInit) (string, error) {
|
2020-06-09 13:04:44 +00:00
|
|
|
args := strings.Fields(cmd.message.Content)
|
|
|
|
if len(args) < 2 {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "You must give this command your application username", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 03:11:22 +00:00
|
|
|
username := args[1]
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
sendTyping(cmd.session, cmd.message.ChannelID)
|
|
|
|
|
2020-08-06 20:07:32 +00:00
|
|
|
manager := mcm.NewMCM(cmd.config.MCM.Token, cmd.config.MCM.URL)
|
2020-06-09 13:04:44 +00:00
|
|
|
models := manager.NewModel()
|
|
|
|
|
2020-07-20 03:11:22 +00:00
|
|
|
players, err := models.Player(models.NewDjangoBuilder().IExact(django.PlayerUsername, username))
|
2020-06-11 06:00:29 +00:00
|
|
|
if err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", err
|
2020-06-11 06:00:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 03:11:22 +00:00
|
|
|
apps, err := models.Application(models.NewDjangoBuilder().IExact(django.ApplicationUsername, username))
|
2020-06-09 13:04:44 +00:00
|
|
|
if err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", err
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 06:00:29 +00:00
|
|
|
var nickname string
|
|
|
|
if len(players) > 0 {
|
2020-07-20 03:11:22 +00:00
|
|
|
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
|
2020-06-11 06:05:33 +00:00
|
|
|
if len(apps) == 0 {
|
2020-07-20 03:11:22 +00:00
|
|
|
apps, err = models.Application(models.NewDjangoBuilder().Eq(django.ApplicationID, player.ApplicationID))
|
2020-09-07 22:47:01 +00:00
|
|
|
if err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "Something went wrong, please contact staff", nil
|
2020-06-11 06:00:29 +00:00
|
|
|
}
|
2020-09-07 22:47:01 +00:00
|
|
|
if len(apps) == 0 {
|
|
|
|
return "No application found for that player", nil
|
|
|
|
}
|
2020-06-11 06:00:29 +00:00
|
|
|
}
|
|
|
|
} else if len(apps) > 0 {
|
2020-06-11 06:16:04 +00:00
|
|
|
if apps[0].Accepted != nil && *apps[0].Accepted {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "Please join the server and then re-try this command", nil
|
2020-06-11 06:00:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "No player or applications found for that username", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
2020-09-07 22:47:01 +00:00
|
|
|
accepted := apps[0].Accepted
|
2020-06-09 13:04:44 +00:00
|
|
|
|
2020-06-11 06:16:04 +00:00
|
|
|
if accepted == nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "Your application is still being reviewed, hang tight", nil
|
2020-06-11 06:16:04 +00:00
|
|
|
} else if !*accepted {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "Your application was denied, good luck finding a new server", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Accepted
|
2020-06-11 06:00:29 +00:00
|
|
|
if err := cmd.session.GuildMemberNickname(cmd.message.GuildID, cmd.message.Author.ID, nickname); err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", err
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
if err := cmd.session.GuildMemberRoleAdd(cmd.message.GuildID, cmd.message.Author.ID, cmd.config.RegisterRole); err != nil {
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", err
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
2020-06-12 05:17:58 +00:00
|
|
|
|
|
|
|
// Don't return feedback because this goes in a different channel
|
2020-07-02 20:43:43 +00:00
|
|
|
sendMessage(cmd.session, cmd.config.RegisteredChannel, fmt.Sprintf("Welcome, **%s**!", cmd.message.Author.Mention()), false)
|
2020-06-12 05:17:58 +00:00
|
|
|
return "", nil
|
2020-06-09 13:04:44 +00:00
|
|
|
},
|
|
|
|
help: "Register yourself with the Discord",
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 03:11:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|