0
votes

I am making discord bot which will assign some roles when user sends particular message. I run the bot and everything compiles perfectly, but after I type-in "Prihvacam uslove" which means something like "I accept rules" it assigns roles and prints me out next on discord in server: enter image description here

Also except it sends me message here, it also gives me this into console:

DiscordAPIError: Unknown Message
    at RequestHandler.execute (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async MessageManager.delete (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\managers\MessageManager.js:126:5)
DiscordAPIError: Unknown Message
    at RequestHandler.execute (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async MessageManager.delete (C:\Users\dell\Desktop\FAV-verifier\node_modules\discord.js\src\managers\MessageManager.js:126:5)

This is my code so far:

      #!/usr/bin/env node
            
    "use strict";
        require("dotenv").config();
        

const Discord = require("discord.js");
    const chalk = require("chalk");
    const moment = require("moment");
    
const BOT_TOKEN = "HERE GOES MY BOT TOKEN"
const VERIFICATION_CHANNEL = "831101010038095912"
const VERIFIED_ROLE = "Plava"
const VERIFIED_ROLE2 = "Zuta"
const VERIFIED_ROLE3 = "Crvena"
const VERIFIED_ROLE4 = "????┃Srbija"
const client = new Discord.Client({
  disableEveryone: true
});

client.once("ready", () => {
  console.log(chalk.greenBright("[READY]"), `Logged in as ${client.user.tag} (${client.user.id}) at ${moment().format("DD MMMM YYYY, hh:mm:ss")}`);
});

client.on("message", message => {
  if (!message.guild) return;
  if (message.author.bot) return;
  if (message.content === "Prihvacam uslove" && message.channel.id === VERIFICATION_CHANNEL) {
    if (!message.channel.permissionsFor(message.guild.me).serialize().SEND_MESSAGES) return console.error("Bot nema permisiju\nPotrebno je : SEND_MESSAGES");
    if (!message.channel.permissionsFor(message.guild.me).serialize().ADD_REACTIONS) {
      console.error("The bot doesn't have the permission to add reactions.\nRequired permission: `ADD_REACTIONS`");
      message.channel.send("Bot nema permisiju da salje poruku.\nPotrebna permisija: `ADD_REACTIONS`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (!message.channel.permissionsFor(message.guild.me).serialize().MANAGE_MESSAGES) {
      console.error("Bot nema permisiju da obrise poruku.\nRequired permission: `MANAGE_MESSAGES`");
      message.channel.send("Bot nema permisiju da obrise poruku.\nRequired permission: `MANAGE_MESSAGES`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    const messageRole = message.guild.roles.cache.find(role => role.name === VERIFIED_ROLE)
    const messageRole2 = message.guild.roles.cache.find(role => role.name === VERIFIED_ROLE2)
    const messageRole3 = message.guild.roles.cache.find(role => role.name === VERIFIED_ROLE3)
    const messageRole4 = message.guild.roles.cache.find(role => role.name === VERIFIED_ROLE4)




    //ONO STO SE DESI KADA NIJE POSTAVLJEN
    //-------------------------------------------------------------------------------------------------------------------
    if (messageRole == null) return;
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      message.channel.send("Bot nema permisiju da dodjeli role.\nRequired permission: `MANAGE_ROLES`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (message.guild.me.roles.highest.comparePositionTo(messageRole) < 1) {
      message.channel.send("Pozicija ovog rola je veća od pozicije rola bota, pa se ne moze dodijeliti od strane bota.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (messageRole.managed == true) {
      message.channel.send("Ovo je automanaged role, ne moze biti dodijeljen.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }

    //------------------------------------------------------------------------------------------------------------------------
    if (messageRole2 == null) return;
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      message.channel.send("Bot nema permisiju da dodjeli role.\nRequired permission: `MANAGE_ROLES`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (message.guild.me.roles.highest.comparePositionTo(messageRole2) < 1) {
      message.channel.send("Pozicija ovog rola je veća od pozicije rola bota, pa se ne moze dodijeliti od strane bota.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (messageRole2.managed == true) {
      message.channel.send("Ovo je automanaged role, ne moze biti dodijeljen.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    //----------------------------------------------------------------------------------------------------------------------

    if (messageRole3 == null) return;
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      message.channel.send("Bot nema permisiju da dodjeli role.\nRequired permission: `MANAGE_ROLES`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (message.guild.me.roles.highest.comparePositionTo(messageRole3) < 1) {
      message.channel.send("Pozicija ovog rola je veća od pozicije rola bota, pa se ne moze dodijeliti od strane bota.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (messageRole3.managed == true) {
      message.channel.send("Ovo je automanaged role, ne moze biti dodijeljen.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }

    //------------------------------------------------------------------------------------------------------------------------
    if (messageRole4 == null) return;
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      message.channel.send("Bot nema permisiju da dodjeli role.\nRequired permission: `MANAGE_ROLES`")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (message.guild.me.roles.highest.comparePositionTo(messageRole4) < 1) {
      message.channel.send("Pozicija ovog rola je veća od pozicije rola bota, pa se ne moze dodijeliti od strane bota.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }
    if (messageRole4.managed == true) {
      message.channel.send("Ovo je automanaged role, ne moze biti dodijeljen.")
        .then(m => m.delete({ timeout: 20000 }));
      return;
    }

    //-------------------------------------------------------------------------------------------------------------------
    //ONO STO SE DESI KAD JE POSTAVLJEN ROLE
    if (message.member.roles.cache.has(messageRole2.id)) return;
    message.react("✅");
    message.member.roles.add(messageRole2)
      .then(() => message.delete({ timeout: 5000 }))
      .catch(error => {
        console.error(error.stack);
        message.channel.send(error.stack)
          .then(m => m.delete({ timeout: 20000 }));
      });
    //----------------------------------------------------------------------------------------------------------------

    if (message.member.roles.cache.has(messageRole3.id)) return;
    message.react("✅");
    message.member.roles.add(messageRole3)
      .then(() => message.delete({ timeout: 5000 }))
      .catch(error => {
        console.error(error.stack);
        message.channel.send(error.stack)
          .then(m => m.delete({ timeout: 20000 }));
      });
    //------------------------------------------------------------------------------------------------------------------

    if (message.member.roles.cache.has(messageRole4.id)) return;
    message.react("✅");
    message.member.roles.add(messageRole4)
      .then(() => message.delete({ timeout: 5000 }))
      .catch(error => {
        console.error(error.stack);
        message.channel.send(error.stack)
          .then(m => m.delete({ timeout: 20000 }));
      });
    //-----------------------------------------------------------------------------------------------------------------


  }
});

client.login(BOT_TOKEN);

Can anyone help me how to solve this? Thanks in advance

1
Does the bot react to the message it sent out with the ✅ emoji?PerplexingParadox
@PerplexingParadox Yes, it does. It reacts with ✅, then it deletes my "Prihvacam uslove" (which is supposed to happen), and after like 2 seconds after that. it prints out those error messages.Aldin
What I was also able to figure out is that the bot doesn't assign VERIFIED_ROLE .. Basically it doesn't assign that Plava role. (Plava means Blue)Aldin
It seems like you could try fixing it via adding else statements to the code. Because the if conditions may seem to act as barriers to it, but in reality, you're just repeating the same steps over and over again in succession but with different roles.PerplexingParadox
Unless if it's your plan to make sure that once the user selects the ✅ it would make sure to add every single role that the user doesn't currently have.PerplexingParadox

1 Answers

0
votes

For people referencing this in the future: It was more of a logical error in the code.

Your if statements at the end weren't encapsulating the whole condition, merely acting like barriers. This means that it repeats the message reactions multiple times and assigns multiple roles to the user since the if statements merely concerned whether or not the user had the role already.

To solve this, you can add a corresponding else statement. Example:

if (message.member.roles.cache.has(messageRole2.id)) {
    return;
} else { //this part is key
    message.react("✅");
    message.member.roles.add(messageRole2)
      .then(() => message.delete({ timeout: 5000 }))
      .catch(error => {
        console.error(error.stack);
        message.channel.send(error.stack)
          .then(m => m.delete({ timeout: 20000 }));
      });
}