(узел: 24) Необработанное promiserejectionwarning: ошибка типа: не удается прочитать свойство «отправить» с нулевым значением

#discord.js

#discord.js

Вопрос:

Ошибка:

    (node:24) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of null
        at Client.<anonymous> (/home/container/index.js:49:16)
        at Client.emit (events.js:314:20)
        at Object.module.exports [as GUILD_CREATE] (/home/container/node_modules/discord.js/src/client/websocket/handlers/GUILD_CREATE.js:33:14)
        at WebSocketManager.handlePacket (/home/container/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
        at WebSocketShard.onPacket (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
        at WebSocketShard.onMessage (/home/container/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
        at WebSocket.onMessage (/home/container/node_modules/ws/lib/event-target.js:125:16)
        at WebSocket.emit (events.js:314:20)
        at Receiver.receiverOnMessage (/home/container/node_modules/ws/lib/websocket.js:797:20)
        at Receiver.emit (events.js:314:20)
    (node:24) 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:24) [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.
 

Код:

 client.on("guildCreate", guild => {
   guild.owner.send("Thanks! You can use /help to discover commands. Don't forget to join our discord! https://discord.gg/EnR4PXK")
   client.channels.cache.get(`771736722894618685`).send(`PlayMusic zit nu ook in *${guild.name}* (${guild.id}), De eigenaar is: ${guild.owner.user.tag} (${guild.owner.user.id}) (<@${guild.owner.user.id}>) . Deze server heeft *${guild.memberCount}* leden! :tada: ** De bot zit nu in ${client.guilds.cache.size} servers** `)
   console.log(`PlayMusic zit nu ook in ${guild.name} (${guild.id}), De eigenaar is: ${guild.owner.user.username} (${guild.owner.user.id}). Deze server heeft ${guild.memberCount} leden!`);
   channel.createInvite({unique: true})
});
 

Во-первых, он проработал месяц, а затем я получил эту ошибку? Кто-нибудь знает правильный способ создать что-то подобное?

Ответ №1:

Guild#owner является обнуляемым свойством, которое зависит от кэша. Извлеките владельца с помощью GuildMemberManager#fetch() using Guild#ownerID для принудительного кэширования пользовательского объекта.

 // make sure to make your function async!
const owner = await client.users.fetch(guild.ownerID);
owner.send(...);