You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.4 KiB
79 lines
3.4 KiB
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 or an existing player to update their registration.",
|
|
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,
|
|
})), {
|
|
name: "NULL",
|
|
value: "NULL",
|
|
}],
|
|
}
|
|
]
|
|
});
|
|
|
|
this.pool = pool
|
|
}
|
|
|
|
async run(ctx: CommandContext): Promise<void> {
|
|
const name = ctx.options.name ?? "Anonymous"
|
|
const gender = ctx.options.gender === "NULL" ? null : (ctx.options.gender ?? "nb")
|
|
try {
|
|
const result =
|
|
singleRowQueryResult(await this.pool.query<{ resultid: number, newplayername: string, newgendername: string | null, 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 ?? `\`NULL\``}. 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 ?? `\`NULL\``}. If you ever want to change your name or gender, just /join again!`,
|
|
ephemeral: true,
|
|
})
|
|
}
|
|
} catch (e) {
|
|
await sendErrorMessage(ctx, e)
|
|
}
|
|
}
|
|
} |