Skip to content

Commit

Permalink
add score confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
WhatCats committed Dec 8, 2024
1 parent e069ba2 commit d7ff119
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
22 changes: 22 additions & 0 deletions src/discord/buttons/confirmScore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { UserError } from "@/lib/discord/UserError"
import { getGame } from "@/lib/game"
import { scoreGame } from "@/lib/game/scoreGame"
import { type ButtonInteraction } from "discord.js"

export default {
id: "confirmScore",
async execute(interaction: ButtonInteraction) {
await interaction.deferReply({ ephemeral: true })

const score1 = parseInt(interaction.args.shift()!)
const score2 = parseInt(interaction.args.shift()!)

const game = await getGame(interaction.channelId)
if (!game) throw new UserError("This command can only be used in game channels!")

const success = await scoreGame(game, score1, score2)
if (!success) throw new UserError("This game has already been scored.")

return "Game scored."
},
}
2 changes: 1 addition & 1 deletion src/discord/buttons/score.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
throw new UserError("This score request must be accepted by the other team.")

await interaction.editReply("Closing channel...")
const success = await closeChannel(game)
const success = await closeChannel(game, interaction.message.embeds[0]?.image?.url)
if (!success) throw new UserError("Channel already closed!")
},
}
34 changes: 29 additions & 5 deletions src/discord/commands/scoreGame.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { InteractionContextType, SlashCommandBuilder, type ChatInputCommandInteraction } from "discord.js"
import {
bold,
ButtonStyle,
InteractionContextType,
SlashCommandBuilder,
userMention,
type ChatInputCommandInteraction,
} from "discord.js"

import { MessageOptionsBuilder } from "@/lib/discord/MessageOptionsBuilder"
import { UserError } from "@/lib/discord/UserError"
import { getGame } from "@/lib/game"
import { scoreGame } from "@/lib/game/scoreGame"

const Options = {
Team1Score: "team-1-score",
Expand Down Expand Up @@ -36,9 +43,26 @@ export default {
const game = await getGame(interaction.channelId)
if (!game) throw new UserError("This command can only be used in game channels!")

const success = await scoreGame(game, score1, score2)
if (!success) throw new UserError("This game has already been scored.")
const winner = score1 > score2 ? game.teams[0] : score2 > score1 ? game.teams[1] : null
const score = score1 > score2 ? `${score1}-${score2}` : `${score2}-${score1}`

return "Game scored."
await interaction.editReply(
new MessageOptionsBuilder()
.setContent(
"Please confirm " +
bold(
winner
? winner.players.map(userMention).join(" ") + ` won ${score}`
: "game ended in a draw",
) +
".",
)
.addButtons((button) =>
button
.setLabel("Confirm")
.setStyle(ButtonStyle.Success)
.setCustomId(`confirmScore:${score1}:${score2}`),
),
)
},
}
6 changes: 4 additions & 2 deletions src/lib/game/closeChannel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Game } from "@/database/models/Game"
import { client } from "@/discord"
import { client, colors } from "@/discord"
import {
EmbedBuilder,
OverwriteType,
Expand All @@ -10,7 +10,7 @@ import {
type OverwriteResolvable,
} from "discord.js"

export async function closeChannel(game: Game) {
export async function closeChannel(game: Game, image?: string) {
const guild = await client.guilds.fetch(game.guildId!)
if (!guild) return

Expand All @@ -20,6 +20,8 @@ export async function closeChannel(game: Game) {
const embed = new EmbedBuilder()
.setTitle(`Game ${game.sequence} Finished`)
.setDescription(`The game has finished. Please score it via the \`/score-game\` command.`)
.setColor(colors.baseColor)
.setImage(image ?? null)
.setFields(
game.teams.map((v, i) => ({
name: `Team ${i + 1}`,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/game/voidGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function voidGame(gameId: string) {

const success = await archiveGame(
game,
game.teams.map(() => 0),
game.teams.map(() => -1),
)

if (!success) return false
Expand Down
2 changes: 1 addition & 1 deletion src/lib/queue/tempNicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const previousNicks = new Map<string, string | null>()
export function setTempNick(queue: Queue, user: string, nick: string) {
const guild = client.guilds.cache.get(queue.guildId)
const member = guild?.members.cache.get(user)
if (member && guild?.ownerId === member.id) {
if (member && guild?.ownerId !== member.id) {
previousNicks.set(user, member.nickname)
updateNick(queue, user, nick).catch(console.error)
}
Expand Down

0 comments on commit d7ff119

Please sign in to comment.