143 lines
4.2 KiB
Go
143 lines
4.2 KiB
Go
package discord
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.jojodev.com/minecraft/mcm-discord/database"
|
|
"github.com/Karitham/corde"
|
|
)
|
|
|
|
func appResponse(app *database.Application) *corde.RespB {
|
|
embed := corde.NewEmbed().
|
|
Color(0x417505).
|
|
Title("Application").
|
|
Thumbnail(corde.Image{
|
|
URL: fmt.Sprintf("https://minotar.net/helm/%s/100.png", app.Username),
|
|
}).
|
|
Field("Username", app.EscapeUsername()).
|
|
Field("Age", fmt.Sprint(app.Age)).
|
|
Field("Type of Player", app.PlayerType).
|
|
Field("Ever been Banned", fmt.Sprint(app.EverBanned))
|
|
if app.EverBanned && app.EverBannedExplanation != "" {
|
|
embed.Field("Reason for being banned", app.EverBannedExplanation)
|
|
}
|
|
if app.Reference != "" {
|
|
embed.Field("Reference", app.Reference)
|
|
}
|
|
embed.Field("Read the Rules", app.ReadRules).
|
|
Field("Status", app.Status())
|
|
return corde.NewResp().Embeds(embed)
|
|
}
|
|
|
|
func (d Discord) info(w corde.ResponseWriter, i *corde.Interaction) {
|
|
id := i.Data.Options.Int64("app_id")
|
|
username := i.Data.Options.String("username")
|
|
if id == 0 && username == "" {
|
|
w.Respond(corde.NewResp().Content("Info requires an application ID or username.").Ephemeral())
|
|
return
|
|
}
|
|
|
|
var app *database.Application
|
|
var err error
|
|
if id != 0 {
|
|
app, err = d.store.ApplicationByID(id)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Contentf("No application #%d was found.", id).Ephemeral())
|
|
return
|
|
}
|
|
} else {
|
|
apps, err := d.store.ApplicationsByUsername(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
if len(apps) == 0 {
|
|
apps, err = d.store.ApplicationsByPlayerUsername(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
if len(apps) == 1 {
|
|
app = apps[0]
|
|
}
|
|
} else if len(apps) == 1 {
|
|
app = apps[0]
|
|
}
|
|
}
|
|
|
|
w.Respond(appResponse(app).Ephemeral())
|
|
}
|
|
|
|
func (d Discord) search(w corde.ResponseWriter, i *corde.Interaction) {
|
|
username := i.Data.Options.String("username")
|
|
|
|
apps, err := d.store.ApplicationsByUsername(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
count, err := d.store.ApplicationsByUsernameCount(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
|
|
if count > 0 {
|
|
if count == 1 {
|
|
w.Respond(appResponse(apps[0]).Ephemeral())
|
|
return
|
|
} else {
|
|
var sb strings.Builder
|
|
sb.WriteString("**Found the following applications**")
|
|
for _, app := range apps {
|
|
sb.WriteString(fmt.Sprintf("\n%d - %s (%s)", app.ID, app.EscapeUsername(), app.Status()))
|
|
}
|
|
if count > 10 {
|
|
sb.WriteString(fmt.Sprintf("\n**This is only 10 applications out of %d found. Please narrow your search if possible.**", count))
|
|
}
|
|
w.Respond(corde.NewResp().Content(sb.String()).Ephemeral())
|
|
}
|
|
}
|
|
|
|
players, err := d.store.PlayersByUsernameApplicationNotNull(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
count, err = d.store.PlayersByUsernameApplicationNotNullCount(username)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
|
|
if count > 0 {
|
|
if count == 1 {
|
|
app, err := d.store.ApplicationByID(players[0].ApplicationID.Int64)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
w.Respond(appResponse(app).Ephemeral())
|
|
return
|
|
} else {
|
|
var sb strings.Builder
|
|
sb.WriteString("**No applications matched, however there are player matches**")
|
|
for _, player := range players {
|
|
app, err := d.store.ApplicationByID(player.ApplicationID.Int64)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
return
|
|
}
|
|
sb.WriteString(fmt.Sprintf("\n%d - %s AKA %s (%s)", app.ID, app.EscapeUsername(), player.EscapeUsername(), app.Status()))
|
|
}
|
|
if count > 10 {
|
|
sb.WriteString(fmt.Sprintf("\n**This is only 10 players out of %d found. Please narrow your search if possible.**", count))
|
|
}
|
|
w.Respond(corde.NewResp().Content(sb.String()).Ephemeral())
|
|
}
|
|
}
|
|
|
|
w.Respond(corde.NewResp().Content("No applications matched.").Ephemeral())
|
|
}
|