javascriptlockingdiscorddiscord.jsunlock

How to create a discord.js lock command


I'm trying to create a lock and unlock command for my discord.js bot. How would I be able to do this?

I want to make it so when I do >lock it takes permission away from Verified to SEND_MESSAGES.

Then if I do >unlock, it unlocks the channel.


Solution

  • In your function, you just need to call the following lines to remove the permissions,

    const role = guild.roles.find("name", "Verified ");
    
    role.permissions.remove('SEND_MESSAGES')
    

    and to give them back, just put the following lines under the command:

    const role = guild.roles.find("name", "Verified ");
    
    role.permissions.add('SEND_MESSAGES')
    

    If you want to understand why this will work, here are some relevant docs links: role, permissions, and the permissions flags.

    EDIT: To change the permissions for the specific channels, just do:

    const role = guild.roles.find("name", "Verified ");
    
    message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': false })
    

    and to give them back, you would do the following

    const role = guild.roles.find("name", "Verified ");
    
    message.channel.overwritePermissions(role,{ 'SEND_MESSAGES': true})