V
V
Vlad Morkovkin2021-06-30 01:31:12
JavaScript
Vlad Morkovkin, 2021-06-30 01:31:12

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: 'тест'
}

My main file is where the error comes from:
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)

Gives the following error:
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

1 answer(s)
I
Ivan Chernyshev, 2021-07-07
@Vlad808

According to the documentation , the field guildis 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)messageDiscord.Clientdeprecated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question