猿问

discord.js unban 命令错误,当没有给 unban 一个 id 时

我一直在关注这个 discord.js 机器人教程系列,但我发现了一个我无法解决的错误。该命令在您给它一个 id 时有效,但是当您不给它任何东西时,它不会显示错误行,它应该在控制台中显示给我一个错误。


这是没有一些不必要的行或有效行的代码:


const Discord = require("discord.js");

const botconfig = require("../botconfig.json");

const colours = require("../colours.json");



module.exports.run = async (bot, message, args) => { 


    if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("...")


    let bannedMember = await bot.users.fetch(args[0])       //I believe the error is somewhere in this line maybe because of the promise

    if(!bannedMember) return message.channel.send("I need an ID")


    let reason = args.slice(1).join(" ")

    if(!reason) reason = "..."



    try {

        message.guild.members.unban(bannedMember, {reason: reason})

        message.channel.send(`${bannedMember.tag} ha sido readmitido.`)

    } catch(e) {

        console.log(e.message)

    }



}

这是错误:


(node:19648) UnhandledPromiseRejectionWarning: DiscordAPIError: 404: Not Found

    at RequestHandler.execute (C:\Users\Anton\Desktop\Bob\node_modules\discord.js\src\rest\RequestHandler.js:170:25)

    at processTicksAndRejections (internal/process/task_queues.js:97:5)

(node:19648) 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:19648) [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.


我不知道第一个错误有什么问题,对于第二个错误,我想我只需要检查args[0]是id还是snowflake,但我不知道如何。


慕斯709654
浏览 113回答 2
2回答

慕标琳琳

我已经设法为我想做的事情提供了一个适当的解决方案,但首先我想评论几件事:正如 Zer0 所说,如果bannedMember = await bot.users.fetch(args[0])返回错误并且我们用if(!bannedMember)它来检查它就像!!bannedMember把它变成一个真实的陈述但是,我们对if条件语句有这个定义:如果指定条件为真,则使用if指定要执行的代码块。这就是我们if(!condition)用来检查条件是否为假的原因。但这里的问题不在于。问题是await函数是 async 函数的块。这意味着,如果它正在等待的承诺在调用时没有到达,它会出现我遇到的错误,而无需继续执行其余代码。这是一位朋友给我的解决方案以及我最终使用的解决方案,它运行良好:module.exports.run = async (bot, message, args) => {&nbsp;&nbsp; &nbsp; if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("You can't do that.")&nbsp; &nbsp; if(!args[0]) return message.channel.send("Give me a valid ID");&nbsp;&nbsp; &nbsp; //This if() checks if we typed anything after "!unban"&nbsp; &nbsp; let bannedMember;&nbsp; &nbsp; //This try...catch solves the problem with the await&nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; bannedMember = await bot.users.fetch(args[0])&nbsp; &nbsp; }catch(e){&nbsp; &nbsp; &nbsp; &nbsp; if(!bannedMember) return message.channel.send("That's not a valid ID")&nbsp; &nbsp; }&nbsp; &nbsp; //Check if the user is not banned&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await message.guild.fetchBan(args[0])&nbsp; &nbsp; &nbsp; &nbsp; } catch(e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.channel.send('This user is not banned.');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; let reason = args.slice(1).join(" ")&nbsp; &nbsp; if(!reason) reason = "..."&nbsp; &nbsp; if(!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("I can't do that")&nbsp; &nbsp; message.delete()&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; message.guild.members.unban(bannedMember, {reason: reason})&nbsp; &nbsp; &nbsp; &nbsp; message.channel.send(`${bannedMember.tag} was readmitted.`)&nbsp; &nbsp; } catch(e) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(e.message)&nbsp; &nbsp; }}我正在使用 Zer0 的建议if(!args[0]) return message.channel.send("Give me a valid ID");来检查在命令!unban解决第一个错误之后是否输入了某些内容。为了解决第二个错误并检查我们是否获得了有效的 ID,我们进行了第一次尝试……但如果我们获得了有效的 ID ,我们只能通过尝试,因为:.users:在任何时候缓存的所有用户对象,由它们的 ID 映射。.fetch():获取此用户。返回:承诺<用户>。如果尝试失败,则catch运行if以检查是否bannedMember为false并返回消息错误。

拉丁的传说

对于第一个错误,我会检查是否给出了 args[0]。我假设bot.users.fetch返回一个错误对象,因此 a!!bannedMember将评估为真。你在使用 Discord.js v12 吗?这在 v11 和 v12 中有所不同,所以我现在不能给你一个明确的答案。如果你想检查它返回的内容,你可以 console.log 被禁止的成员。所以我的建议是:if(!args[0]) return message.channel.send("please provide a valid ID");此外,让下面的代码工作以捕获您的第二种错误类型也是一种完全有效的方法&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; message.guild.members.unban(bannedMember, { reason });&nbsp; &nbsp; &nbsp; &nbsp; message.channel.send(`${bannedMember.tag} ha sido readmitido.`);&nbsp; &nbsp; } catch(e if e instanceof DiscordAPIError) {&nbsp; &nbsp; &nbsp; &nbsp; message.channel.send("Are you sure this is a valid user ID?");&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答