Discord.js, cannot read property “user” of null

送分小仙女□ 提交于 2021-02-17 04:50:05

问题


I have a trouble, that I can't understand at all. The function working on my server, and not working on another one. Here's my code:

const user = message.author;
let servericon = message.guild.iconURL;
let serverembed = new Discord.MessageEmbed()
.setAuthor(message.author.username, auser.displayAvatarURL({ format: 'png' }))
.setTitle("Информация о сервере")
.setColor("RANDOM")
.setThumbnail(servericon)
.addField("Название сервера :", message.guild.name)
.addField("Владелец :", `${message.guild.owner.user.tag}`, true)
.addField("Каналы :", message.guild.channels.cache.size, true)
.addField("Роли :", message.guild.roles.cache.size, true)
.addField("Сервер создан :", `${moment(message.guild.createdAt).format('MMMM Do YYYY')}`)
.addField("Пользователей :", message.guild.memberCount)
.setThumbnail(message.guild.iconURL())
.setTimestamp()
message.channel.send(serverembed);

And error is:
(node:11288) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'user' of null at Object.run (C:\Users\alumetryu\Desktop\bot\commands\info\serverinfo.js:20:48) at Client. (C:\Users\alumetryu\Desktop\bot\ame\index.js:38:17) at Client.emit (events.js:315:20) at MessageCreateAction.handle (C:\Users\alumetryu\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (C:\Users\alumetryu\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\alumetryu\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31) at WebSocketShard.onPacket (C:\Users\alumetryu\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (C:\Users\alumetryu\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users\alumetryu\node_modules\ws\lib\event-target.js:125:16) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (C:\Users\alumetryu\node_modules\ws\lib\websocket.js:797:20) at Receiver.emit (events.js:315:20) at Receiver.dataMessage (C:\Users\alumetryu\node_modules\ws\lib\receiver.js:437:14) at Receiver.getData (C:\Users\alumetryu\node_modules\ws\lib\receiver.js:367:17) at Receiver.startLoop (C:\Users\alumetryu\node_modules\ws\lib\receiver.js:143:22) at Receiver._write (C:\Users\alumetryu\node_modules\ws\lib\receiver.js:78:10) at doWrite (_stream_writable.js:403:12) at writeOrBuffer (_stream_writable.js:387:5) at Receiver.Writable.write (_stream_writable.js:318:11) at TLSSocket.socketOnData (C:\Users\alumetryu\node_modules\ws\lib\websocket.js:872:35) at TLSSocket.emit (events.js:315:20) at addChunk (_stream_readable.js:295:12) (node:11288) 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:11288) [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.


回答1:


This is because the owner of the guild isn't cached. Instead, manually fetch() the GuildMember with Guild.prototype.ownerID

// make sure the function is async!
const owner = await message.guild.members.fetch(message.guild.ownerID);
const serverembed = new Discord.MessageEmbed().addField(
 'Владелец :',
 `${message.guild.owner.user.tag}`,
 true
);



回答2:


I'm not sure exactly which line the problem is because the lines are not numbered in stackoverflow, but I'm assuming that the problem is in the line that says .addField("Владелец :", ${message.guild.owner.user.tag} , true) - I'm guessing you're trying to get the owner, if so, here's how message.guild.owner - By the way, for anyone who doesn't know, guild is basically a server. So each guild, is each server the bot is on. message.guild, means the guild/server the message was sent from. Taking it even more deeper, message.guild.owner, is the guild/servers owner. If you have any questions feel free to reply to this post



来源:https://stackoverflow.com/questions/65006931/discord-js-cannot-read-property-user-of-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!