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 toolkit2y 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
MassicraftOP2y ago
and to a specific channel
Nounejm
Nounejm2y ago
you can check that with if
KAVI
KAVI2y 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
MassicraftOP2y ago
how for the channel?
Nounejm
Nounejm2y ago
if(interaction.channelId !== 'id'){
return ...
}
if(interaction.channelId !== 'id'){
return ...
}
Massicraft
MassicraftOP2y 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
KAVI2y ago
Show us how you handle that
Massicraft
MassicraftOP2y ago
member.roles.add() member.roles.remove()
KAVI
KAVI2y ago
Use await on both
Massicraft
MassicraftOP2y ago
ok
KAVI
KAVI2y 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
MassicraftOP2y ago
it still only remove the role, but if i only try to add the role it add it i’ll try
KAVI
KAVI2y ago
. you still didn't show the exact code snippet
Nounejm
Nounejm2y ago
dont forget that await requires the function to be async
Massicraft
MassicraftOP2y ago
it is
KAVI
KAVI2y 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
Nounejm2y ago
you're right, thank you
Massicraft
MassicraftOP2y ago
it works
KAVI
KAVI2y 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
Nounejm2y ago
you shine with happiness
Massicraft
MassicraftOP2y ago
first rule: if it works don’t touch it and i don’t care about the order thank you very much
Auride
Auride2y 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.
Want results from more Discord servers?
Add your server