簡體   English   中英

discord.js v12 Unban 命令

[英]discord.js v12 Unban Command

我使用 discord.js v12 做了一個 unban 命令。 每次我運行它時都會遇到相同的錯誤,說 DISCORD API ERROR: NOT FOUND。 即使我發送了正確的用戶 ID 或提及

 const Discord = require("discord.js");
    module.exports = {
      name: "unban",
      category: "moderation",
      description: "unban",
      run: async (client, message, args) => {
    
    
    
        let unbanned = message.mentions.users.first() || client.users.resolve(args[0]);
        let reason = args.slice(1).join(" ");
    
        let member = await client.users.fetch(unbanned);
        let ban = await message.guild.fetchBans();
    
    // MESSAGES

    if (!unbanned) {
      let unbaninfoembed = new Discord.MessageEmbed()
        .setTitle("Command: unban")
        .setDescription(
          `**Description:** Unban a member. \n` +
            "**Sub Commands:**\n" +
            "" +
            "**Usage:**\n" +
            "-unban [user] (limit) (reason) \n" +
            "**Examples:** \n" +
            "-unban <@597253939469221891> good guy \n" +
            "-unban 597253939469221891 good guy "
        )
        .setColor("#2C2F33");
      message.channel.send(unbaninfoembed);

      return;
    }

    if (!ban.get(member.id)) {
      let notbannedembed = new Discord.MessageEmbed()
        .setDescription("This user is not banned")
        .setColor("#2C2F33");
      message.channel.send(notbannedembed);

      return;
    }

    if (!message.guild.me.permissions.has("BAN_MEMBERS")) {
      let botnoperms = new Discord.MessageEmbed()
        .setDescription(
          "I do not have permissions, please contact an administrator"
        )
        .setColor("#2C2F33");
      message.channel.send(botnoperms);

      return;
    }

    if (!message.member.permissions.has("BAN_MEMBERS")) {
      let nopermsembed = new Discord.MessageEmbed()
        .setDescription(
          "You do not have permission `BAN MEMBERS` contact an administrator"
        )
        .setColor("#2C2F33");
      message.channel.send(nopermsembed);

      return;
    }

    var user = ban.get(member.id);
    message.guild.members.unban(member);
    let successfullyembed = new Discord.MessageEmbed()
      .setTitle(`${member.tag} has been successfully unbanned.`)
      .setColor("#2C2F33");

    message.channel.send(successfullyembed);
  },
};

每當我運行命令時,我都會收到此錯誤:

(node:106) UnhandledPromiseRejectionWarning: DiscordAPIError: 404: Not Found
    at RequestHandler.execute (/home/runner/bot/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:106) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:106) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

你能幫我嗎? 提前致謝。 此命令也只能取消禁止服務器中的用戶,而不是在服務器之外。 有沒有辦法解決這個問題?

您使用的client.user.fetch()不正確。 您提供的變量unbanned是用戶對象,因此會導致 API 錯誤。 client.user.fetch()正在請求一個 id,所以改為寫client.user.fetch(unbanned.id)

https://discord.js.org/#/docs/main/stable/class/UserManager?scrollTo=fetch

.fetch(id, [cache], [force])

退貨:承諾>

由於 fetch 只是輸出一個用戶對象,因此您可以使用unbanned變量而不是創建member變量。

我找到了答案,將client.user.fetch(unbanned)替換為let member = await client.users.fetch(args.slice(0).join(" "));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM