Answer the question
In order to leave comments, you need to log in
What to do with an error when sending a PM message to a discord.js member?
Throws a TypeError when sending a message to a participant, sends it 1 time, and immediately throws an error, moreover, from the main file. Immediately I say I wrote a message to the bot not in a personal. My code:
const Discord = module.require('discord.js')
const fs = require('fs')
const { send } = require('process')
module.exports.run = async (client,message,args) => {
let rUser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (rUser){
rUser.createDM()
rUser.send("hh")
}
}
module.exports.help = {
name: 'тест'
}
const Discord = require('discord.js')
const fs = require('fs')
const client = new Discord.Client()
const config = require('./config.json')
let prefixes = require('./prefixes.json')
const fetch = require('node-fetch')
let profile = require('./profile.json')
client.commands = new Discord.Collection()
fs.readdir('./commands', (err, files) => {
if (err) console.log(err)
let jsfile = files.filter(f => f.split('.').pop() === 'js')
if (jsfile.length <= 0) return message.channel.send("Команды не найдены!")
console.log(`Загружено ${jsfile.length} команд`)
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`)
client.commands.set(props.help.name, props)
})
})
client.on('ready', () => {
console.log(`Бот ${client.user.username} запустился`);
client.user.setPresence({
status: 'online',
activity: {
type: 'STREAMING',
name: '.help',
}
})
})
client.on('message', message => {
let sid = message.guild.id
let uid = message.author.id
if(!prefixes[sid]){
prefixes[sid] ={
prefix:'.',
};
};
let prefix = prefixes[sid].prefix
fs.writeFile('./prefixes.json',JSON.stringify(prefixes),(err)=>{
if (err) console.log(err)
})
if(!profile[uid]){
profile[uid] ={
warns:0,
};
};
fs.writeFile('./profile.json',JSON.stringify(profile),(err)=>{
if (err) console.log(err)
})
if (!message.content.startsWith(prefix) || message.author.bot) return;
let messageArray = message.content.split(' ')
let command = messageArray[0]
let args = messageArray.slice(1)
let command_file = client.commands.get(command.slice(prefix.length))
if (command_file) command_file.run(client, message, args, prefix)
})
client.login(config.token)
let sid = message.guild.id
^
TypeError: Cannot read property 'id' of null
at Client.<anonymous> (C:\Users\User\MyBot\bot.js:35:29)
at Client.emit (events.js:376:20)
at MessageCreateAction.handle (C:\Users\User\MyBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\User\MyBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\User\MyBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\User\MyBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\User\MyBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\User\MyBot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (events.js:376:20)
at Receiver.receiverOnMessage (C:\Users\User\MyBot\node_modules\ws\lib\websocket.js:834:20)
Answer the question
In order to leave comments, you need to log in
According to the documentation , the field guild
is optional and only matters if the message was sent in a guild channel. Judging by the error, it is in your case that this field has the value null
, but you are still trying to get the value from it id
. Fixed by verification . Also, I draw attention to the fact that the event y is . if(message.guild !== null)
message
Discord.Client
deprecated
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question