简体   繁体   English

discord.js v12 Unban 命令

[英]discord.js v12 Unban Command

I made a unban command using discord.js v12.我使用 discord.js v12 做了一个 unban 命令。 I keep getting the same error every time I run it saying DISCORD API ERROR: NOT FOUND.每次我运行它时都会遇到相同的错误,说 DISCORD API ERROR: NOT FOUND。 Even though I send a correct user ID or mention即使我发送了正确的用户 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);
  },
};

I get this error whenever I run the command:每当我运行命令时,我都会收到此错误:

(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.

Can you help me out?你能帮我吗? Thanks in advance.提前致谢。 This command also only unbans users in the server and not outside it.此命令也只能取消禁止服务器中的用户,而不是在服务器之外。 Is there any way to fix this out?有没有办法解决这个问题?

You're using client.user.fetch() incorrectly.您使用的client.user.fetch()不正确。 You're providing the variable unbanned which is a user object, and therefore it results in an API error.您提供的变量unbanned是用户对象,因此会导致 API 错误。 client.user.fetch() is requesting an id, so instead write client.user.fetch(unbanned.id) . client.user.fetch()正在请求一个 id,所以改为写client.user.fetch(unbanned.id)

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

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

退货:承诺>

Since the fetch just outputs a user object, you could use the unbanned variable instead of making the member variable.由于 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