import { CommandContext , CommandOptionType , SlashCommand , SlashCreator } from "slash-create" ;
import { Snowflake } from "discord-api-types" ;
import { singleRowQueryResult } from "../../queries/QueryHelpers.js" ;
import { sendErrorMessage } from "../../queries/ErrorCodes.js" ;
import { Pool } from "pg" ;
export class JoinCommand extends SlashCommand {
readonly pool : Pool
constructor ( creator : SlashCreator , { pool , gameGuildIds , genders } : {
pool : Pool ,
gameGuildIds : Snowflake [ ] ,
genders : { id : string , name : string } [ ] ,
} ) {
super ( creator , {
name : "join" ,
guildIDs : gameGuildIds ,
description : "Allows a new player to join the game." ,
options : [
{
name : "name" ,
description : "Your alias for the purposes of this game. Purely cosmetic. You can change it at any time." ,
required : true ,
type : CommandOptionType . STRING ,
} ,
{
name : "gender" ,
description : "Your gender for the purposes of this game. Purely cosmetic. You can change it at any time." ,
required : true ,
type : CommandOptionType . STRING ,
choices : genders.map ( ( item ) = > ( {
name : item.name ,
value : item.id ,
} ) ) ,
}
]
} ) ;
this . pool = pool
}
async run ( ctx : CommandContext ) : Promise < void > {
const name = ctx . options . name ? ? "Anonymous"
const gender = ctx . options . gender ? ? "nb"
try {
const result =
singleRowQueryResult ( await this . pool . query < { resultid : number , newplayername : string , newgendername : string , wascreated : boolean } > ( {
text : ` SELECT resultId, newPlayerName, newGenderName, wasCreated
FROM Command_Join ( $1 , $2 , $3 , $4 , $5 , $6 , $7 ) ` ,
values : [
ctx . channelID , ctx . guildID , ctx . user . id , ctx . user . username , ctx . user . discriminator ,
name , gender ] ,
} ) )
console . log ( result )
if ( typeof result === "undefined" ) {
await ctx . send ( {
content : "Unexpectedly got no results!!" ,
ephemeral : true ,
} )
console . log ( "Unexpectedly empty Command_Join result!" )
} else if ( result . wascreated ) {
await ctx . send ( {
content : ` You got it! Welcome aboard, ${ result . newplayername } ! I have you down in my records as ${ result . newgendername } . If you ever want to change your name or gender, just /join again! ` ,
ephemeral : true ,
} )
} else {
await ctx . send ( {
content : ` Duly noted! I've updated your deets to have you down as ${ result . newplayername } , who is ${ result . newgendername } . If you ever want to change your name or gender, just /join again! ` ,
ephemeral : true ,
} )
}
} catch ( e ) {
await sendErrorMessage ( ctx , e )
}
}
}