title: I am getting this error via trying to assign roles to the user in the discord via discord.js bot
body: <pre><code>/app/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85
                 new DiscordAPIError(res.request.path, res.body, res.request.method) : err);
                 ^
   
   DiscordAPIError: Missing Permissions
       at /app/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15
       at /app/node_modules/snekfetch/src/index.js:215:21
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
     path: '/api/v7/guilds/715245634218885131/members/235148962103951360',
     code: 50013,
    method: 'PATCH'
   }
  </code></pre>
  <p>This is the error i am facing while the bot got all the permission and is not trying the change the role of owner of someone who is higher in hierarchy.</p>
  <p>A year ago this code was working fine but now suddenly it is not working please look at it.</p>
  <pre><code>//basic constants and functions
  var checked;
  var checking = [];
  const Discord = require(&quot;discord.js&quot;);
  const {usernameorid, token,usernamesp, roles, apiKey, spreadsheetId, range, unremoveableroles, refreshrate} = require(&quot;./config/discord.json&quot;);
  const { google } = require(&quot;googleapis&quot;);
  
    const connection = google.sheets({
    version: &quot;v4&quot;,
    auth: apiKey
  });
  
  
  const client = new Discord.Client();
  
  let oldRows = [];
  
  
  //checking for errors
  const logError = error =&gt; {
    console.error(&quot;error log &quot; + new Date());
    console.error(error);
  };
  
  //the spreadsheet fetching
  const fetchRows = async (spreadsheetId, range, sheetsConnection) =&gt; {
    try {
      const response = await sheetsConnection.spreadsheets.values.get({
        spreadsheetId: spreadsheetId,
        range: range
      });
      //fetching all data
      const rows = response.data.values;
  
  
      
      
      
      
      
      
      //dividing the data between different array using while loop
      var i = 0;
      var k=0;
     
      
      
      while (i &lt; rows.length) {
   
     
        const checkrow=&quot;&quot;;
        
     
        
        
        const rows123= rows[i]
   
        
        
     
          
  //finally putting all roles together
  
        
        
  const therolenames = rows123[1]+&quot;,&quot;+rows123[2]+&quot;,&quot;+rows123[3]+&quot;,&quot;+rows123[4];
  
      
        assignRoles(rows123[0].trim(), therolenames.trim());
    
  
  
           rows123[0]==null;
     rows123[1]==null;
     rows123[2]==null;
     rows123[3]==null;
     rows123[4]==null;   rows123[1]==null;
     roles[1]==null;
     roles[2]==null;
     roles[3]==null;
     roles[4]==null;
     
        
      therolenames==null;
     roles.length=0;   
  rows123.length=0;
  i++;
  k++;
         
        }
     
      return rows;
    } catch (error) {
      logError(error);
    }
  };
  
  const assignRoles = async (usernames, roleNames) =&gt; {
  
     var rolenames1 = roleNames.split(&quot;,&quot;);
  
    try {
      const guildMembers = client.guilds.array()[0].members.array();
   
      //number of all members in the group
   
      
      const guildRoles = client.guilds.array()[0].roles.array();
      // deleting unremovable roles from the list
    const Roles2 = guildRoles.filter(Role =&gt; !unremoveableroles.includes(Role.name));
      // * getting Role instances of role names
      const removedRoles = guildRoles.filter(Role =&gt; unremoveableroles.includes(Role.name));
      const Roles = guildRoles.filter(Role =&gt; roleNames.includes(Role.name));
     //role number of all members in the group
     
  
      roleNames.length=0;
      // * getting GuildMember instances of usernames and setting roles
      guildMembers.forEach(async member =&gt; {
       
        const  username= member.user.username + &quot;#&quot; + member.user.discriminator;
      
   
    //check if the first column of spreadsheet matches the userid/username of discord
  
  var UNcheck;
        
        if (usernameorid==&quot;UN&quot;){
    
     UNcheck=username
    
    }else{
      
    UNcheck=member.id
      
      
    }
  
    
    if (usernames.includes(UNcheck))  {
          
          const notAssignedRoles = [];
          
         
        
          
          
          
          
          Roles.forEach(role =&gt; {
         
           //if (!member.roles.array().includes(Roles)) {
           //if (!guildRoles.includes(Roles)) {
            
            
        
        checked=&quot;no&quot;
            
             
                    //Check whether the roles (discord) are in roles (spreadsheet)
            
            
          
                if (Roles.includes(member.roles.array()[1])|| removedRoles.includes(member.roles.array()[1])|| member.roles.array()[1]==null){
               
                }else{checked=&quot;yes&quot; }    
                  if (Roles.includes(member.roles.array()[2]) || removedRoles.includes(member.roles.array()[2]) || member.roles.array()[2]==null){
                
                 }else{checked=&quot;yes&quot;; }    
                      if (Roles.includes(member.roles.array()[3]) || removedRoles.includes(member.roles.array()[3])|| member.roles.array()[3]==null){
                    
                   }else{checked=&quot;yes&quot;; }    
                     if (Roles.includes(member.roles.array()[4])|| removedRoles.includes(member.roles.array()[4]) || member.roles.array()[4]==null){
             
                     }else{checked=&quot;yes&quot;; }    
            
                      
                             //Check whether the roles (Spreadsheet) are in roles (Discord)
             
                      
                if (member.roles.array().includes(Roles[0])|| removedRoles.includes(Roles[0]) || Roles[0]==null){
                
                }else{checked=&quot;yes&quot;; }    
                 if (member.roles.array().includes(Roles[1]) || removedRoles.includes(Roles[1])|| Roles[1]==null){
            
               
                    }else{checked=&quot;yes&quot;; }    
                          if (member.roles.array().includes(Roles[2]) || removedRoles.includes(Roles[2]) || Roles[2]==null){
              
                        }else{checked=&quot;yes&quot;; }    
                               if (member.roles.array().includes(Roles[3])|| removedRoles.includes(Roles[3]) || Roles[3]==null){
               
                            }else{checked=&quot;yes&quot;; }    
               
       
            
                        if (checked==&quot;yes&quot;){ 
                          notAssignedRoles.push(role);
                        }
  
          });
          
          if (notAssignedRoles.length &gt; 0) {
          
          
       
            
          
       
            
            await  member.removeRoles(Roles2); member.addRoles(Roles);
   
            
            
          
          
        
            console.log(
              &quot;Assigned &quot; +
                notAssignedRoles.map(role =&gt; role.name) +
                &quot; to &quot; +
                username +  &quot; [&quot;+member.id+&quot;] on &quot; + new Date().toString()
            );
          } else {
                
              if (Roles[0]==null &amp;&amp; Roles[1]==null &amp;&amp; Roles[2]==null &amp;&amp; Roles[3]==null) {
               member.removeRoles(Roles2);
         console.log(username + &quot; [&quot;+member.id+&quot;] has all roles removed on&quot;+ new Date().toString());     
            }else{
            
            console.log(username + &quot; [&quot;+member.id+&quot;] already has all the roles assigned, checked on &quot;+ new Date().toString());
            }
            }
        }
      });
    } catch (err) {
      logError(err);
    }
  };
  
  
  
  
  
  
  const extractNewEntries = (oldRows, rows) =&gt; {
    let newRows = [];
    if (rows.length &gt; oldRows.length) {
      newRows = rows.slice(oldRows.length);
     
    }
    newRows.forEach(row =&gt; {
      oldRows.push(row);
      
    });
    return newRows;
  };
  
  const extractDiscordIDs = rows =&gt; {
    return rows.map(user =&gt; user[0]);
    console.log(rows.map(user =&gt; user[0]));
  };
  
  client.once(
    &quot;ready&quot;,
    () =&gt; {
      console.log(&quot;Bot started with these settings:&quot;);
      console.log(&quot;• Spreadsheet ID: &quot;);
      console.log(&quot;• Range: &quot; + range);
      console.log(&quot;• Roles: &quot; + roles.join(&quot;, &quot;));
      setInterval(async () =&gt; {
        console.log(&quot;\nChecked for new entries on &quot; + new Date().toString());
        let rows;
        try {
          rows = await fetchRows(spreadsheetId, range, connection);
  
          const newEntries = extractNewEntries(oldRows, rows);
          if (newEntries.length &gt; 0) {
            console.log(`Found ${newEntries.length} new entries`);
            const usernames = extractDiscordIDs(newEntries);
            
          } else {
            console.log(&quot;No new entries&quot;);
          }
        } catch (err) {
          if (rows === undefined) console.error(&quot;Google Sheet is empty&quot;);
          logError(err);
          rows = [];
        }
      }, refreshrate); // refresh rate: 60000 milliseconds == 1 minute
    },
    logError
  );
  
  client.on(&quot;error&quot;, logError);
  
  const start = async () =&gt; {
    try {
      await client.login(token);
    } catch (err) {
      logError(err);
      setTimeout(start, 30000);
    }
  };
  
  start();
  
  client.on(&quot;disconnect&quot;, start);
  
  module.exports = {
    fetchRows,
    assignRoles,
    extractNewEntries,
    extractDiscordIDs
  };
  
  
  
  const express = require(&quot;express&quot;);
  const fs = require(&quot;fs&quot;);
  const app = express();
  
  app.use(express.static(&quot;public&quot;));
  const listener = app.listen(process.env.PORT, function() {
    console.log(&quot;Your app is listening on port &quot; + listener.address().port);
  });
  </code></pre>
  <p>The complete code is available at
  <a href="https://github.com/supreen/form2role-bot" rel="nofollow noreferrer">https://github.com/supreen/form2role-bot</a>
  please see if you can reproduce the error.</p>