簡體   English   中英

類型錯誤:無法讀取未定義 discord.js 的屬性“查找”

[英]TypeError: Cannot read property 'find' of undefined discord.js

我正在為我的 discord 機器人發出一個命令,我在這個過程中遇到了一個錯誤

TypeError: Cannot read property 'find' of undefined
    at Object.callback (C:\Users\Aoshey\Downloads\Nol\bot test\modules\setimage.mod.js:11:30)
    at ReputationCore.<anonymous> (C:\Users\Aoshey\node_modules\reputation-core\builtin_modules\commands.mod.js:54:33)
    at ReputationCore.emit (events.js:314:20)
    at ReputationCore.EventEmitter.emit (domain.js:483:12)
    at Client.<anonymous> (C:\Users\Aoshey\node_modules\reputation-core\reputationCore.js:35:10)
    at Client.emit (events.js:314:20)
    at Client.EventEmitter.emit (domain.js:483:12)
    at MessageCreateHandler.handle (C:\Users\Aoshey\node_modules\reputation-core\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\Aoshey\node_modules\reputation-core\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:108:65)
    at WebSocketConnection.onPacket (C:\Users\Aoshey\node_modules\reputatiocore\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:336:35)

代碼:

const fsn = require("fs-nextra");
const colors = require("colors");

exports.id = "setimg";

exports.onLoad = api => {
    api.commands.add("setimg", (msg) => {
        let employeeRole = msg.guild.roles.get("745410836901789749");

        if (msg.member.roles.cache.find(r => r.id === '745410836901789749')) {
            if(msg.channel.id == 746423099871985755) {
                fsn.readJSON("./orders.json").then((orderDB) => {
                    let ticketID = msg.content.substring(9);
                    let order = orderDB[ticketID];

                    // If the order doesn't exist.
                    if(order === undefined) {
                        msg.reply("Couldn't find that order. Try again!");

                        return;
                    }
                    //and the rest of the command's code

所以你定義了employeeRole ,但沒有使用它。 不是詢問用戶是否具有角色employeeRole ,而是在第一個 if 語句中做一些不可能的事情。

if(msg.member.roles.cache.has(employeeRole)) {...}

將您的第一個 if 語句更改為我在這里給您的代碼,它應該被修復。 現在您在詢問用戶是否擁有employeeRole

PS:你需要在你的employeeRole中添加.cache ,所以它必須是

let employeeRole = msg.guild.roles.cache.get("745410836901789749");

暫無
暫無

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

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