Home > Software design >  How can I set some id's that only allow to use my bot?
How can I set some id's that only allow to use my bot?

Time:08-03

Hi for my discord bot I have a command called ‘.say’ which allows anyone to say something with the bot. I want to change so only an array of ids are able to use it (trusted ppl). Here is my current code (bannedid is an id that is banned from using the bot).

client.on('message', message => {
    if (message.content.startsWith(prefix1   'say')) {
        if (message.author.bot) return;
        if ((message.author.id) === bannedid)
            // If user is in list, then check if message matches a command
              return message.react('⛔');
        const SayMessage = message.content.slice(4).trim();
        setTimeout(function(){ 
            message.delete()
         }, `50`)
        message.channel.send(SayMessage)
        client.channels.cache.get('995545654485078079').send(SayMessage   " "   "**from**"   " "   "**"   (message.author.username)   "**"   " "   "("   (message.author)   ")")
    }
});

banned id refers to:

const bannedid = '745158959698280448'

CodePudding user response:

You can use an array to create a allowed list id's. For example:

const allowed = [
  "first_id", "second_id"
];

Now after listing your allowed id's, you can now create a logic to allow them to use your bot or command.

client.on('message', async(message) => {
   const allowed = [
      "first_id", "second_id"
   ];

   if(message.content.startsWith(prefix   'say')) {
     if(message.author.bot) return;

     if(allowed.includes(message.author.id) {
        //your command etc
     } else {
        //message for not allowed users.
     }
   }
});

On line 
```js
if(message.content.startsWith(prefix   'say'))

You can use back quote ( ` )

if(message.content.startsWith(`${prefix}say`))

CodePudding user response:

You can create an Array with the IDs of the users you want to allow, then check if the person who ran the command is part of that Array.

const trustedPeople = [ "userID_1", "userID_2"];

// if the author of the message is not in the list return
if(!trustedPeople.some(element => message.author.id.includes(element))) { return; }

// Rest of your code
  • Related