Skip to content

Commit

Permalink
Send bookmark on multiple choice; bookmark cleanup (#2031)
Browse files Browse the repository at this point in the history
* Send bookmark on multiple choice; bookmark cleanup

* Fix preventing bookmark outside game
  • Loading branch information
taahamahdi authored Apr 16, 2024
1 parent bc2b93d commit fac9c46
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 137 deletions.
11 changes: 1 addition & 10 deletions src/commands/game_commands/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,16 +391,7 @@ export default class LookupCommand implements BaseCommand {
locale: LocaleType,
): Promise<boolean> {
const guildID = messageOrInteraction.guildID as string;
const queriedSongRaw = await dbContext.kmq
.selectFrom("available_songs")
.select(SongSelector.QueriedSongFields)
.where("link", "=", videoID)
.executeTakeFirst();

const kmqSongEntry: QueriedSong | undefined = queriedSongRaw
? new QueriedSong(queriedSongRaw)
: undefined;

const kmqSongEntry = await SongSelector.getSongByLink(videoID);
const daisukiEntry = await dbContext.kpopVideos
.selectFrom("app_kpop")
.select([
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ export const ExpBonusModifierValues = {

export const LEADERBOARD_ENTRIES_PER_PAGE = 10;

export const BOOKMARK_COMMAND_NAME = "Bookmark Song";
export const PROFILE_COMMAND_NAME = "Profile";

export const BOOKMARK_BUTTON_PREFIX = "bookmark";

export const EMBED_ERROR_COLOR = 0xed4245; // Red
export const EMBED_SUCCESS_COLOR = 0x57f287; // Green
export const EMBED_SUCCESS_BONUS_COLOR = 0xfee75c; // Gold
Expand Down
45 changes: 19 additions & 26 deletions src/events/client/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as uuid from "uuid";
import { BOOKMARK_COMMAND_NAME, PROFILE_COMMAND_NAME } from "../../constants";
import { BOOKMARK_BUTTON_PREFIX, PROFILE_COMMAND_NAME } from "../../constants";
import { IPCLogger } from "../../logger";
import {
getDebugLogHeader,
Expand Down Expand Up @@ -150,11 +150,24 @@ export default async function interactionCreateHandler(
let interactionName: string | null = null;
try {
if (interaction instanceof Eris.ComponentInteraction) {
if (
!session ||
(!session.round && interaction.data.custom_id !== "bookmark")
) {
await tryInteractionAcknowledge(interaction);
if (!session) {
if (
interaction.data.custom_id.startsWith(
BOOKMARK_BUTTON_PREFIX,
)
) {
await tryCreateInteractionErrorAcknowledgement(
interaction,
null,
i18n.translate(
interaction.guildID as string,
"misc.failure.interaction.bookmarkOutsideGame",
),
);
} else {
await tryInteractionAcknowledge(interaction);
}

return;
}

Expand Down Expand Up @@ -251,26 +264,6 @@ export default async function interactionCreateHandler(
break;
}

case BOOKMARK_COMMAND_NAME: {
if (!session) {
await tryCreateInteractionErrorAcknowledgement(
interaction as Eris.CommandInteraction,
null,
i18n.translate(
interaction.guildID as string,
"misc.failure.interaction.bookmarkOutsideGame",
),
);
return;
}

interactionName = `Application Command for '${interaction.data.name}'`;
await session.handleBookmarkInteraction(
interaction as Eris.CommandInteraction,
);
break;
}

default: {
logger.error(
`No handler found for CommandInteraction (type = ${interaction.data.type}): ${interaction.data.name}`,
Expand Down
15 changes: 5 additions & 10 deletions src/helpers/discord_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import * as uuid from "uuid";
import {
BOOKMARK_COMMAND_NAME,
ConflictingGameOptions,
GameOptionCommand,
PriorityGameOption,
} from "../types";
import {
DataFiles,
EMBED_DESCRIPTION_MAX_LENGTH,
EMBED_ERROR_COLOR,
Expand All @@ -15,11 +19,6 @@ import {
SPOTIFY_BASE_URL,
YOUTUBE_PLAYLIST_BASE_URL,
} from "../constants";
import {
ConflictingGameOptions,
GameOptionCommand,
PriorityGameOption,
} from "../types";
import { IPCLogger } from "../logger";
import {
bold,
Expand Down Expand Up @@ -2267,10 +2266,6 @@ export const updateAppCommands = async (

if (appCommandType === AppCommandsAction.RELOAD) {
commandStructures = [
{
name: BOOKMARK_COMMAND_NAME,
type: Eris.Constants.ApplicationCommandTypes.MESSAGE,
},
{
name: PROFILE_COMMAND_NAME,
type: Eris.Constants.ApplicationCommandTypes.MESSAGE,
Expand Down
9 changes: 8 additions & 1 deletion src/structures/game_round.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BOOKMARK_BUTTON_PREFIX,
CORRECT_GUESS_EMOJI,
EMBED_ERROR_COLOR,
EMBED_SUCCESS_BONUS_COLOR,
Expand Down Expand Up @@ -385,13 +386,19 @@ export default class GameRound extends Round {
button.custom_id as ClipAction,
)
) {
// TODO heart here
return {
...button,
disabled: true,
};
}

if (
button.custom_id.startsWith(BOOKMARK_BUTTON_PREFIX)
) {
// Users should still be able to bookmark songs from previous rounds
return button;
}

const noGuesses =
this.interactionIncorrectAnswerUUIDs[
button.custom_id
Expand Down
47 changes: 25 additions & 22 deletions src/structures/game_session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default class GameSession extends Session {
),
);

const endRoundMessage = await this.sendRoundMessage(
await this.sendRoundMessage(
messageContext,
fields,
round,
Expand All @@ -394,10 +394,6 @@ export default class GameSession extends Session {
remainingDuration,
);

if (endRoundMessage) {
this.updateBookmarkSongList(endRoundMessage.id, round.song);
}

const gameFinishedDueToGameOptions = this.scoreboard.gameFinished(
this.guildPreference,
);
Expand Down Expand Up @@ -1138,7 +1134,7 @@ export default class GameSession extends Session {
},
],
components: this.isClipMode()
? [{ type: 1, components: this.generateClipButton() }]
? [{ type: 1, components: [this.generateClipButton()] }]
: undefined,
});
} catch (e) {
Expand Down Expand Up @@ -1935,13 +1931,22 @@ export default class GameSession extends Session {
break;
}

const lastRow: Eris.InteractionButton[] = [
this.generateBookmarkButton(
round,
State.getGuildLocale(this.guildID),
),
];

if (this.isClipMode()) {
actionRows.push({
type: 1,
components: this.generateClipButton(),
});
lastRow.push(this.generateClipButton());
}

actionRows.push({
type: 1,
components: lastRow,
});

round.interactionComponents = actionRows;

round.interactionMessage = await sendInfoMessage(
Expand Down Expand Up @@ -1971,7 +1976,7 @@ export default class GameSession extends Session {
round.interactionMessage = await sendInfoMessage(messageContext, {
...this.generateRemainingPlayersMessage(round),
components: this.isClipMode()
? [{ type: 1, components: this.generateClipButton() }]
? [{ type: 1, components: [this.generateClipButton()] }]
: undefined,
});

Expand All @@ -1997,22 +2002,20 @@ export default class GameSession extends Session {
components: [
{
type: 1,
components: this.generateClipButton(),
components: [this.generateClipButton()],
},
],
});
}

private generateClipButton(): Eris.InteractionButton[] {
return [
{
type: 2,
style: 2,
custom_id: ClipAction.NEW_CLIP,
label: i18n.translate(this.guildID, "misc.newClip"),
emoji: { name: "🎬", id: null },
},
];
private generateClipButton(): Eris.InteractionButton {
return {
type: 2,
style: 2,
custom_id: ClipAction.NEW_CLIP,
label: i18n.translate(this.guildID, "misc.newClip"),
emoji: { name: "🎬", id: null },
};
}

private async sendStartRoundMessage(
Expand Down
7 changes: 5 additions & 2 deletions src/structures/listening_round.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { EMBED_SUCCESS_BONUS_COLOR } from "../constants";
import {
BOOKMARK_BUTTON_PREFIX,
EMBED_SUCCESS_BONUS_COLOR,
} from "../constants";
import { IPCLogger } from "../logger";
import Round from "./round";
import type Eris from "eris";
Expand Down Expand Up @@ -47,7 +50,7 @@ export default class ListeningRound extends Round {
isValidInteraction(interactionID: string): boolean {
return (
interactionID === this.interactionSkipUUID ||
interactionID === "bookmark"
interactionID.startsWith(BOOKMARK_BUTTON_PREFIX)
);
}

Expand Down
10 changes: 3 additions & 7 deletions src/structures/listening_session.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BOOKMARK_BUTTON_PREFIX } from "../constants";
import { IPCLogger } from "../logger";
import { chooseRandom } from "../helpers/utils";
import {
Expand Down Expand Up @@ -112,11 +113,6 @@ export default class ListeningSession extends Session {
);

round.interactionMessage = startRoundMessage;

if (startRoundMessage) {
this.updateBookmarkSongList(startRoundMessage.id, round.song);
}

return round;
}

Expand Down Expand Up @@ -155,7 +151,7 @@ export default class ListeningSession extends Session {

if (!this.round) return false;
if (
interaction.data.custom_id !== "bookmark" &&
!interaction.data.custom_id.startsWith(BOOKMARK_BUTTON_PREFIX) &&
!(await this.handleInSessionInteractionFailures(
interaction,
messageContext,
Expand All @@ -166,7 +162,7 @@ export default class ListeningSession extends Session {

const round = this.round;
const guildID = interaction.guildID as string;
if (interaction.data.custom_id === "bookmark") {
if (interaction.data.custom_id.startsWith(BOOKMARK_BUTTON_PREFIX)) {
await this.handleBookmarkInteraction(interaction);
return true;
} else if (interaction.data.custom_id === round.interactionSkipUUID) {
Expand Down
Loading

0 comments on commit fac9c46

Please sign in to comment.