Disabling A Button Through CustomId(handler)

const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ButtonStyle, ButtonBuilder, ActionRowBuilder } = require('discord.js');

module.exports = {
customId: "urgent-ping",
async execute(interaction, client) {

if (interaction.user.id !== interaction.channel.ownerId) {
return await interaction.reply({ content: 'You are not the owner of this thread!', ephemeral: true });
}

const staffRole = interaction.guild.roles.cache.get('1325170122134130779');
const helpfulRole = interaction.guild.roles.cache.get('1325170416020357282');

const membersToPing = interaction.guild.members.cache.filter(member =>
(member.roles.cache.has(staffRole.id) || member.roles.cache.has(helpfulRole.id)) &&
member.presence?.status === 'online' &&
member.id !== interaction.user.id
);

if (membersToPing.size === 0) {
return await interaction.reply({ content: 'No online members with the required roles to ping.', ephemeral: true });
}

const content = membersToPing.map(member => `<@${member.id}> (${member.user.username})`).join('\n');

await interaction.reply({
content,
components: [],
ephemeral: false
});
}
};
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ButtonStyle, ButtonBuilder, ActionRowBuilder } = require('discord.js');

module.exports = {
customId: "urgent-ping",
async execute(interaction, client) {

if (interaction.user.id !== interaction.channel.ownerId) {
return await interaction.reply({ content: 'You are not the owner of this thread!', ephemeral: true });
}

const staffRole = interaction.guild.roles.cache.get('1325170122134130779');
const helpfulRole = interaction.guild.roles.cache.get('1325170416020357282');

const membersToPing = interaction.guild.members.cache.filter(member =>
(member.roles.cache.has(staffRole.id) || member.roles.cache.has(helpfulRole.id)) &&
member.presence?.status === 'online' &&
member.id !== interaction.user.id
);

if (membersToPing.size === 0) {
return await interaction.reply({ content: 'No online members with the required roles to ping.', ephemeral: true });
}

const content = membersToPing.map(member => `<@${member.id}> (${member.user.username})`).join('\n');

await interaction.reply({
content,
components: [],
ephemeral: false
});
}
};
11 Replies
d.js toolkit
d.js toolkit3w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
saoy
saoyOP3w ago
And I was wondering, how could I make it so that it disables the button attached to a message? If you need the handler, lmk! Handler:
const fs = require('fs');

module.exports = (client) => {
client.buttons = new Map();
let allbuttons = fs.readdirSync('./src/buttons').filter(file => file.endsWith('.js'))
for(const file of allbuttons) {
let button = require(`../buttons/${file}`);
client.buttons.set(button.customId, button);
}
}
const fs = require('fs');

module.exports = (client) => {
client.buttons = new Map();
let allbuttons = fs.readdirSync('./src/buttons').filter(file => file.endsWith('.js'))
for(const file of allbuttons) {
let button = require(`../buttons/${file}`);
client.buttons.set(button.customId, button);
}
}
Amgelo
Amgelo3w ago
to what message? the message from the clicked button?
saoy
saoyOP3w ago
Basically haha... before clicking a button, there is an embed I was to disable the button sent to the channel on that embed so the button can't be used more at once because it does invovle pinging
Amgelo
Amgelo3w ago
use #update() instead of #reply()
d.js docs
d.js docs3w ago
Responding to interactions: - #reply immediately respond with a message - #update immediately update the original message (buttons, select menus) - #showModal immediately show a modal (cannot be deferred) - #deferReply/Update respond later (up to 15 minutes) - #followUp post an additional message The initial response has to happen within 3s of receiving the interaction!
Amgelo
Amgelo3w ago
components: [] deletes every row though if you want to disable the button you'd need to send the same row, but with the button being disabled
saoy
saoyOP3w ago
So basically setup another Actionrow but instead have it disabled this time?
Amgelo
Amgelo3w ago
yeah, you can either build the same action row (ideally you'd have some function for that so you don't repeat code, you could also pass the disabled state you want to that function), or build the row from the message's data:
d.js docs
d.js docs3w ago
:property: ButtonInteraction#message [email protected] The message to which the component was attached :property: Message#components [email protected] An array of action rows in the message. This property requires the GatewayIntentBits.MessageContent privileged intent in a guild for messages that do not mention the client. Structures from the API cannot be edited directly. To do so, you can create a new structure (a builder) using the .from() method
const newEmbed = EmbedBuilder.from(embed).setTitle("title")
const newRow = ActionRowBuilder.from(row).addComponents(component)
const newEmbed = EmbedBuilder.from(embed).setTitle("title")
const newRow = ActionRowBuilder.from(row).addComponents(component)
saoy
saoyOP3w ago
Thank you, it works! I forgot some of the most simplest things I swear lol

Did you find this page helpful?