finish button selector and fix rolesNew not responding after completing the prompt

This commit is contained in:
SadlyNotSappho 2023-03-03 13:04:41 -08:00
parent 6239e20f92
commit b45f74894f
3 changed files with 67 additions and 42 deletions

View File

@ -1,7 +1,7 @@
import { ApplyOptions } from '@sapphire/decorators';
import { Listener, ListenerOptions } from '@sapphire/framework';
import { ComponentType, GuildMember, Interaction, InteractionType } from 'discord.js';
import { RolesChecker, RolesMessage } from '../lib/types';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType, GuildMember, Interaction, InteractionType } from 'discord.js';
import { RolesMessage } from '../lib/types';
@ApplyOptions<ListenerOptions>({
event: 'interactionCreate'
@ -12,7 +12,8 @@ export class UserEvent extends Listener {
switch (interaction.componentType) {
case ComponentType.Button: {
if (interaction.customId.startsWith('showRoleSelector-')) {
switch (interaction.customId.split('-')[0]) {
case 'showRoleSelector': {
let id = interaction.customId.split('-')[1];
let data = await RolesMessage.findByPk(id);
if (!data) {
@ -27,33 +28,52 @@ export class UserEvent extends Listener {
}
}
let userRoles = (interaction.member as GuildMember).roles.cache;
let newRoles: RolesChecker[] = [];
for (const role of roles) {
if (userRoles.has(role.id)) {
newRoles.push({ role, has: true });
} else {
newRoles.push({ role, has: false });
}
}
console.log(roles);
console.log(userRoles);
console.log(newRoles);
// console.log(roles);
// console.log(userRoles);
// console.log(newRoles);
if (data.dataValues.style == 'buttons') {
let size = 5;
let arrayOfArrays = [];
for (let i = 0; i < newRoles.length; i += size) {
arrayOfArrays.push(newRoles.slice(i, i + size));
for (let i = 0; i < roles.length; i += size) {
arrayOfArrays.push(roles.slice(i, i + size));
}
// arrays with all the buttons and if the user has them are now in arrayOfArrays, figure out how to get that in a message later:tm:
let rows: ActionRowBuilder[] = [];
for (const roles of arrayOfArrays) {
let row = new ActionRowBuilder();
for (const role of roles) {
row.addComponents(
new ButtonBuilder()
.setLabel(role.name)
.setCustomId(`toggleRole-${role.id}`)
.setStyle(ButtonStyle.Primary)
);
}
rows.push(row);
}
await interaction.reply({ content: undefined, components: rows as any, ephemeral: true });
}
return;
}
case 'toggleRole': {
const id = interaction.customId.split('-')[1]
if ((interaction.member as GuildMember).roles.cache.has(id)) {
await (interaction.member as GuildMember).roles.remove(id)
await interaction.reply({content: `You already had <@&${id}>, so I removed it from you.`, ephemeral: true})
} else {
await (interaction.member as GuildMember).roles.add(id)
await interaction.reply({content: `You didn't have <@&${id}>, so I gave it to you.`, ephemeral: true})
}
}
}
}
}
return;

View File

@ -48,6 +48,9 @@ export class UserEvent extends Listener {
embedColor: {
type: DataTypes.STRING
},
buttonLabel: {
type: DataTypes.STRING
},
roles: {
type: DataTypes.ARRAY(DataTypes.STRING),
allowNull: false

View File

@ -14,6 +14,7 @@ export class UserEvent extends Listener {
let title = interaction.fields.fields.get('embedTitle')?.value;
let description = interaction.fields.fields.get('embedDescription')?.value;
let color: string = interaction.fields.fields.get('embedColor')?.value as string;
let label = interaction.fields.fields.get('buttonLabel')?.value;
let regex = /[0-9A-F]{6}/gi;
let matches = color.match(regex);
@ -47,8 +48,8 @@ export class UserEvent extends Listener {
} catch (err) {
await interaction.editReply({
content: "You didn't make a decision in time. Here's the data you put into the modal, as an embed.",
embeds: [{ title, description, color: Number(color), fields: [{ name: 'Color', value: color }] }],
components: []
embeds: [{ title, description, color: Number(color), fields: [{ name: 'Color', value: color }, {name: 'Button Label', value: `${label}`}] }],
components: [{type: 1, components: [{type: ComponentType.Button, label, customId: 'ignore', style: ButtonStyle.Primary, disabled: true}]}]
});
return null;
}
@ -92,7 +93,7 @@ export class UserEvent extends Listener {
await interaction.editReply({
content: "You didn't make a decision in time. Here's the data you put into the modal, as an embed.",
embeds: [{ title, description, color: Number(color), fields: [{ name: 'Color', value: color }] }],
components: []
components: [{type: 1, components: [{type: ComponentType.Button, label, customId: 'ignore', style: ButtonStyle.Primary, disabled: true}]}]
});
return null;
}
@ -106,11 +107,12 @@ export class UserEvent extends Listener {
embedTitle: title,
embedDescription: description,
embedColor: color,
buttonLabel: label,
roles: roles.values,
style: type == 'selectButtons' ? 'buttons' : 'dropdown'
})
console.log(data)
await interaction.editReply({content: `Alright. The roles menu has been created, with the ID ${data.dataValues.id}. Use \`/roles show ${data.dataValues.id}\` in any channel to display the menu.`, components: [], embeds: []})
return;
}