From 832f88fd859dbf4a1e7e9d7b6655e748c07b089f Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 1 Jul 2024 19:02:39 +0100 Subject: [PATCH 01/13] Add missing values to example .env --- .env.example | 6 ++++-- .gitignore | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 674b0f8..9c92b48 100644 --- a/.env.example +++ b/.env.example @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=3.2.1 +BOT_VER=3.3.0 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 @@ -23,4 +23,6 @@ DB_NAME=vylbot DB_AUTH_USER=dev DB_AUTH_PASS=dev DB_SYNC=true -DB_LOGGING=true \ No newline at end of file +DB_LOGGING=true +DB_ROOT_HOST=0.0.0.0 +DB_DATA_LOCATION=./.temp/database diff --git a/.gitignore b/.gitignore index 1707d85..c6754e1 100644 --- a/.gitignore +++ b/.gitignore @@ -105,4 +105,5 @@ dist config.json .DS_Store -ormconfig.json \ No newline at end of file +ormconfig.json +.temp/ -- 2.45.2 From 50c237f6fa59efe7098307d77695fdfcdf7494e7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 1 Jul 2024 19:03:25 +0100 Subject: [PATCH 02/13] Add database entity --- .../1719856023429-CreateMoon/Up/01-Moon.sql | 9 +++++++ src/database/entities/Moon.ts | 27 +++++++++++++++++++ .../3.3/1719856023429-CreateMoon.ts | 15 +++++++++++ 3 files changed, 51 insertions(+) create mode 100644 database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql create mode 100644 src/database/entities/Moon.ts create mode 100644 src/database/migrations/3.3/1719856023429-CreateMoon.ts diff --git a/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql new file mode 100644 index 0000000..38ef6b2 --- /dev/null +++ b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql @@ -0,0 +1,9 @@ +CREATE TABLE `moon` ( + `Id` varchar(255) NOT NULL, + `WhenCreated` datetime NOT NULL, + `WhenUpdated` datetime NOT NULL, + `ServerId` varchar(255) NOT NULL, + `UserId` varchar(255) NOT NULL, + `Description` varchar(255) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; + diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts new file mode 100644 index 0000000..cedf96f --- /dev/null +++ b/src/database/entities/Moon.ts @@ -0,0 +1,27 @@ +import { Column, Entity } from "typeorm"; +import BaseEntity from "../../contracts/BaseEntity"; +import AppDataSource from "../dataSources/appDataSource"; + +@Entity() +export default class Moon extends BaseEntity { + constructor(description: string, userId: string) { + super(); + + this.Description = description; + this.UserId = userId; + } + + @Column() + Description: string; + + @Column() + UserId: string; + + public static async FetchMoonsByUserId(userId: string): Promise { + const repository = AppDataSource.getRepository(Moon); + + const all = await repository.find({ where: { UserId: userId } }); + + return all; + } +} diff --git a/src/database/migrations/3.3/1719856023429-CreateMoon.ts b/src/database/migrations/3.3/1719856023429-CreateMoon.ts new file mode 100644 index 0000000..0ccc820 --- /dev/null +++ b/src/database/migrations/3.3/1719856023429-CreateMoon.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm" +import MigrationHelper from "../../../helpers/MigrationHelper" + +export class CreateMoon1719856023429 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + MigrationHelper.Up('1719856023429-CreateMoon', '3.3.0', [ + "01-Moon", + ], queryRunner); + } + + public async down(queryRunner: QueryRunner): Promise { + } + +} -- 2.45.2 From 7b620dfd90c2439833e785cd572b24fa642c4981 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 1 Jul 2024 19:03:49 +0100 Subject: [PATCH 03/13] WIP: Create base moon command --- src/commands/304276391837302787/moons.ts | 27 +++++++++++++++++++ src/commands/304276391837302787/moons/list.ts | 5 ++++ 2 files changed, 32 insertions(+) create mode 100644 src/commands/304276391837302787/moons.ts create mode 100644 src/commands/304276391837302787/moons/list.ts diff --git a/src/commands/304276391837302787/moons.ts b/src/commands/304276391837302787/moons.ts new file mode 100644 index 0000000..1556d97 --- /dev/null +++ b/src/commands/304276391837302787/moons.ts @@ -0,0 +1,27 @@ +import { Command } from "../../type/command"; +import { CommandInteraction, SlashCommandBuilder } from "discord.js"; +import ListMoons from "./moons/list"; + +export default class Moons extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName("moons") + .setDescription("View and create moons") + .addSubcommand(subcommand => + subcommand + .setName('list') + .setDescription('List moons you have obtained')); + } + + public override async execute(interaction: CommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + switch (interaction.options.getSubcommand()) { + case "list": + await ListMoons(interaction); + break; + } + } +} diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts new file mode 100644 index 0000000..023fa89 --- /dev/null +++ b/src/commands/304276391837302787/moons/list.ts @@ -0,0 +1,5 @@ +import {CommandInteraction} from "discord.js"; + +export default async function ListMoons(interaction: CommandInteraction) { + +} -- 2.45.2 From 690b63470a3d99cd98b0fd8824c390ef0b83d401 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 3 Jul 2024 20:50:47 +0100 Subject: [PATCH 04/13] Create lobby command --- .../1719856023429-CreateMoon/Up/01-Moon.sql | 2 +- src/commands/304276391837302787/moons/list.ts | 19 ++++++++++++++++++- src/database/entities/Moon.ts | 6 +++++- src/registry.ts | 7 +++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql index 38ef6b2..a2afcbf 100644 --- a/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql +++ b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql @@ -2,7 +2,7 @@ CREATE TABLE `moon` ( `Id` varchar(255) NOT NULL, `WhenCreated` datetime NOT NULL, `WhenUpdated` datetime NOT NULL, - `ServerId` varchar(255) NOT NULL, + `MoonNumber` int NOT NULL, `UserId` varchar(255) NOT NULL, `Description` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 023fa89..d862d5c 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -1,5 +1,22 @@ -import {CommandInteraction} from "discord.js"; +import {CommandInteraction, EmbedBuilder} from "discord.js"; +import Moon from "../../../database/entities/Moon"; +import EmbedColours from "../../../constants/EmbedColours"; export default async function ListMoons(interaction: CommandInteraction) { + const moons = await Moon.FetchMoonsByUserId(interaction.user.id); + if (!moons || moons.length == 0) { + await interaction.reply("You do not have any moons."); + return; + } + + const description = moons.flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); + + const embed = new EmbedBuilder() + .setTitle(`${interaction.user.username}'s Moons`) + .setColor(EmbedColours.Ok) + .setDescription(description.join("\n")) + .setFooter({ text: `${moons.length} moons` }); + + await interaction.reply({ embeds: [ embed ] }); } diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index cedf96f..feb2cbb 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -4,12 +4,16 @@ import AppDataSource from "../dataSources/appDataSource"; @Entity() export default class Moon extends BaseEntity { - constructor(description: string, userId: string) { + constructor(moonNumber: number, description: string, userId: string) { super(); + this.MoonNumber = moonNumber; this.Description = description; this.UserId = userId; } + + @Column() + MoonNumber: number; @Column() Description: string; diff --git a/src/registry.ts b/src/registry.ts index 68428de..703bb9f 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -30,6 +30,9 @@ import AddLobby from "./commands/501231711271780357/Lobby/add"; import RemoveLobby from "./commands/501231711271780357/Lobby/remove"; import ListLobby from "./commands/501231711271780357/Lobby/list"; +// Command Imports: Potato Talk +import Moons from "./commands/304276391837302787/moons"; + // Event Imports import GuildMemberAdd from "./events/MemberEvents/GuildMemberAdd"; import GuildMemberRemove from "./events/MemberEvents/GuildMemberRemove"; @@ -72,12 +75,16 @@ export default class Registry { CoreClient.RegisterCommand("listlobby", new ListLobby(), "501231711271780357"); CoreClient.RegisterCommand("entry", new Entry(), "501231711271780357"); + // Exclusive Commands: Potato Talk + CoreClient.RegisterCommand("moons", new Moons(), "304276391837302787"); + // Add Exclusive Commands to Test Server CoreClient.RegisterCommand("lobby", new Lobby(), "442730357897429002"); CoreClient.RegisterCommand("addlobby", new AddLobby(), "442730357897429002"); CoreClient.RegisterCommand("removelobby", new RemoveLobby(), "442730357897429002"); CoreClient.RegisterCommand("listlobby", new ListLobby(), "442730357897429002"); CoreClient.RegisterCommand("entry", new Entry(), "442730357897429002"); + CoreClient.RegisterCommand("moons", new Moons(), "442730357897429002"); } public static RegisterEvents() { -- 2.45.2 From 6f4ff1df0622aaa8e3429b31b0d2647ab2dd95ba Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 4 Jul 2024 18:56:05 +0100 Subject: [PATCH 05/13] Add user option --- src/commands/304276391837302787/moons.ts | 6 +++++- src/commands/304276391837302787/moons/list.ts | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/commands/304276391837302787/moons.ts b/src/commands/304276391837302787/moons.ts index 1556d97..5c8fc85 100644 --- a/src/commands/304276391837302787/moons.ts +++ b/src/commands/304276391837302787/moons.ts @@ -12,7 +12,11 @@ export default class Moons extends Command { .addSubcommand(subcommand => subcommand .setName('list') - .setDescription('List moons you have obtained')); + .setDescription('List moons you have obtained') + .addUserOption(option => + option + .setName("user") + .setDescription("The user to view (Defaults to yourself)"))); } public override async execute(interaction: CommandInteraction) { diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index d862d5c..b359f2f 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -3,17 +3,19 @@ import Moon from "../../../database/entities/Moon"; import EmbedColours from "../../../constants/EmbedColours"; export default async function ListMoons(interaction: CommandInteraction) { - const moons = await Moon.FetchMoonsByUserId(interaction.user.id); + const user = interaction.options.get("user")?.user ?? interaction.user; + + const moons = await Moon.FetchMoonsByUserId(user.id); if (!moons || moons.length == 0) { - await interaction.reply("You do not have any moons."); + await interaction.reply(`${user.username} does not have any moons.`); return; } const description = moons.flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); const embed = new EmbedBuilder() - .setTitle(`${interaction.user.username}'s Moons`) + .setTitle(`${user.username}'s Moons`) .setColor(EmbedColours.Ok) .setDescription(description.join("\n")) .setFooter({ text: `${moons.length} moons` }); -- 2.45.2 From 673b258fa999eea7aec55cb94ef83becafcd211c Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 4 Jul 2024 19:21:52 +0100 Subject: [PATCH 06/13] Add pagination buttons --- src/commands/304276391837302787/moons.ts | 6 +++- src/commands/304276391837302787/moons/list.ts | 32 +++++++++++++++---- src/constants/MoonValues.ts | 3 ++ src/database/entities/Moon.ts | 16 ++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/constants/MoonValues.ts diff --git a/src/commands/304276391837302787/moons.ts b/src/commands/304276391837302787/moons.ts index 5c8fc85..13f0f98 100644 --- a/src/commands/304276391837302787/moons.ts +++ b/src/commands/304276391837302787/moons.ts @@ -16,7 +16,11 @@ export default class Moons extends Command { .addUserOption(option => option .setName("user") - .setDescription("The user to view (Defaults to yourself)"))); + .setDescription("The user to view (Defaults to yourself)")) + .addNumberOption(option => + option + .setName("page") + .setDescription("The page to start with"))); } public override async execute(interaction: CommandInteraction) { diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index b359f2f..f568ff5 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -1,18 +1,22 @@ -import {CommandInteraction, EmbedBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js"; import Moon from "../../../database/entities/Moon"; import EmbedColours from "../../../constants/EmbedColours"; +import MoonValues from "../../../constants/MoonValues"; export default async function ListMoons(interaction: CommandInteraction) { const user = interaction.options.get("user")?.user ?? interaction.user; + const page = interaction.options.get("page")?.value as number ?? 0; - const moons = await Moon.FetchMoonsByUserId(user.id); + const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, page); - if (!moons || moons.length == 0) { - await interaction.reply(`${user.username} does not have any moons.`); + if (!moons || moons[0].length == 0) { + await interaction.reply(`${user.username} does not have any moons or page is invalid.`); return; } - const description = moons.flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); + const totalPages = Math.ceil(moons[1] / MoonValues.ListPageLength); + + const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); const embed = new EmbedBuilder() .setTitle(`${user.username}'s Moons`) @@ -20,5 +24,21 @@ export default async function ListMoons(interaction: CommandInteraction) { .setDescription(description.join("\n")) .setFooter({ text: `${moons.length} moons` }); - await interaction.reply({ embeds: [ embed ] }); + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`moons ${user} ${page - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == 0), + new ButtonBuilder() + .setCustomId(`moons ${user.id} ${page + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(page + 1 == totalPages)); + + await interaction.reply({ + embeds: [ embed ], + components: [ row ], + }); } diff --git a/src/constants/MoonValues.ts b/src/constants/MoonValues.ts new file mode 100644 index 0000000..52b49c8 --- /dev/null +++ b/src/constants/MoonValues.ts @@ -0,0 +1,3 @@ +export default class MoonValues { + public static readonly ListPageLength = 10; +} diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index feb2cbb..e246f8c 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -1,6 +1,7 @@ import { Column, Entity } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; import AppDataSource from "../dataSources/appDataSource"; +import MoonValues from "../../constants/MoonValues"; @Entity() export default class Moon extends BaseEntity { @@ -28,4 +29,19 @@ export default class Moon extends BaseEntity { return all; } + + public static async FetchPaginatedMoonsByUserId(userId: string, page: number): Promise<[ Moon[], number ]> { + const rangeStart = page * MoonValues.ListPageLength; + + const repository = AppDataSource.getRepository(Moon); + + const moons = await repository.findAndCount({ + where: { UserId: userId }, + order: { MoonNumber: "ASC" }, + skip: rangeStart, + take: MoonValues.ListPageLength, + }); + + return moons; + } } -- 2.45.2 From 084b8aea792ed1421863ab40fe91a4bc9c495476 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 6 Aug 2024 18:37:19 +0100 Subject: [PATCH 07/13] Rename MoonValues to MoonConstants --- src/commands/304276391837302787/moons/list.ts | 4 ++-- src/constants/{MoonValues.ts => MoonConstants.ts} | 2 +- src/database/entities/Moon.ts | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/constants/{MoonValues.ts => MoonConstants.ts} (57%) diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index f568ff5..6339d03 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -1,7 +1,7 @@ import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js"; import Moon from "../../../database/entities/Moon"; import EmbedColours from "../../../constants/EmbedColours"; -import MoonValues from "../../../constants/MoonValues"; +import MoonConstants from "../../../constants/MoonConstants"; export default async function ListMoons(interaction: CommandInteraction) { const user = interaction.options.get("user")?.user ?? interaction.user; @@ -14,7 +14,7 @@ export default async function ListMoons(interaction: CommandInteraction) { return; } - const totalPages = Math.ceil(moons[1] / MoonValues.ListPageLength); + const totalPages = Math.ceil(moons[1] / MoonConstants.ListPageLength); const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); diff --git a/src/constants/MoonValues.ts b/src/constants/MoonConstants.ts similarity index 57% rename from src/constants/MoonValues.ts rename to src/constants/MoonConstants.ts index 52b49c8..17f2340 100644 --- a/src/constants/MoonValues.ts +++ b/src/constants/MoonConstants.ts @@ -1,3 +1,3 @@ -export default class MoonValues { +export default class MoonConstants { public static readonly ListPageLength = 10; } diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index e246f8c..02ebd30 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -1,7 +1,7 @@ import { Column, Entity } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; import AppDataSource from "../dataSources/appDataSource"; -import MoonValues from "../../constants/MoonValues"; +import MoonConstants from "../../constants/MoonConstants"; @Entity() export default class Moon extends BaseEntity { @@ -31,7 +31,7 @@ export default class Moon extends BaseEntity { } public static async FetchPaginatedMoonsByUserId(userId: string, page: number): Promise<[ Moon[], number ]> { - const rangeStart = page * MoonValues.ListPageLength; + const rangeStart = page * MoonConstants.ListPageLength; const repository = AppDataSource.getRepository(Moon); @@ -39,7 +39,7 @@ export default class Moon extends BaseEntity { where: { UserId: userId }, order: { MoonNumber: "ASC" }, skip: rangeStart, - take: MoonValues.ListPageLength, + take: MoonConstants.ListPageLength, }); return moons; -- 2.45.2 From 4ef8f0edd0c803a54874242f980324ff7cc75915 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 12 Aug 2024 16:31:52 +0100 Subject: [PATCH 08/13] Remove need for MoonConstants file --- src/commands/304276391837302787/moons/list.ts | 7 ++++--- src/constants/MoonConstants.ts | 3 --- src/database/entities/Moon.ts | 7 +++---- 3 files changed, 7 insertions(+), 10 deletions(-) delete mode 100644 src/constants/MoonConstants.ts diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 6339d03..35a14d6 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -1,20 +1,21 @@ import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js"; import Moon from "../../../database/entities/Moon"; import EmbedColours from "../../../constants/EmbedColours"; -import MoonConstants from "../../../constants/MoonConstants"; export default async function ListMoons(interaction: CommandInteraction) { const user = interaction.options.get("user")?.user ?? interaction.user; const page = interaction.options.get("page")?.value as number ?? 0; - const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, page); + const pageLength = 10; + + const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, pageLength, page); if (!moons || moons[0].length == 0) { await interaction.reply(`${user.username} does not have any moons or page is invalid.`); return; } - const totalPages = Math.ceil(moons[1] / MoonConstants.ListPageLength); + const totalPages = Math.ceil(moons[1] / pageLength); const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); diff --git a/src/constants/MoonConstants.ts b/src/constants/MoonConstants.ts deleted file mode 100644 index 17f2340..0000000 --- a/src/constants/MoonConstants.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default class MoonConstants { - public static readonly ListPageLength = 10; -} diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index 02ebd30..b139530 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -1,7 +1,6 @@ import { Column, Entity } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; import AppDataSource from "../dataSources/appDataSource"; -import MoonConstants from "../../constants/MoonConstants"; @Entity() export default class Moon extends BaseEntity { @@ -30,8 +29,8 @@ export default class Moon extends BaseEntity { return all; } - public static async FetchPaginatedMoonsByUserId(userId: string, page: number): Promise<[ Moon[], number ]> { - const rangeStart = page * MoonConstants.ListPageLength; + public static async FetchPaginatedMoonsByUserId(userId: string, pageLength: number, page: number): Promise<[ Moon[], number ]> { + const rangeStart = page * pageLength; const repository = AppDataSource.getRepository(Moon); @@ -39,7 +38,7 @@ export default class Moon extends BaseEntity { where: { UserId: userId }, order: { MoonNumber: "ASC" }, skip: rangeStart, - take: MoonConstants.ListPageLength, + take: pageLength, }); return moons; -- 2.45.2 From dc3cb936537326b868d76235e2917d05b4828954 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 14 Aug 2024 18:21:12 +0100 Subject: [PATCH 09/13] Add moon button event --- src/buttonEvents/moons.ts | 14 +++++ src/buttonEvents/moons/list.ts | 54 +++++++++++++++++++ src/commands/304276391837302787/moons/list.ts | 4 +- src/registry.ts | 2 + 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/buttonEvents/moons.ts create mode 100644 src/buttonEvents/moons/list.ts diff --git a/src/buttonEvents/moons.ts b/src/buttonEvents/moons.ts new file mode 100644 index 0000000..373e30c --- /dev/null +++ b/src/buttonEvents/moons.ts @@ -0,0 +1,14 @@ +import {ButtonInteraction} from "discord.js"; +import {ButtonEvent} from "../type/buttonEvent"; +import List from "./moons/list"; + +export default class Moons extends ButtonEvent { + public override async execute(interaction: ButtonInteraction): Promise { + const action = interaction.customId.split(" ")[0]; + + switch (action) { + case "list": + await List(interaction); + } + } +} diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts new file mode 100644 index 0000000..fe5ae21 --- /dev/null +++ b/src/buttonEvents/moons/list.ts @@ -0,0 +1,54 @@ +import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder} from "discord.js"; +import Moon from "../../database/entities/Moon"; +import EmbedColours from "../../constants/EmbedColours"; + +export default async function List(interaction: ButtonInteraction) { + if (!interaction.guild) return; + + const userId = interaction.customId.split(" ")[1]; + const page = interaction.customId.split(" ")[2]; + + if (!userId || !page) return; + if (!Number(page)) return; + + const pageNumber = Number(page); + + const member = interaction.guild.members.cache.find(x => x.user.id == userId); + + const pageLength = 10; + + const moons = await Moon.FetchPaginatedMoonsByUserId(userId, pageLength, pageNumber); + + if (!moons || moons[0].length == 0) { + await interaction.reply(`${member?.user.username ?? "This user"} does not have any moons or page is invalid.`); + return; + } + + const totalPages = Math.ceil(moons[1] / pageLength); + + const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); + + const embed = new EmbedBuilder() + .setTitle(`${member?.user.username}'s Moons`) + .setColor(EmbedColours.Ok) + .setDescription(description.join("\n")) + .setFooter({ text: `${moons.length} moons` }); + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`moons list ${userId} ${pageNumber - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(pageNumber == 0), + new ButtonBuilder() + .setCustomId(`moons list ${userId} ${pageNumber + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(pageNumber + 1 == totalPages)); + + await interaction.update({ + embeds: [ embed ], + components: [ row ], + }); +} diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 35a14d6..5cddffb 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -28,12 +28,12 @@ export default async function ListMoons(interaction: CommandInteraction) { const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() - .setCustomId(`moons ${user} ${page - 1}`) + .setCustomId(`moons list ${user.id} ${page - 1}`) .setLabel("Previous") .setStyle(ButtonStyle.Primary) .setDisabled(page == 0), new ButtonBuilder() - .setCustomId(`moons ${user.id} ${page + 1}`) + .setCustomId(`moons list ${user.id} ${page + 1}`) .setLabel("Next") .setStyle(ButtonStyle.Primary) .setDisabled(page + 1 == totalPages)); diff --git a/src/registry.ts b/src/registry.ts index bfdd0ba..6c80edb 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -41,6 +41,7 @@ import MessageCreate from "./events/MessageEvents/MessageCreate"; // Button Event Imports import Verify from "./buttonEvents/verify"; +import MoonsButtonEvent from "./buttonEvents/moons"; export default class Registry { public static RegisterCommands() { @@ -95,5 +96,6 @@ export default class Registry { public static RegisterButtonEvents() { CoreClient.RegisterButtonEvent("verify", new Verify()); + CoreClient.RegisterButtonEvent("moons", new MoonsButtonEvent()); } } -- 2.45.2 From 736cbe0ac19d4c57915ae738e7d8741652023414 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 15 Aug 2024 19:21:56 +0100 Subject: [PATCH 10/13] Fix button events not working --- src/buttonEvents/moons.ts | 2 +- src/buttonEvents/moons/list.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/buttonEvents/moons.ts b/src/buttonEvents/moons.ts index 373e30c..726f209 100644 --- a/src/buttonEvents/moons.ts +++ b/src/buttonEvents/moons.ts @@ -4,7 +4,7 @@ import List from "./moons/list"; export default class Moons extends ButtonEvent { public override async execute(interaction: ButtonInteraction): Promise { - const action = interaction.customId.split(" ")[0]; + const action = interaction.customId.split(" ")[1]; switch (action) { case "list": diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts index fe5ae21..0c89de1 100644 --- a/src/buttonEvents/moons/list.ts +++ b/src/buttonEvents/moons/list.ts @@ -5,11 +5,10 @@ import EmbedColours from "../../constants/EmbedColours"; export default async function List(interaction: ButtonInteraction) { if (!interaction.guild) return; - const userId = interaction.customId.split(" ")[1]; - const page = interaction.customId.split(" ")[2]; + const userId = interaction.customId.split(" ")[2]; + const page = interaction.customId.split(" ")[3]; if (!userId || !page) return; - if (!Number(page)) return; const pageNumber = Number(page); -- 2.45.2 From afbee94a75f2367bf011b7863a123f1dd94a9928 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 15 Aug 2024 19:27:17 +0100 Subject: [PATCH 11/13] Add WhenArchived column --- database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql | 1 + src/database/entities/Moon.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql index a2afcbf..e7aecab 100644 --- a/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql +++ b/database/3.3.0/1719856023429-CreateMoon/Up/01-Moon.sql @@ -5,5 +5,6 @@ CREATE TABLE `moon` ( `MoonNumber` int NOT NULL, `UserId` varchar(255) NOT NULL, `Description` varchar(255) NOT NULL + `WhenArchived` datetime NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index b139530..566d1d4 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -1,4 +1,4 @@ -import { Column, Entity } from "typeorm"; +import { Column, Entity, IsNull } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; import AppDataSource from "../dataSources/appDataSource"; @@ -18,6 +18,9 @@ export default class Moon extends BaseEntity { @Column() Description: string; + @Column({ nullable: true }) + WhenArchived?: Date; + @Column() UserId: string; @@ -35,7 +38,7 @@ export default class Moon extends BaseEntity { const repository = AppDataSource.getRepository(Moon); const moons = await repository.findAndCount({ - where: { UserId: userId }, + where: { UserId: userId, WhenArchived: IsNull() }, order: { MoonNumber: "ASC" }, skip: rangeStart, take: pageLength, -- 2.45.2 From 89b3af952321deba1e355a521c809a9ec837e42b Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 15 Aug 2024 19:29:08 +0100 Subject: [PATCH 12/13] Fix moon count always saying 2 --- src/buttonEvents/moons/list.ts | 2 +- src/commands/304276391837302787/moons/list.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts index 0c89de1..fc20e5e 100644 --- a/src/buttonEvents/moons/list.ts +++ b/src/buttonEvents/moons/list.ts @@ -31,7 +31,7 @@ export default async function List(interaction: ButtonInteraction) { .setTitle(`${member?.user.username}'s Moons`) .setColor(EmbedColours.Ok) .setDescription(description.join("\n")) - .setFooter({ text: `${moons.length} moons` }); + .setFooter({ text: `${moons[1]} moons` }); const row = new ActionRowBuilder() .addComponents( diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 5cddffb..fd209a4 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -23,7 +23,7 @@ export default async function ListMoons(interaction: CommandInteraction) { .setTitle(`${user.username}'s Moons`) .setColor(EmbedColours.Ok) .setDescription(description.join("\n")) - .setFooter({ text: `${moons.length} moons` }); + .setFooter({ text: `${moons[1]} moons` }); const row = new ActionRowBuilder() .addComponents( -- 2.45.2 From e7527abdf9d9f021f11883243782c024b00e9f17 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 15 Aug 2024 19:31:14 +0100 Subject: [PATCH 13/13] Add page number --- src/buttonEvents/moons/list.ts | 2 +- src/commands/304276391837302787/moons/list.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts index fc20e5e..9309dbe 100644 --- a/src/buttonEvents/moons/list.ts +++ b/src/buttonEvents/moons/list.ts @@ -31,7 +31,7 @@ export default async function List(interaction: ButtonInteraction) { .setTitle(`${member?.user.username}'s Moons`) .setColor(EmbedColours.Ok) .setDescription(description.join("\n")) - .setFooter({ text: `${moons[1]} moons` }); + .setFooter({ text: `Page ${page + 1} of ${totalPages} · ${moons[1]} moons` }); const row = new ActionRowBuilder() .addComponents( diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index fd209a4..8727124 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -23,7 +23,7 @@ export default async function ListMoons(interaction: CommandInteraction) { .setTitle(`${user.username}'s Moons`) .setColor(EmbedColours.Ok) .setDescription(description.join("\n")) - .setFooter({ text: `${moons[1]} moons` }); + .setFooter({ text: `Page ${page + 1} of ${totalPages} · ${moons[1]} moons` }); const row = new ActionRowBuilder() .addComponents( -- 2.45.2