Skip to content
Advertisement

How can I owner lock this command in discord.js v12?

I’m trying to make this command so only I can run it, no luck so far.

client.on("message", message => { 
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase()

    if (command === "test") {
        console.log((chalk.yellow)`You ran a command: test`)
        message.channel.send('test')
    }
});

I tried using

if (!message.author.id === config.ownerID) return;

and

if (message.author.id !== config.ownerID) return;

When I used the first one, the command worked but everyone was able to run it, and when I used the second one no one was able to run it at all. I don’t get any error logs nor crashes. Anyone knows the correct code?

Advertisement

Answer

As I mentioned in my comment above, the first one is definitely incorrect, as you’re converting message.author.id to a boolean by using the logical NOT operator (!). Your second attempt could work if config.ownerID was a string, but you can’t compare a string to an array.

If your config.ownerID is an array of IDs, you can use the includes() method to check if the message.author.id is included the given array:

if (config.ownerID.includes(message.author.id)) return
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement