use unique ID for buttons

This commit is contained in:
Mylloon 2023-01-16 19:13:47 +01:00
parent 240b0d0422
commit 9cb94f5f0a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
6 changed files with 57 additions and 11 deletions

29
package-lock.json generated
View file

@ -14,9 +14,11 @@
"discord-api-types": "^0.36.3", "discord-api-types": "^0.36.3",
"discord.js": "^14.3.0", "discord.js": "^14.3.0",
"sqlite3": "^5.0.11", "sqlite3": "^5.0.11",
"typescript": "^4.7.4" "typescript": "^4.7.4",
"uuid": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.7", "@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7", "@typescript-eslint/parser": "^5.30.7",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
@ -367,6 +369,12 @@
"integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==", "integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==",
"dev": true "dev": true
}, },
"node_modules/@types/uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-kr90f+ERiQtKWMz5rP32ltJ/BtULDI5RVO0uavn1HQUOwjx0R1h0rnDYNL0CepF1zL5bSY6FISAfd9tOdDhU5Q==",
"dev": true
},
"node_modules/@types/ws": { "node_modules/@types/ws": {
"version": "8.5.4", "version": "8.5.4",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.4.tgz", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.4.tgz",
@ -3179,6 +3187,14 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
}, },
"node_modules/uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/v8-compile-cache-lib": { "node_modules/v8-compile-cache-lib": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
@ -3575,6 +3591,12 @@
"integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==", "integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ==",
"dev": true "dev": true
}, },
"@types/uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-kr90f+ERiQtKWMz5rP32ltJ/BtULDI5RVO0uavn1HQUOwjx0R1h0rnDYNL0CepF1zL5bSY6FISAfd9tOdDhU5Q==",
"dev": true
},
"@types/ws": { "@types/ws": {
"version": "8.5.4", "version": "8.5.4",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.4.tgz", "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.4.tgz",
@ -5592,6 +5614,11 @@
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
}, },
"uuid": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.0.tgz",
"integrity": "sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg=="
},
"v8-compile-cache-lib": { "v8-compile-cache-lib": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",

View file

@ -20,9 +20,11 @@
"discord-api-types": "^0.36.3", "discord-api-types": "^0.36.3",
"discord.js": "^14.3.0", "discord.js": "^14.3.0",
"sqlite3": "^5.0.11", "sqlite3": "^5.0.11",
"typescript": "^4.7.4" "typescript": "^4.7.4",
"uuid": "^9.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/uuid": "^9.0.0",
"@typescript-eslint/eslint-plugin": "^5.30.7", "@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7", "@typescript-eslint/parser": "^5.30.7",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",

View file

@ -43,9 +43,9 @@ export default async (client: Client) => {
* @param id Button ID * @param id Button ID
* @param deferUpdate defer update in case update take time * @param deferUpdate defer update in case update take time
*/ */
export const collect = (client: Client, interaction: ChatInputCommandInteraction, id: string, deferUpdate = false) => { export const collect = (client: Client, interaction: ChatInputCommandInteraction | MessageComponentInteraction, id: string, deferUpdate = false) => {
const loc = getLocale(client, interaction.locale); const loc = getLocale(client, interaction.locale);
const button = client.buttons.list.get(id); const button = client.buttons.list.get(id.split('_')[0]);
if (!button) { if (!button) {
interaction.reply({ interaction.reply({
@ -55,7 +55,7 @@ export const collect = (client: Client, interaction: ChatInputCommandInteraction
} }
const filter = (i: MessageComponentInteraction) => i.customId === id; const filter = (i: MessageComponentInteraction) => i.customId === id;
const collector = interaction.channel?.createMessageComponentCollector({ filter }); const collector = interaction.channel?.createMessageComponentCollector({ filter, max: 1 });
collector?.on('collect', async (i) => { collector?.on('collect', async (i) => {
if (deferUpdate) { if (deferUpdate) {
await i.deferUpdate(); await i.deferUpdate();

View file

@ -1,7 +1,9 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js';
import { v4 as uuidv4 } from 'uuid';
import { getLocale } from '../../utils/locales'; import { getLocale } from '../../utils/locales';
import { getFilename } from '../../utils/misc'; import { getFilename } from '../../utils/misc';
import { embedListReminders } from '../../utils/reminder'; import { embedListReminders } from '../../utils/reminder';
import { collect } from '../loader';
export default { export default {
data: { data: {
@ -27,19 +29,25 @@ export default {
// Fetch list // Fetch list
const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale);
const idPrec = 'reminderList-prec_' + uuidv4();
const idNext = 'reminderList-next_' + uuidv4();
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId('reminderList-prec') .setCustomId(idPrec)
.setLabel(loc.get('c_reminder12')) .setLabel(loc.get('c_reminder12'))
.setStyle(ButtonStyle.Primary)) .setStyle(ButtonStyle.Primary))
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId('reminderList-next') .setCustomId(idNext)
.setLabel(loc.get('c_reminder13')) .setLabel(loc.get('c_reminder13'))
.setStyle(ButtonStyle.Primary), .setStyle(ButtonStyle.Primary),
); );
// Buttons interactions
collect(client, interaction, idPrec);
collect(client, interaction, idNext);
return { return {
embeds: [list], embeds: [list],
components: [row], components: [row],

View file

@ -1,7 +1,9 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, Client, MessageComponentInteraction, User } from 'discord.js';
import { v4 as uuidv4 } from 'uuid';
import { getLocale } from '../../utils/locales'; import { getLocale } from '../../utils/locales';
import { getFilename } from '../../utils/misc'; import { getFilename } from '../../utils/misc';
import { embedListReminders } from '../../utils/reminder'; import { embedListReminders } from '../../utils/reminder';
import { collect } from '../loader';
export default { export default {
data: { data: {
@ -27,19 +29,25 @@ export default {
// Fetch list // Fetch list
const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale);
const idPrec = 'reminderList-prec_' + uuidv4();
const idNext = 'reminderList-next_' + uuidv4();
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId('reminderList-prec') .setCustomId(idPrec)
.setLabel(loc.get('c_reminder12')) .setLabel(loc.get('c_reminder12'))
.setStyle(ButtonStyle.Primary)) .setStyle(ButtonStyle.Primary))
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId('reminderList-next') .setCustomId(idNext)
.setLabel(loc.get('c_reminder13')) .setLabel(loc.get('c_reminder13'))
.setStyle(ButtonStyle.Primary), .setStyle(ButtonStyle.Primary),
); );
// Buttons interactions
collect(client, interaction, idPrec);
collect(client, interaction, idNext);
return { return {
embeds: [list], embeds: [list],
components: [row], components: [row],

View file

@ -1,5 +1,6 @@
import { ModalActionRowComponentBuilder, SlashCommandBuilder } from '@discordjs/builders'; import { ModalActionRowComponentBuilder, SlashCommandBuilder } from '@discordjs/builders';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, Client, ModalBuilder, TextInputBuilder, TextInputStyle } from 'discord.js'; import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, Client, ModalBuilder, TextInputBuilder, TextInputStyle } from 'discord.js';
import { v4 as uuidv4 } from 'uuid';
import { collect } from '../../buttons/loader'; import { collect } from '../../buttons/loader';
import { getLocale, getLocalizations } from '../../utils/locales'; import { getLocale, getLocalizations } from '../../utils/locales';
import { getFilename } from '../../utils/misc'; import { getFilename } from '../../utils/misc';
@ -222,8 +223,8 @@ export default {
const page = interaction.options.getInteger(loc_default?.get(`c_${filename}_sub2_opt2_name`) as string) ?? 1; const page = interaction.options.getInteger(loc_default?.get(`c_${filename}_sub2_opt2_name`) as string) ?? 1;
const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale); const list = await embedListReminders(client, user, interaction.guildId, page, interaction.locale);
const idPrec = 'reminderList-prec'; const idPrec = 'reminderList-prec_' + uuidv4();
const idNext = 'reminderList-next'; const idNext = 'reminderList-next_' + uuidv4();
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()