Create series command #190
3 changed files with 148 additions and 20 deletions
45
src/buttonEvents/Series.ts
Normal file
45
src/buttonEvents/Series.ts
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
import { ButtonInteraction } from "discord.js";
|
||||||
|
import { ButtonEvent } from "../type/buttonEvent";
|
||||||
|
import AppLogger from "../client/appLogger";
|
||||||
|
import SeriesHelper from "../helpers/SeriesHelper";
|
||||||
|
|
||||||
|
export default class Series extends ButtonEvent {
|
||||||
|
public override async execute(interaction: ButtonInteraction) {
|
||||||
|
const subaction = interaction.customId.split(" ")[1];
|
||||||
|
|
||||||
|
switch(subaction) {
|
||||||
|
case "view":
|
||||||
|
await this.ViewSeries(interaction);
|
||||||
|
break;
|
||||||
|
case "list":
|
||||||
|
await this.ListSeries(interaction);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
AppLogger.LogWarn("Commands/Series", `Subaction doesn't exist: ${subaction}`);
|
||||||
|
interaction.reply("Subaction doesn't exist.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ViewSeries(interaction: ButtonInteraction) {
|
||||||
|
const seriesid = interaction.customId.split(" ")[2];
|
||||||
|
const page = interaction.customId.split(" ")[3];
|
||||||
|
|
||||||
|
const embed = SeriesHelper.GenerateSeriesViewPage(Number(seriesid), Number(page));
|
||||||
|
|
||||||
|
await interaction.update({
|
||||||
|
embeds: [ embed!.embed ],
|
||||||
|
components: [ embed!.row ],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ListSeries(interaction: ButtonInteraction) {
|
||||||
|
const page = interaction.customId.split(" ")[2];
|
||||||
|
|
||||||
|
const embed = SeriesHelper.GenerateSeriesListPage(Number(page));
|
||||||
|
|
||||||
|
await interaction.update({
|
||||||
|
embeds: [ embed!.embed ],
|
||||||
|
components: [ embed!.row ],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ import { Command } from "../type/command";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
import EmbedColours from "../constants/EmbedColours";
|
import EmbedColours from "../constants/EmbedColours";
|
||||||
import AppLogger from "../client/appLogger";
|
import AppLogger from "../client/appLogger";
|
||||||
|
import SeriesHelper from "../helpers/SeriesHelper";
|
||||||
|
|
||||||
export default class Series extends Command {
|
export default class Series extends Command {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -58,30 +59,14 @@ export default class Series extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const description = series.cards
|
const embed = SeriesHelper.GenerateSeriesViewPage(series.id, 0);
|
||||||
.map(x => { return `[${x.id}] ${x.name}` })
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
await interaction.reply({ embeds: [ embed!.embed ], components: [ embed!.row ]});
|
||||||
.setTitle(series.name)
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setDescription(description)
|
|
||||||
.setFooter({ text: `${series.id} · ${series.cards.length} cards` });
|
|
||||||
|
|
||||||
await interaction.reply({ embeds: [ embed ]});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ListSeries(interaction: CommandInteraction) {
|
private async ListSeries(interaction: CommandInteraction) {
|
||||||
const description = CoreClient.Cards
|
const embed = SeriesHelper.GenerateSeriesListPage(0);
|
||||||
.map(x => { return `[${x.id}] ${x.name}` })
|
|
||||||
.join("\n");
|
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
await interaction.reply({ embeds: [ embed!.embed ], components: [ embed!.row ]});
|
||||||
.setTitle("Series")
|
|
||||||
.setColor(EmbedColours.Ok)
|
|
||||||
.setDescription(description)
|
|
||||||
.setFooter({ text: `${CoreClient.Cards.length} series` });
|
|
||||||
|
|
||||||
await interaction.reply({ embeds: [ embed ]});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
98
src/helpers/SeriesHelper.ts
Normal file
98
src/helpers/SeriesHelper.ts
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
|
||||||
|
import AppLogger from "../client/appLogger";
|
||||||
|
import cloneDeep from "clone-deep";
|
||||||
|
import { CoreClient } from "../client/client";
|
||||||
|
import EmbedColours from "../constants/EmbedColours";
|
||||||
|
|
||||||
|
export default class SeriesHelper {
|
||||||
|
public static GenerateSeriesViewPage(seriesId: number, page: number): { embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> } | null {
|
||||||
|
AppLogger.LogSilly("Helpers/SeriesHelper", `Parameters: seriesId=${seriesId}, page=${page}`);
|
||||||
|
|
||||||
|
const itemsPerPage = 15;
|
||||||
|
|
||||||
|
const series = cloneDeep(CoreClient.Cards)
|
||||||
|
.find(x => x.id == seriesId);
|
||||||
|
|
||||||
|
if (!series) {
|
||||||
|
AppLogger.LogVerbose("Helpers/SeriesHelper", `Unable to find series: ${seriesId}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(series.cards.length / itemsPerPage);
|
||||||
|
|
||||||
|
if (page > totalPages) {
|
||||||
|
AppLogger.LogVerbose("Helpers/SeriesHelper", `Trying to find page greater than what exists for this series. Page: ${page} but there are only ${totalPages} pages`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cardsOnPage = series.cards.splice(page * itemsPerPage, itemsPerPage);
|
||||||
|
|
||||||
|
const description = cardsOnPage
|
||||||
|
.map(x => `[${x.id}] ${x.name}`)
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(series.name)
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setDescription(description)
|
||||||
|
.setFooter({ text: `${series.id} · ${series.cards.length} cards · Page ${page + 1} of ${totalPages}` });
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`series view ${seriesId} ${page - 1}`)
|
||||||
|
.setLabel("Previous")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page == 0),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`series view ${seriesId} ${page + 1}`)
|
||||||
|
.setLabel("Next")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page + 1 > totalPages));
|
||||||
|
|
||||||
|
return { embed, row };
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GenerateSeriesListPage(page: number): { embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> } | null {
|
||||||
|
AppLogger.LogSilly("Helpers/InventoryHelper", `Parameters: page=${page}`);
|
||||||
|
|
||||||
|
const itemsPerPage = 15;
|
||||||
|
|
||||||
|
const series = cloneDeep(CoreClient.Cards)
|
||||||
|
.sort((a, b) => a.id - b.id);
|
||||||
|
|
||||||
|
const totalPages = Math.ceil(series.length / itemsPerPage);
|
||||||
|
|
||||||
|
if (page > totalPages) {
|
||||||
|
AppLogger.LogVerbose("Helpers/SeriesHelper", `Trying to find page greater than what exists for this series. Page: ${page} but there are only ${totalPages} pages`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const seriesOnPage = series.splice(page * itemsPerPage, itemsPerPage);
|
||||||
|
|
||||||
|
const description = seriesOnPage
|
||||||
|
.map(x => `[${x.id}] ${x.name}`)
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle("Series")
|
||||||
|
.setColor(EmbedColours.Ok)
|
||||||
|
.setDescription(description)
|
||||||
|
.setFooter({ text: `${CoreClient.Cards.length} series · Page ${page + 1} of ${totalPages}` });
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`series list ${page - 1}`)
|
||||||
|
.setLabel("Previous")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page == 0),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`series list ${page + 1}`)
|
||||||
|
.setLabel("Next")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page + 1 > totalPages));
|
||||||
|
|
||||||
|
return { embed, row };
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue