PiggyPlex
PiggyPlex
DIAdiscord.js - Imagine an app
Created by PiggyPlex on 5/11/2024 in #djs-questions
Roles not adding correctly
// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
Hi, I have this strange problem. If I run the command /sync in server #1, the roles are added correctly in server #1, however they are not added to server #2. The converse is also true. Could anyone help me out? No errors. Using discord.js 14.15.3-dev.1715429013-7816ec2e6.
3 replies
DIAdiscord.js - Imagine an app
Created by PiggyPlex on 5/11/2024 in #djs-questions
Roles not adding correctly
// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
// Member is a GuildMember, passed from the interaction's interaction.member. i.e.:
const member = interaction.member;
// Example of rolesToAdd:
const rolesToAdd = new Map([
['server id #1', new Set(['role id #1', ...])],
['server id #2', new Set(['role id #1', ...])],
]);
for await (const [guildId, roleIds] of Object.entries(rolesToAdd)) {
if (!roleIds.size) {
container.logger.debug(`Skipped adding roles to user with id ${member.id} in guild with id ${guildId} as they already have them`);
continue;
}
const guild = await container.client.guilds.fetch(guildId);
if (!guild) {
container.logger.warn(`Couldn't add roles to a user while syncing: guild with id ${guildId} not found.`);
continue;
}
const guildMember = await guild.members.fetch(member.id);
if (!guildMember) {
container.logger.warn(`Couldn't add roles to a user while syncing: user with id ${member.id} not found in guild with id ${guildId}.`);
continue;
}
await guildMember.roles.add([...roleIds], `[Sync] Adding staff role`)
.catch((error) => {
container.logger.warn(`Failed to add roles to user with id ${member.id} in guild with id ${guildId}:`, error);
});
}
Hi, I have this strange problem. If I run the command /sync in server #1, the roles are added correctly in server #1, however they are not added to server #2. The converse is also true. Could anyone help me out? No errors. Using discord.js 14.15.3-dev.1715429013-7816ec2e6.
2 replies
DIAdiscord.js - Imagine an app
Created by PiggyPlex on 5/11/2024 in #djs-questions
Nested message component collector not firing
Can anyone help me with this? Response from second button not firing
private async handleComponentInteraction(target: GuildMember, initialInteraction: InitialInteraction, interaction: MessageComponentInteraction) {
if (interaction.isButton()) {
switch (interaction.customId) {
case 'manage-demote':
await this.handleDemoteButton(target, interaction);
break;
default:
break;
}
}
}

private async handleDemoteButton(target: GuildMember, interaction: MessageComponentInteraction) {
// this sends
const reply = await interaction.reply({
embeds: [createErrorEmbed('Danger Zone', 'Are you sure you want to demote this user?')],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId('manage-demote-confirm')
.setLabel('Yes, demote them')
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId('manage-demote-cancel')
.setLabel('Cancel')
.setStyle(ButtonStyle.Secondary),
),
],
});
// likewise, I have also tried awaitMessageComponent
const collector = reply.createMessageComponentCollector({
filter: (componentInteraction) => componentInteraction.user.id === interaction.user.id,
time: 30 * 1000,
});
collector.on('collect', async (componentInteraction) => {
// ... never fires
});
}
private async handleComponentInteraction(target: GuildMember, initialInteraction: InitialInteraction, interaction: MessageComponentInteraction) {
if (interaction.isButton()) {
switch (interaction.customId) {
case 'manage-demote':
await this.handleDemoteButton(target, interaction);
break;
default:
break;
}
}
}

private async handleDemoteButton(target: GuildMember, interaction: MessageComponentInteraction) {
// this sends
const reply = await interaction.reply({
embeds: [createErrorEmbed('Danger Zone', 'Are you sure you want to demote this user?')],
components: [
new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId('manage-demote-confirm')
.setLabel('Yes, demote them')
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId('manage-demote-cancel')
.setLabel('Cancel')
.setStyle(ButtonStyle.Secondary),
),
],
});
// likewise, I have also tried awaitMessageComponent
const collector = reply.createMessageComponentCollector({
filter: (componentInteraction) => componentInteraction.user.id === interaction.user.id,
time: 30 * 1000,
});
collector.on('collect', async (componentInteraction) => {
// ... never fires
});
}
24 replies
DIAdiscord.js - Imagine an app
Created by PiggyPlex on 6/3/2023 in #djs-questions
Error Handling?
I want to supress only certain types of errors. - Where can I find the documentation for different error.code values? - Where can I find the structure of errors returned by Discord.js? - What are the best practices for handling discord.js errors?
46 replies