Create list effects command (#412)
All checks were successful
Deploy To Stage / build (push) Successful in 14s
Deploy To Stage / deploy (push) Successful in 16s

# 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.

#379

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# 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: #412
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:
Ethan Lane 2024-12-07 22:32:19 +00:00 committed by Vylpes
parent d7a5472759
commit 3d143e7c73
10 changed files with 644 additions and 0 deletions

View file

@ -0,0 +1,33 @@
import {ButtonInteraction} from "discord.js";
import {ButtonEvent} from "../type/buttonEvent";
import EffectHelper from "../helpers/EffectHelper";
export default class Effects extends ButtonEvent {
public override async execute(interaction: ButtonInteraction) {
const action = interaction.customId.split(" ")[1];
switch (action) {
case "list":
await this.List(interaction);
break;
}
}
private async List(interaction: ButtonInteraction) {
const pageOption = interaction.customId.split(" ")[2];
const page = Number(pageOption);
if (!page) {
await interaction.reply("Page option is not a valid number");
return;
}
const result = await EffectHelper.GenerateEffectEmbed(interaction.user.id, page);
await interaction.update({
embeds: [ result.embed ],
components: [ result.row ],
});
}
}

45
src/commands/effects.ts Normal file
View file

@ -0,0 +1,45 @@
import {CommandInteraction, SlashCommandBuilder} from "discord.js";
import {Command} from "../type/command";
import EffectHelper from "../helpers/EffectHelper";
export default class Effects extends Command {
constructor() {
super();
this.CommandBuilder = new SlashCommandBuilder()
.setName("effects")
.setDescription("Effects")
.addSubcommand(x => x
.setName("list")
.setDescription("List all effects I have")
.addNumberOption(x => x
.setName("page")
.setDescription("The page number")
.setMinValue(1)));
}
public override async execute(interaction: CommandInteraction) {
if (!interaction.isChatInputCommand()) return;
const subcommand = interaction.options.getSubcommand();
switch (subcommand) {
case "list":
await this.List(interaction);
break;
}
}
private async List(interaction: CommandInteraction) {
const pageOption = interaction.options.get("page");
const page = !isNaN(Number(pageOption?.value)) ? Number(pageOption?.value) : 1;
const result = await EffectHelper.GenerateEffectEmbed(interaction.user.id, page);
await interaction.reply({
embeds: [ result.embed ],
components: [ result.row ],
});
}
}

View file

@ -57,4 +57,18 @@ export default class UserEffect extends AppBaseEntity {
return single;
}
public static async FetchAllByUserIdPaginated(userId: string, page: number = 0, itemsPerPage: number = 10): Promise<[UserEffect[], number]> {
const repository = AppDataSource.getRepository(UserEffect);
const query = await repository.createQueryBuilder("effect")
.where("effect.UserId = :userId", { userId })
.where("effect.Unused > 0")
.orderBy("effect.Name", "ASC")
.skip(page * itemsPerPage)
.take(itemsPerPage)
.getManyAndCount();
return query;
}
}

View file

@ -1,4 +1,6 @@
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder} from "discord.js";
import UserEffect from "../database/entities/app/UserEffect";
import EmbedColours from "../constants/EmbedColours";
export default class EffectHelper {
public static async AddEffectToUserInventory(userId: string, name: string, quantity: number = 1) {
@ -46,4 +48,49 @@ export default class EffectHelper {
return true;
}
public static async GenerateEffectEmbed(userId: string, page: number): Promise<{
embed: EmbedBuilder,
row: ActionRowBuilder<ButtonBuilder>,
}> {
const itemsPerPage = 10;
const query = await UserEffect.FetchAllByUserIdPaginated(userId, page - 1, itemsPerPage);
const effects = query[0];
const count = query[1];
const totalPages = count > 0 ? Math.ceil(count / itemsPerPage) : 1;
let description = "*none*";
if (effects.length > 0) {
description = effects.map(x => `${x.Name} x${x.Unused}`).join("\n");
}
const embed = new EmbedBuilder()
.setTitle("Effects")
.setDescription(description)
.setColor(EmbedColours.Ok)
.setFooter({ text: `Page ${page} of ${totalPages}` });
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId(`effects list ${page - 1}`)
.setLabel("Previous")
.setStyle(ButtonStyle.Primary)
.setDisabled(page == 1),
new ButtonBuilder()
.setCustomId(`effects list ${page + 1}`)
.setLabel("Next")
.setStyle(ButtonStyle.Primary)
.setDisabled(page == totalPages),
);
return {
embed,
row,
};
}
}

View file

@ -7,6 +7,7 @@ import AllBalance from "./commands/allbalance";
import Balance from "./commands/balance";
import Daily from "./commands/daily";
import Drop from "./commands/drop";
import Effects from "./commands/effects";
import Gdrivesync from "./commands/gdrivesync";
import Give from "./commands/give";
import Id from "./commands/id";
@ -25,6 +26,7 @@ import Droprarity from "./commands/stage/droprarity";
// Button Event Imports
import Claim from "./buttonEvents/Claim";
import EffectsButtonEvent from "./buttonEvents/Effects";
import InventoryButtonEvent from "./buttonEvents/Inventory";
import MultidropButtonEvent from "./buttonEvents/Multidrop";
import Reroll from "./buttonEvents/Reroll";
@ -44,6 +46,7 @@ export default class Registry {
CoreClient.RegisterCommand("balance", new Balance());
CoreClient.RegisterCommand("daily", new Daily());
CoreClient.RegisterCommand("drop", new Drop());
CoreClient.RegisterCommand("effects", new Effects());
CoreClient.RegisterCommand("gdrivesync", new Gdrivesync());
CoreClient.RegisterCommand("give", new Give());
CoreClient.RegisterCommand("id", new Id());
@ -63,6 +66,7 @@ export default class Registry {
public static RegisterButtonEvents() {
CoreClient.RegisterButtonEvent("claim", new Claim());
CoreClient.RegisterButtonEvent("effects", new EffectsButtonEvent());
CoreClient.RegisterButtonEvent("inventory", new InventoryButtonEvent());
CoreClient.RegisterButtonEvent("multidrop", new MultidropButtonEvent());
CoreClient.RegisterButtonEvent("reroll", new Reroll());