'How to create and set permissions of a role at the same command in discord.j?

I have been trying to make a command that creates the roles that are necessary for some actions of the bot and I have successfully made the code for creating the roles but I couldn't set the roles permissions because it always gives an error Here are the codes:

} else if (command == 'genelkurulum') {
                client.commands.get('generalsetup').execute(message, args)
                client.commands.get('rolepermissions').execute(message, args)

generalsetup creates the roles and rolepermissions sets the permissions of the roles. generalsetups inside:

module.exports = {
    name: 'generalsetup',
    description: "This is important for bot to understand some of the commands",
    async execute(message, args) {
        if (message.member.permissions.has("ADMINISTRATOR")) {
            message.channel.send('```Setup in process```');
            message.guild.roles.create({
                name: 'Muted',
                reason: 'Before giving custom roles, you need to add the roles to server'
            })
            message.guild.roles.create({
                name: 'Member',
                reason: 'Before giving custom roles, you need to add the roles to server'
            })
        } else {
            message.reply('Sorry, you are not an admin...')
        }
    }
}

And rolepermissions inside:

const { Permissions } = require("discord.js");
module.exports = {
    name: 'rolepermissions', 
    description: "Gives the roles the necessary permissions", 
    async execute(message, args) {
        let Muted = message.guild.roles.cache.find(role => role.name === 'Muted');
            Muted.setPermissions([Permissions.FLAGS.VIEW_CHANNEL, Permissions.FLAGS.READ_MESSAGE_HISTORY]);
    message.channel.send('Role permissions have been set up')
    }
}

How can I make this work?

The codes weren't serapeted in the beginning, I tried to separate them to fix the problem but it didn't work but didn't cause me any harm, so let it be that way



Solution 1:[1]

You can use a time wait before executing the first command or in a way, you can set permissions in your generalsetup command. Like;

guild.roles.create({ name: 'Muted', permissions: [Permissions.FLAGS.MANAGE_MESSAGES, Permissions.FLAGS.KICK_MEMBERS] });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman