Skip to content

Commit

Permalink
feat: add close button
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Mar 3, 2024
1 parent 11d1a4e commit 3fdabeb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
38 changes: 36 additions & 2 deletions src/commands/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { db } from "@/db/db";
import { match } from "@/db/schema";
import { createMatchDamageFile } from "@/features/details/matchDamage";
import { createMatchDetailsFile } from "@/features/details/matchDetails";
import { galeforce } from "@/features/summoner";
import { ButtonInteraction } from "discord.js";
import { getComponentsRow } from "@/features/lol/elo";
import { ButtonBuilder } from "@discordjs/builders";
import { ButtonInteraction, ButtonStyle } from "discord.js";
import { eq } from "drizzle-orm";

export const executeButtonInteraction = async (interaction: ButtonInteraction) => {
Expand All @@ -24,8 +25,16 @@ export const executeButtonInteraction = async (interaction: ButtonInteraction) =

const file = await createMatchDetailsFile(details, participant);

const closeButton = new ButtonBuilder()
.setCustomId(`close-${matchId}`)
.setLabel("Close")
.setStyle(ButtonStyle.Danger);

const row = getComponentsRow(matchId, [closeButton]);

await interaction.message.edit({
files: [file],
components: [row],
});

return void interaction.deferUpdate();
Expand All @@ -48,8 +57,33 @@ export const executeButtonInteraction = async (interaction: ButtonInteraction) =

const file = await createMatchDamageFile(details, participant);

const closeButton = new ButtonBuilder()
.setCustomId(`close-${matchId}`)
.setLabel("Close")
.setStyle(ButtonStyle.Danger);

const row = getComponentsRow(matchId, [closeButton]);

await interaction.message.edit({
files: [file],
components: [row],
});

return void interaction.deferUpdate();
}

if (interaction.customId.startsWith("close")) {
const matchId = interaction.customId.split("-")[1];

if (!matchId) {
return void console.log("No matchId found in customId", interaction.customId);
}

const row = getComponentsRow(matchId);

await interaction.message.edit({
files: [],
components: [row],
});

return void interaction.deferUpdate();
Expand Down
26 changes: 15 additions & 11 deletions src/features/lol/elo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ export const getCheckEloEmbedAndButton = async ({
}) => {
const lastGame = await getNewLastGameIfExists(summ);

const detailsButton = new ButtonBuilder()
.setLabel("Details")
.setCustomId(`details-${lastGame.metadata.matchId}`)
.setStyle(ButtonStyle.Secondary);

const damageButton = new ButtonBuilder()
.setLabel("Damages")
.setCustomId(`damages-${lastGame.metadata.matchId}`)
.setStyle(ButtonStyle.Danger);

const row = new ActionRowBuilder().addComponents(detailsButton, damageButton);
const row = getComponentsRow(lastGame.metadata.matchId);

if (!lastRank) {
const embed = await getFirstRankEmbed(summ, newRank, elo, lastGame);
Expand All @@ -122,6 +112,20 @@ export const getCheckEloEmbedAndButton = async ({
return { embed, lastGame, row };
};

export const getComponentsRow = (matchId: string, additionalComponents?: ButtonBuilder[]) => {
const detailsButton = new ButtonBuilder()
.setLabel("Details")
.setCustomId(`details-${matchId}`)
.setStyle(ButtonStyle.Secondary);

const damageButton = new ButtonBuilder()
.setLabel("Damages")
.setCustomId(`damages-${matchId}`)
.setStyle(ButtonStyle.Secondary);

return new ActionRowBuilder().addComponents(detailsButton, damageButton, ...(additionalComponents || []));
};

export const checkBets = async () => {
const bets = await checkBetsAndGetLastGame();
if (!bets.length) return;
Expand Down

0 comments on commit 3fdabeb

Please sign in to comment.