DiscordAPIError :未知消息(用于在消息上分配角色的机器人)

时间:2021-04-12 12:24:15

标签: node.js discord discord.js

我正在制作不和谐机器人,它会在用户发送特定消息时分配一些角色。我运行机器人并且一切都完美编译,但是在我输入“Prihvacam uslove”后,它的意思是“我接受规则”,它会分配角色并在服务器中的不和谐下打印我: enter image description here

此外,除了它在此处向我发送消息外,它还向我提供了控制台:

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)

这是我目前的代码:

      #!/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);

谁能帮我解决这个问题? 提前致谢

1 个答案:

答案 0 :(得分:0)

对于将来引用此内容的人:这更多是代码中的逻辑错误。

您最后的 if 语句并没有封装整个条件,只是起到了屏障的作用。这意味着它会多次重复消息反应并为用户分配多个角色,因为 if 语句只关心用户是否已经拥有该角色。

要解决这个问题,可以添加相应的else语句。示例:

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 }));
      });
}