亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

discord.js unban 命令錯誤,當沒有給 unban 一個 id 時

discord.js unban 命令錯誤,當沒有給 unban 一個 id 時

慕斯709654 2022-07-21 10:33:50
我一直在關注這個 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,但我不知道如何。
查看完整描述

2 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

我已經設法為我想做的事情提供了一個適當的解決方案,但首先我想評論幾件事:正如 Zer0 所說,如果bannedMember = await bot.users.fetch(args[0])返回錯誤并且我們用if(!bannedMember)它來檢查它就像!!bannedMember把它變成一個真實的陳述但是,我們對if條件語句有這個定義:

如果指定條件為真,則使用if指定要執行的代碼塊。

這就是我們if(!condition)用來檢查條件是否為假的原因。

但這里的問題不在于。問題是await函數是 async 函數的塊。這意味著,如果它正在等待的承諾在調用時沒有到達,它會出現我遇到的錯誤,而無需繼續執行其余代碼。這是一位朋友給我的解決方案以及我最終使用的解決方案,它運行良好:

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


    if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("You can't do that.")


    if(!args[0]) return message.channel.send("Give me a valid ID"); 

    //This if() checks if we typed anything after "!unban"


    let bannedMember;

    //This try...catch solves the problem with the await

    try{                                                            

        bannedMember = await bot.users.fetch(args[0])

    }catch(e){

        if(!bannedMember) return message.channel.send("That's not a valid ID")

    }


    //Check if the user is not banned

    try {

            await message.guild.fetchBan(args[0])

        } catch(e){

            message.channel.send('This user is not banned.');

            return;

        }


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

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


    if(!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("I can't do that")

    message.delete()

    try {

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

        message.channel.send(`${bannedMember.tag} was readmitted.`)

    } catch(e) {

        console.log(e.message)

    }

}

我正在使用 Zer0 的建議if(!args[0]) return message.channel.send("Give me a valid ID");來檢查在命令!unban解決第一個錯誤之后是否輸入了某些內容。為了解決第二個錯誤并檢查我們是否獲得了有效的 ID,我們進行了第一次嘗試……但如果我們獲得了有效的 ID ,我們只能通過嘗試,因為:


.users:在任何時候緩存的所有用戶對象,由它們的 ID 映射。

.fetch():獲取此用戶。返回:承諾<用戶>。

如果嘗試失敗,則catch運行if以檢查是否bannedMember為false并返回消息錯誤。


查看完整回答
反對 回復 2022-07-21
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

對于第一個錯誤,我會檢查是否給出了 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");

此外,讓下面的代碼工作以捕獲您的第二種錯誤類型也是一種完全有效的方法


    try {

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

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

    } catch(e if e instanceof DiscordAPIError) {

        message.channel.send("Are you sure this is a valid user ID?");

    }


查看完整回答
反對 回復 2022-07-21
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號