How to limit a slash command to a specific role?

node = 18.16.0 discordjs = 14.11.0
23 Replies
d.js toolkit
d.js toolkit12mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Massicraft
Massicraft12mo ago
and to a specific channel
Nounejm
Nounejm12mo ago
you can check that with if
KAVI
KAVI12mo ago
You can limit the slash command to a specific perms using the builder itself. .setDefaultMemberPermissions() exist. But if you wanna check for a role. Do a check on the roles cache of the interaction member object. And see if they have the role ID that you're looking for. And use an if for the channel ID as well using the interaction.channelId
Massicraft
Massicraft12mo ago
how for the channel?
Nounejm
Nounejm12mo ago
if(interaction.channelId !== 'id'){
return ...
}
if(interaction.channelId !== 'id'){
return ...
}
Massicraft
Massicraft12mo ago
thanks and i have another problem, when this command is run it adds a role to a member and then remove another role from the member but it only remove the role it don’t adds the other role whit no errors on the log
KAVI
KAVI12mo ago
Show us how you handle that
Massicraft
Massicraft12mo ago
member.roles.add() member.roles.remove()
KAVI
KAVI12mo ago
Use await on both
Massicraft
Massicraft12mo ago
ok
KAVI
KAVI12mo ago
Those are promise based actions And make sure the IDs are right as well For example you can also use await member.roles.add().then(m => m.roles.remove()) if you want it to happen in a certain order.
Massicraft
Massicraft12mo ago
it still only remove the role, but if i only try to add the role it add it i’ll try
KAVI
KAVI12mo ago
. you still didn't show the exact code snippet
Nounejm
Nounejm12mo ago
dont forget that await requires the function to be async
Massicraft
Massicraft12mo ago
it is
KAVI
KAVI12mo ago
I think the IDE throws an error if the top level function is not async based Before being executed Please show your code where you handle that. If it's too long use source bin or smth like that. Maybe there's something else wrong in the code if it's not working.
Nounejm
Nounejm12mo ago
you're right, thank you
Massicraft
Massicraft12mo ago
it works
KAVI
KAVI12mo ago
<:nb_cat_pat:851511871664029707> np Glad to hear that. Good luck <a:seele_cheer:942760769618059284> But the line by line await also should work <:E_think:793918036787986453> Without having to use then
Nounejm
Nounejm12mo ago
you shine with happiness
Massicraft
Massicraft12mo ago
first rule: if it works don’t touch it and i don’t care about the order thank you very much
Auride
Auride12mo ago
@massicraft You can also use member.roles.set to add/remove multiple roles at once. https://old.discordjs.dev/#/docs/discord.js/14.11.0/class/GuildMemberRoleManager?scrollTo=set
Discord.js
Discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.