简体   繁体   中英

Discord.js v12 Owner prefix

so I'm trying to create a owner only prefix, I had no idea how. Here's is my current code.

I've tried to create a owner prefix for a months and hopefully to have someone that can help me for code command.

client.on('message', async message => {
    const prefix = '.' 
    if (message.author.bot || message.channel.type === 'dm') return;
    if (message.content.startsWith(prefix)) {
        const messageArray = message.content.split(' ');
        const cmd = messageArray[0]
        const args = messageArray.slice(1);
        const command = client.commands.get(cmd.slice(prefix.length)) || client.commands.get(client.aliases.get(cmd.slice(prefix.length)));
        if (command) {
            if (!command.config.botPerms) return console.log("You didn't provide botPerms");
            if (!Array.isArray(command.config.botPerms)) return console.log('botPerms must be an array.');
            if (!command.config.userPerms) return console.log("You didn't provide userPerms.");
            if (!Array.isArray(command.config.userPerms)) return console.log('userPerms must be an array.')
            if (!message.guild.me.hasPermission(command.config.botPerms)) {
                const beauty = command.config.botPerms.join('\`, \`');
                const noBotPerms = new Discord.MessageEmbed()
                    .setTitle('Missing Permissions')
                    .setDescription(`I am missing these permissions: \`${beauty}\`.`)
                    .setColor('RED');
                return message.channel.send(noBotPerms)
            }
            if (!message.member.hasPermission(command.config.userPerms)) {
                const beauty = command.config.userPerms.join('\`, \`');
                const noUserPerms = new Discord.MessageEmbed()
                    .setTitle('Missing Permissions')
                    .setDescription(`You are missing these permissions: \`${beauty}\`.`)
                    .setColor('RED');
                return message.channel.send(noUserPerms)
            }

            command.run(client, message, args);
        }
    }
});

You can easily assign a prefix depending on whether the user is the owner or not by using ternary operator , frequently used as alternative to an if...else statement.

const prefix = message.author.id !== OWNER_ID ? '.' : OWNER_PREFIX; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM