Create simple inventory list (#120)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
# Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Create a simple inventory list #97 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. # Checklist - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/120 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
66b499d8d2
commit
bcb7beabc6
10 changed files with 176 additions and 5 deletions
2
.dev.env
2
.dev.env
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=0.3.1
|
BOT_VER=0.4.0
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=682942374040961060
|
BOT_CLIENTID=682942374040961060
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=0.3.1
|
BOT_VER=0.4.0
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=1093810443589529631
|
BOT_CLIENTID=1093810443589529631
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
# any secret values.
|
# any secret values.
|
||||||
|
|
||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
BOT_VER=0.3.1
|
BOT_VER=0.4.0
|
||||||
BOT_AUTHOR=Vylpes
|
BOT_AUTHOR=Vylpes
|
||||||
BOT_OWNERID=147392775707426816
|
BOT_OWNERID=147392775707426816
|
||||||
BOT_CLIENTID=1147976642942214235
|
BOT_CLIENTID=1147976642942214235
|
||||||
|
|
21
src/buttonEvents/Inventory.ts
Normal file
21
src/buttonEvents/Inventory.ts
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
import { ButtonInteraction } from "discord.js";
|
||||||
|
import { ButtonEvent } from "../type/buttonEvent";
|
||||||
|
import InventoryHelper from "../helpers/InventoryHelper";
|
||||||
|
|
||||||
|
export default class Inventory extends ButtonEvent {
|
||||||
|
public override async execute(interaction: ButtonInteraction) {
|
||||||
|
const userid = interaction.customId.split(' ')[1];
|
||||||
|
const page = interaction.customId.split(' ')[2];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const embed = await InventoryHelper.GenerateInventoryPage(interaction.user.username, userid, Number(page));
|
||||||
|
|
||||||
|
await interaction.update({
|
||||||
|
embeds: [ embed.embed ],
|
||||||
|
components: [ embed.row ],
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
await interaction.reply("No page for user found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,6 +14,7 @@ import { Environment } from "../constants/Environment";
|
||||||
import Webhooks from "../webhooks";
|
import Webhooks from "../webhooks";
|
||||||
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
||||||
import SeriesMetadata from "../contracts/SeriesMetadata";
|
import SeriesMetadata from "../contracts/SeriesMetadata";
|
||||||
|
import InventoryHelper from "../helpers/InventoryHelper";
|
||||||
|
|
||||||
export class CoreClient extends Client {
|
export class CoreClient extends Client {
|
||||||
private static _commandItems: ICommandItem[];
|
private static _commandItems: ICommandItem[];
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { AttachmentBuilder, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
import { AttachmentBuilder, CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
import { Command } from "../type/command";
|
import { Command } from "../type/command";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
|
|
38
src/commands/inventory.ts
Normal file
38
src/commands/inventory.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
|
import { Command } from "../type/command";
|
||||||
|
import InventoryHelper from "../helpers/InventoryHelper";
|
||||||
|
|
||||||
|
export default class Inventory extends Command {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.CommandBuilder = new SlashCommandBuilder()
|
||||||
|
.setName('inventory')
|
||||||
|
.setDescription('View your inventory')
|
||||||
|
.addNumberOption(x =>
|
||||||
|
x
|
||||||
|
.setName('page')
|
||||||
|
.setDescription('The page to start with'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async execute(interaction: CommandInteraction) {
|
||||||
|
const page = interaction.options.get('page');
|
||||||
|
|
||||||
|
try {
|
||||||
|
let pageNumber = 0;
|
||||||
|
|
||||||
|
if (page && page.value) {
|
||||||
|
pageNumber = Number(page.value) - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = await InventoryHelper.GenerateInventoryPage(interaction.user.username, interaction.user.id, pageNumber);
|
||||||
|
|
||||||
|
await interaction.reply({
|
||||||
|
embeds: [ embed.embed ],
|
||||||
|
components: [ embed.row ],
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
await interaction.reply("No page for user found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -40,4 +40,12 @@ export default class Inventory extends AppBaseEntity {
|
||||||
|
|
||||||
return single;
|
return single;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async FetchAllByUserId(userId: string): Promise<Inventory[]> {
|
||||||
|
const repository = AppDataSource.getRepository(Inventory);
|
||||||
|
|
||||||
|
const all = await repository.find({ where: { UserId: userId }});
|
||||||
|
|
||||||
|
return all;
|
||||||
|
}
|
||||||
}
|
}
|
99
src/helpers/InventoryHelper.ts
Normal file
99
src/helpers/InventoryHelper.ts
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js";
|
||||||
|
import Inventory from "../database/entities/app/Inventory";
|
||||||
|
import { CoreClient } from "../client/client";
|
||||||
|
import SeriesMetadata from "../contracts/SeriesMetadata";
|
||||||
|
import EmbedColours from "../constants/EmbedColours";
|
||||||
|
import { CardRarity, CardRarityToString } from "../constants/CardRarity";
|
||||||
|
|
||||||
|
interface InventoryPage {
|
||||||
|
id: number,
|
||||||
|
name: string,
|
||||||
|
cards: InventoryPageCards[],
|
||||||
|
seriesSubpage: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
interface InventoryPageCards {
|
||||||
|
id: string,
|
||||||
|
name: string,
|
||||||
|
type: CardRarity,
|
||||||
|
quantity: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class InventoryHelper {
|
||||||
|
public static async GenerateInventoryPage(username: string, userid: string, page: number): Promise<{ embed: EmbedBuilder, row: ActionRowBuilder<ButtonBuilder> }> {
|
||||||
|
const cardsPerPage = 15;
|
||||||
|
|
||||||
|
const inventory = await Inventory.FetchAllByUserId(userid);
|
||||||
|
|
||||||
|
const allSeriesClaimed = CoreClient.Cards
|
||||||
|
.sort((a, b) => a.id - b.id)
|
||||||
|
.filter(x => {
|
||||||
|
x.cards = x.cards
|
||||||
|
.sort((a, b) => b.type - a.type)
|
||||||
|
.filter(y => inventory.find(z => z.CardNumber == y.id));
|
||||||
|
|
||||||
|
return x;
|
||||||
|
});
|
||||||
|
|
||||||
|
const pages: InventoryPage[] = [];
|
||||||
|
|
||||||
|
for (let series of allSeriesClaimed) {
|
||||||
|
const seriesCards = series.cards;
|
||||||
|
|
||||||
|
for (let i = 0; i < seriesCards.length; i+= cardsPerPage) {
|
||||||
|
const cards = series.cards.slice(i, i + cardsPerPage);
|
||||||
|
const pageCards: InventoryPageCards[] = [];
|
||||||
|
|
||||||
|
for (let card of cards) {
|
||||||
|
const item = inventory.find(x => x.CardNumber == card.id);
|
||||||
|
|
||||||
|
if (!item) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pageCards.push({
|
||||||
|
id: card.id,
|
||||||
|
name: card.name,
|
||||||
|
type: card.type,
|
||||||
|
quantity: item.Quantity,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pages.push({
|
||||||
|
id: series.id,
|
||||||
|
name: series.name,
|
||||||
|
cards: pageCards,
|
||||||
|
seriesSubpage: i / cardsPerPage,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentPage = pages[page];
|
||||||
|
|
||||||
|
if (!currentPage) {
|
||||||
|
console.error("Unable to find page");
|
||||||
|
return Promise.reject("Unable to find page");
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setTitle(username)
|
||||||
|
.setDescription(`**${currentPage.name} (${currentPage.seriesSubpage + 1})**\n${currentPage.cards.map(x => `[${x.id}] ${x.name} (${CardRarityToString(x.type)}) x${x.quantity}`).join('\n')}`)
|
||||||
|
.setFooter({ text: `Page ${page + 1} of ${pages.length}` })
|
||||||
|
.setColor(EmbedColours.Ok);
|
||||||
|
|
||||||
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||||
|
.addComponents(
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`inventory ${userid} ${page - 1}`)
|
||||||
|
.setLabel("Previous")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page == 0),
|
||||||
|
new ButtonBuilder()
|
||||||
|
.setCustomId(`inventory ${userid} ${page + 1}`)
|
||||||
|
.setLabel("Next")
|
||||||
|
.setStyle(ButtonStyle.Primary)
|
||||||
|
.setDisabled(page + 1 == pages.length));
|
||||||
|
|
||||||
|
return { embed, row };
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
import { CoreClient } from "./client/client";
|
import { CoreClient } from "./client/client";
|
||||||
|
import { Environment } from "./constants/Environment";
|
||||||
|
|
||||||
// Global Command Imports
|
// Global Command Imports
|
||||||
import About from "./commands/about";
|
import About from "./commands/about";
|
||||||
import Drop from "./commands/drop";
|
import Drop from "./commands/drop";
|
||||||
import Gdrivesync from "./commands/gdrivesync";
|
import Gdrivesync from "./commands/gdrivesync";
|
||||||
|
import Inventory from "./commands/inventory";
|
||||||
import Resync from "./commands/resync";
|
import Resync from "./commands/resync";
|
||||||
|
|
||||||
// Test Command Imports
|
// Test Command Imports
|
||||||
|
@ -12,8 +14,8 @@ import Droprarity from "./commands/stage/droprarity";
|
||||||
|
|
||||||
// Button Event Imports
|
// Button Event Imports
|
||||||
import Claim from "./buttonEvents/Claim";
|
import Claim from "./buttonEvents/Claim";
|
||||||
|
import InventoryButtonEvent from "./buttonEvents/Inventory";
|
||||||
import Reroll from "./buttonEvents/Reroll";
|
import Reroll from "./buttonEvents/Reroll";
|
||||||
import { Environment } from "./constants/Environment";
|
|
||||||
|
|
||||||
export default class Registry {
|
export default class Registry {
|
||||||
public static RegisterCommands() {
|
public static RegisterCommands() {
|
||||||
|
@ -21,6 +23,7 @@ export default class Registry {
|
||||||
CoreClient.RegisterCommand('about', new About());
|
CoreClient.RegisterCommand('about', new About());
|
||||||
CoreClient.RegisterCommand('drop', new Drop());
|
CoreClient.RegisterCommand('drop', new Drop());
|
||||||
CoreClient.RegisterCommand('gdrivesync', new Gdrivesync());
|
CoreClient.RegisterCommand('gdrivesync', new Gdrivesync());
|
||||||
|
CoreClient.RegisterCommand('inventory', new Inventory());
|
||||||
CoreClient.RegisterCommand('resync', new Resync());
|
CoreClient.RegisterCommand('resync', new Resync());
|
||||||
|
|
||||||
// Test Commands
|
// Test Commands
|
||||||
|
@ -34,6 +37,7 @@ export default class Registry {
|
||||||
|
|
||||||
public static RegisterButtonEvents() {
|
public static RegisterButtonEvents() {
|
||||||
CoreClient.RegisterButtonEvent('claim', new Claim());
|
CoreClient.RegisterButtonEvent('claim', new Claim());
|
||||||
|
CoreClient.RegisterButtonEvent('inventory', new InventoryButtonEvent);
|
||||||
CoreClient.RegisterButtonEvent('reroll', new Reroll());
|
CoreClient.RegisterButtonEvent('reroll', new Reroll());
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue