Requiring a specific role for a specific button.

So as I was in my testing phase, I created a ticket and noticed everyone could see the ticket. Can anyone have a look at this for me please, would mean a lot to me :)
9 Replies
d.js toolkit
d.js toolkit9mo 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! - Marked as resolved by staff
axiprime2.0
axiprime2.09mo ago
Look at what ? share mode information 🥲
Salis
SalisOP9mo ago
Im so sorry its my first time creating a bot i got very far but now like
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Salis
SalisOP9mo ago
Hmm a quick explanation for this is, I want to make it so only one specific role can make use of the accept button but it says "you have no permissions" but still assigns the role when the member themself press accept, code will follow now.
const { EmbedBuilder, PermissionsBitField, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const client = require('..');

client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return;
let button;
console.log(interaction.customId)


if (interaction.customId.startsWith('accepteren')) {
button = client.buttons.get('accepteren');
} else if (interaction.customId.startsWith('roles')) {
button = client.buttons.get('roles');
} else if (interaction.customId.startsWith('unban')) {
button = client.buttons.get('unban');
} else {
button = client.buttons.get(interaction.customId);
}








if (!button) return;

try {
if (button.permissions) {
if (!interaction.memberPermissions.has(PermissionsBitField.resolve(button.permissions || []))) {
const perms = new EmbedBuilder()
.setDescription(`🚫 ${interaction.user}, You don't have \`${button.permissions}\` permissions to interact this button!`)
.setColor('Red')
return interaction.reply({ embeds: [perms], ephemeral: true })
}
}
return await button.run(client, interaction);
} catch (error) {
interaction.reply({ content: 'Er is een fout opgetreden!', ephemeral: true });
console.log(error);
}

interaction.reply({ content: 'Button is niet beschikbaar!', ephemeral: true })
});
const { EmbedBuilder, PermissionsBitField, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const client = require('..');

client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return;
let button;
console.log(interaction.customId)


if (interaction.customId.startsWith('accepteren')) {
button = client.buttons.get('accepteren');
} else if (interaction.customId.startsWith('roles')) {
button = client.buttons.get('roles');
} else if (interaction.customId.startsWith('unban')) {
button = client.buttons.get('unban');
} else {
button = client.buttons.get(interaction.customId);
}








if (!button) return;

try {
if (button.permissions) {
if (!interaction.memberPermissions.has(PermissionsBitField.resolve(button.permissions || []))) {
const perms = new EmbedBuilder()
.setDescription(`🚫 ${interaction.user}, You don't have \`${button.permissions}\` permissions to interact this button!`)
.setColor('Red')
return interaction.reply({ embeds: [perms], ephemeral: true })
}
}
return await button.run(client, interaction);
} catch (error) {
interaction.reply({ content: 'Er is een fout opgetreden!', ephemeral: true });
console.log(error);
}

interaction.reply({ content: 'Button is niet beschikbaar!', ephemeral: true })
});
I want the accept button to require a specific role, so it doesn't complete the output of giving the role. Any ideas? Here you go
const { PermissionsBitField , EmbedBuilder } = require('discord.js');
const { ticketRoles, vrouwenRol, primaryColor, succesColor } = require('../../config');

module.exports = {
id: 'accepteren',
permissions: [],
run: async (client, interaction) => {
const memberId = interaction.customId.split('-')[1];
const member = interaction.guild.members.cache.get(memberId);
const role = interaction.guild.roles.cache.get(vrouwenRol);

const hasTicketRole = member.roles.cache.some(userRole => ticketRoles.includes(userRole.id));

if (!hasTicketRole) {
if (!member.permissions.has(PermissionsBitField.Flags.KickMembers || PermissionsBitField.Flags.BanMembers)) {
await interaction.reply({ content: "Je hebt niet de vereiste rol om dit te doen.", ephemeral: true });
}
}



member.roles.add(vrouwenRol);

const embed = new EmbedBuilder()
.setDescription(`${member} heeft de ${role} ontvangen.`)
.setColor(succesColor);

interaction.reply({ embeds: [embed] });
},
};
const { PermissionsBitField , EmbedBuilder } = require('discord.js');
const { ticketRoles, vrouwenRol, primaryColor, succesColor } = require('../../config');

