118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package discord
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
|
|
"github.com/Karitham/corde"
|
|
)
|
|
|
|
func (d Discord) accept(w corde.ResponseWriter, i *corde.Interaction) {
|
|
id := i.Data.Options.Int64("app_id")
|
|
|
|
app, err := d.store.ApplicationByID(id)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Contentf("No application #%d was found.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if !app.Accepted.Bool {
|
|
t := true
|
|
if err := d.store.ApplicationUpdateAccepted(&t, app.ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not accept application #%d.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
players, err := d.store.PlayersByUsername(app.Username)
|
|
if err != nil || len(players) == 0 {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return
|
|
}
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if err := d.store.PlayerUpdateApplication(app.ID, players[0].ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
}
|
|
|
|
if err := d.sock.Command("accept", app.Username); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not accept player in-game, is the server running?", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
w.Respond(corde.NewResp().Contentf("Application #%d was successfully accepted.", id).B())
|
|
}
|
|
}
|
|
|
|
func (d Discord) deny(w corde.ResponseWriter, i *corde.Interaction) {
|
|
id := i.Data.Options.Int64("app_id")
|
|
|
|
app, err := d.store.ApplicationByID(id)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Contentf("No application #%d was found.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if !app.Accepted.Bool {
|
|
f := false
|
|
if err := d.store.ApplicationUpdateAccepted(&f, app.ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not deny application #%d.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
players, err := d.store.PlayersByUsername(app.Username)
|
|
if err != nil || len(players) == 0 {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return
|
|
}
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if err := d.store.PlayerUpdateApplication(app.ID, players[0].ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
}
|
|
|
|
if err := d.sock.Command("deny", app.Username); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not deny player in-game, is the server running?", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
w.Respond(corde.NewResp().Contentf("Application #%d was successfully denied.", id).B())
|
|
}
|
|
}
|
|
|
|
func (d Discord) clear(w corde.ResponseWriter, i *corde.Interaction) {
|
|
id := i.Data.Options.Int64("app_id")
|
|
|
|
app, err := d.store.ApplicationByID(id)
|
|
if err != nil {
|
|
w.Respond(corde.NewResp().Contentf("No application #%d was found.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if !app.Accepted.Bool {
|
|
f := false
|
|
if err := d.store.ApplicationUpdateAccepted(&f, app.ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not clear application #%d.", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
players, err := d.store.PlayersByUsername(app.Username)
|
|
if err != nil || len(players) == 0 {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return
|
|
}
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
return
|
|
}
|
|
|
|
if err := d.store.PlayerUpdateApplication(app.ID, players[0].ID); err != nil {
|
|
w.Respond(corde.NewResp().Contentf("Could not link player to application", id).Ephemeral().B())
|
|
}
|
|
|
|
w.Respond(corde.NewResp().Contentf("Application #%d was successfully cleared.", id).B())
|
|
}
|
|
}
|