JianZen
JianZen

Reputation: 227

Discord chat bot change channel post permissions

I'm currently writing a discord bot for a role-play bar. I want it to close down the bar (i.e. restrict post permissions to just me) when I tell it to.

Here's the code:

const Discord = require("discord.js");
const bot = new Discord.Client();

bot.on("message", (message) => {

    switch (message.content) {
        case "Close down the bar for me":
            if (message.author.discriminator == ) { // this isn't a typo i just haven't put it in for posting
                message.postMessage("*Ushers people out, closes the cabinets, changes sign to closed, checks for stragglers, locks the doors, shuts the metal barriers, gets on motorbike and rides home*");

        }
}

});

bot.login(''); // the token is meant to be here, I'm just not putting it on the internet!

what should I put after the message.postMessage to change the default chat permissions to no posting?

Upvotes: 0

Views: 15680

Answers (1)

Blundering Philosopher
Blundering Philosopher

Reputation: 6805

You can try using .overwritePermissions like this, which configures the permissions of a Role in a channel to not allow anyone with that role to send messages:

function closeDownChannel(message) {
    let channel = message.channel;
    let roles = message.guild.roles; // collection

    // find specific role - enter name of a role you create here
    let testRole = roles.cache.find(r => r.id === 'role_id_here');

    // overwrites 'SEND_MESSAGES' role, only on this specific channel
    channel.overwritePermissions(
        testRole,
        { 'SEND_MESSAGES': false },
        // optional 'reason' for permission overwrite
        'closing up shop'
    )
    // handle responses / errors
    .then(console.log)
    .catch(console.log);
}

You just have to make sure the people with that Role don't also have other Roles allowing them to send messages.

Upvotes: 2

Related Questions