module.exports = {
id: 'accepteren',
permissions: [],
run: async (client, interaction) => {
const memberId = interaction.customId.split('-')[1];
const member = interaction.guild.members.cache.get(memberId);
const role = interaction.guild.roles.cache.get(vrouwenRol);

const hasTicketRole = member.roles.cache.some(userRole => ticketRoles.includes(userRole.id));

if (!hasTicketRole) {
if (!member.permissions.has(PermissionsBitField.Flags.KickMembers || PermissionsBitField.Flags.BanMembers)) {
await interaction.reply({ content: "Je hebt niet de vereiste rol om dit te doen.", ephemeral: true });
}
}



member.roles.add(vrouwenRol);

const embed = new EmbedBuilder()
.setDescription(`${member} heeft de ${role} ontvangen.`)
.setColor(succesColor);

interaction.reply({ embeds: [embed] });
},
};
Here's also the code where it gives the role Wth, i just noticed anyone can do ANYTHING every slash command if you need any more handlers or events let me know @TAEMBO.md sorry for the ping 😅
Salis
SalisOP9mo ago
No description
Salis
SalisOP9mo ago
So I did what you said, it is not giving the role indeed, instead it crashes haha.
TÆMBØ
TÆMBØ9mo ago
Well, this code here is going to result in two attempted replies because you aren't returning in either of those if statements, so even if both of them pass, it's going to continue to the reply() at the bottom of your command logic
Salis
SalisOP9mo ago
im really not getting it to work.. It says, "you do not have the specified role to do this"
const { PermissionsBitField , EmbedBuilder } = require('discord.js');
const { ticketRoles, vrouwenRol, primaryColor, succesColor } = require('../../config');

module.exports = {
id: 'accepteren',
permissions: [],
run: async (client, interaction) => {
const memberId = interaction.customId.split('-')[1];
const member = interaction.guild.members.cache.get(memberId);
const role = interaction.guild.roles.cache.get(vrouwenRol);

const hasTicketRole = member.roles.cache.some(userRole => ticketRoles.includes(userRole.id));

if (!hasTicketRole) {
if (!member.permissions.has(PermissionsBitField.Flags.KickMembers || PermissionsBitField.Flags.BanMembers)) {
await interaction.reply({ content: "Je hebt niet de vereiste rol om dit te doen.", ephemeral: true });
}
}


member.roles.add(vrouwenRol);

const embed = new EmbedBuilder()
.setDescription(`${member} heeft de ${role} ontvangen.`)
.setColor(succesColor);

interaction.reply({ embeds: [embed] });
},
};

// interaction.member.roles.cache.has(1209319748563636244) <<< this is what i want to restrict it to
const { PermissionsBitField , EmbedBuilder } = require('discord.js');
const { ticketRoles, vrouwenRol, primaryColor, succesColor } = require('../../config');

module.exports = {
id: 'accepteren',
permissions: [],
run: async (client, interaction) => {
const memberId = interaction.customId.split('-')[1];
const member = interaction.guild.members.cache.get(memberId);
const role = interaction.guild.roles.cache.get(vrouwenRol);

const hasTicketRole = member.roles.cache.some(userRole => ticketRoles.includes(userRole.id));

if (!hasTicketRole) {
if (!member.permissions.has(PermissionsBitField.Flags.KickMembers || PermissionsBitField.Flags.BanMembers)) {
await interaction.reply({ content: "Je hebt niet de vereiste rol om dit te doen.", ephemeral: true });
}
}


member.roles.add(vrouwenRol);

const embed = new EmbedBuilder()
.setDescription(`${member} heeft de ${role} ontvangen.`)
.setColor(succesColor);

interaction.reply({ embeds: [embed] });
},
};

// interaction.member.roles.cache.has(1209319748563636244) <<< this is what i want to restrict it to
@TAEMBO.md 😅 i put it all back before i mess it up even more but maybe you would want to place it in the right spot Hey I solved it after trying for so long :) thanks now i understand :))
Want results from more Discord servers?
Add your server