首页 > 解决方案 > UnhandledPromiseRejectionWarning:TypeError:无法读取 null 的属性“拆分”

问题描述

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of null
    at C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\commands\tickets\new.js:14:30
    at Map.forEach (<anonymous>)
    at Object.exports.run (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\commands\tickets\new.js:11:34)
    at module.exports (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\events\message.js:584:13)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\JorickJuh\Desktop\Zeltux_v1.3.2\Zeltux_v1.3.2\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
[00:09:04] (node:3876) 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)
[00:09:04] (node:3876) [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.

这是我得到的错误。这是 Nodejs 脚本:

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

exports.run = async (client, message, args) => {
    if(client.config.ticketSystem === "off") return;

    var reason = args.join(" ")
    if(!(reason == "None Specified")){message.delete()}

    const listedChannels = []; 
    message.guild.channels.cache.forEach(channel => {
        channel = client.findChannel(channel.id)
        if(channel.name.startsWith("ticket-")){
            if(channel.topic.split("-")[0] === message.author.id){
                listedChannels.push(channel.id)
            }
        }
    })

    let limitEmbed = new Discord.MessageEmbed()
        .setTitle(client.l.tick.new.limit.replace('%USER%', message.author.username))
        .setColor(client.config.colour)
        .setFooter(client.l.tick.footer.replace('%SERVERNAME%', client.config.serverName).replace('%USER%', message.author.username))

    if(listedChannels.length >= client.config.maxTicketsPerUser){const lim = await message.channel.send(limitEmbed);setTimeout(() => {lim.delete()}, 6000); return}

    if(!reason) return client.missingArguments(client.command, client.l.tick.new.usage)

    let everyone = message.guild.roles.cache.find(x => x.name === "@everyone");

    message.guild.channels.create(`ticket-${message.author.username}`, {type: 'text',
    topic: message.author.id}).then(async c => {

        let roles = []
        client.config.canSeeTicket.forEach(role => roles.push(client.findRole(role)))

        flag = false
        roles.forEach(async role => {c.createOverwrite(role, {SEND_MESSAGES: true, VIEW_CHANNEL: true, EMBED_LINKS: true, ATTACH_FILES: true})})
        
        c.createOverwrite(everyone, {SEND_MESSAGES: false, VIEW_CHANNEL: false, EMBED_LINKS: true, ATTACH_FILES: true})
        c.createOverwrite(message.author, {SEND_MESSAGES: true, VIEW_CHANNEL: true, EMBED_LINKS: true, ATTACH_FILES: true})
        c.setParent(client.ticketCategory)

        fs.appendFile(`${__dirname}/transcripts/transcript-${c.id}.html`, `
        
        <html>

            <head>

                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width">
                <title>Transcript - ticket-${c.id}</title>

            </head>

            <style>

                body {
                    background-color: #bbbbbd;
                    }

                .boxed {
                    border: 1px solid white ;
                    border-radius: 25px;
                    }

                img {
                    border-radius: 50%;
                    width: 50px;
                    padding: 10px;
                    }
                
                p {
                    font-family: "Comic Sans MS", cursive, sans-serif;
                    }

                h1 {
                    font-family: "Comic Sans MS", cursive, sans-serif;
                    }

            </style>

            <body align="middle">
                <h1>
                    <b>${client.config.serverName} Ticket Transcript</b>
                </h1>
                <p>
                    <b>Ticket - </b>#${c.name} <i>(${message.channel.id})</i></br>
                    <b>User - </b>#${message.author.tag} <i>(${message.author.id})</i></br>
                    <b>Opened At - </b>${message.createdAt}</br></br>
                    <b>Reason - </b>${reason}</br>
                </p>
                </br>


        `, function (err) {
            if (err) throw err;
        });

        let openEmbed = new Discord.MessageEmbed()
            .setTitle(client.l.tick.new.created.replace('%USER%', message.author.username))
            .setDescription(client.l.tick.new.location.replace('%LOCATION%', c))
            .setColor(client.config.colour)
            .setFooter(client.l.tick.footer.replace('%SERVERNAME%', client.config.serverName).replace('%USER%', message.author.username))

        if(!(reason == "None Specified")){message.channel.send(openEmbed)}
        else{const fail = await message.channel.send(openEmbed);setTimeout(() => {fail.delete()}, 6000)}

        c.send(`${message.author}`)

        let thanksEmbed = new Discord.MessageEmbed()
            .setTitle(client.l.tick.new.support.replace('%SERVERNAME%', client.config.serverName))
            .setDescription(`${client.l.tick.new.messageLine1}\n${client.l.tick.new.messageLine2}
            \n${client.l.tick.new.messageLine3}\n\n${client.l.tick.new.reason} ${reason}`)
            .setFooter(`${client.l.tick.footer.replace('%SERVERNAME%', client.config.serverName).replace('%USER%', message.author.username)}`, client.user.avatarURL())
            .setColor(client.config.colour)
        c.send(thanksEmbed)

        client.log(client.l.tick.new.log, `${client.l.gen.logs.user} ${message.author} (${message.author.id})\n${client.l.gen.logs.channel} ${message.channel}\n${client.l.gen.logs.ticket} ${c}`)

    })

}

这个机器人使用 Discord.JS。我想不出办法来解决它。

有谁知道解决这个问题?因为我不是很喜欢 Nodejs。我确实知道一些修复,但不是这个。 ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ​‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ​​​​

标签: node.jsdiscorddiscord.js

解决方案


guild.findChannel(channel.id)已弃用。相反,使用guild.channels.cache.get(channel.id)


另外,首先那条线的意义是什么?您正在将通道对象分配给已经是通道对象的变量。这就像在说:

var number = 0
number = number

就我而言,您可以完全删除该行。


推荐阅读