Discord Bot Command Randomly Crashes

I created a bot command where people, user inputs are saved to a text file. (on my local comp) It works for a while then “randomly” breaks with the error message Uncaught TypeError: Cannot read property ‘content’ of undefined

module.exports = {
    name: 'sea',
    description: 'Takes User Input and saves it as Text file letters.txt',
    execute(message, args) {
      let application = {}
      let filter = (message) => !message.author.bot;
      let options = {
        max: 1,
        time: 15000
      };
      message.member.send("Write something")
      .then(dm => {
        // After each question, we'll setup a collector on the DM channel
        return dm.channel.awaitMessages(filter, options)
      })
      .then(collected => {
        // Convert the collection to an array & get the content from the first element
        application = collected.array()[0].content;
        let appString = application.toString();
        var fs = require('fs')
        fs.appendFile("./see/letters.txt", appString + "\n", function (err){
          if (err) {

          } else {

          }
        })
        // Ask the next question
        console.log(application)
        return message.member.send("Got it, It was recieved")
        
      })
    }
  } 
Console.log: 
!sea
Command name: sea 
Arguments:
Write something
(2) hel
Got it, It was received
!sea
Command name: sea
Arguments:
Write something
Uncaught TypeError: Cannot read property 'content' of undefined

Can you type your code with :
``` js
// code here
```

?

2 Likes

“content” isn’t a property of this array.

module.exports = {
    name: 'sea',
    description: 'Takes User Input and saves it as Text file letters.txt',
    execute(message, args) {
      let application = {}
      let filter = (message) => !message.author.bot;
      let options = {
        max: 1,
        time: 15000
      };
      message.member.send("Write something")
      .then(dm => {
        // After each question, we'll setup a collector on the DM channel
        return dm.channel.awaitMessages(filter, options)
      })
      .then(collected => {
        // Convert the collection to an array & get the content from the first element
        application = collected.array()[0].content;
        let appString = application.toString();
        var fs = require('fs')
        fs.appendFile("./see/letters.txt", appString + "\n", function (err){
          if (err) {

          } else {

          }
        })
        // Ask the next question
        console.log(application)
        return message.member.send("Got it, It was recieved")
        
      })
    }
  }

sorry new to this stuff

1 Like

I removed it and now its says Cannot read property ‘toString’ of undefined

I just saw this too and now new error “Uncaught TypeError: Cannot read property ‘send’ of null”
updated code

module.exports = {
    name: 'ocean',
    description: 'Takes User Input and saves it as Text file letters.txt',
    execute(message, args) {
      let application = {}
      let filter = (message) => !message.author.bot;
      let options = {
        max: 1,
        time: 15000
      };
      message.member.send("Write something")
      .then(dm => {
        // After each question, we'll setup a collector on the DM channel
        return dm.channel.awaitMessages(filter, options)
      })
      .then(collected => {
        // Convert the collection to an array & get the content from the first element
        application = collected.array()[0];
        let fileSearch = Math.floor(Math.random() * 11) + 1
        let appString = application(string);
        var fs = require('fs')
        fs.appendFile(`./see/letters(${fileSearch}).txt`, appString + "\n", function (err){
          if (err) {

          } else {

          }
        })
        // Ask the next question
        console.log(application)
        return message.member.send("Got it, It was recieved")
        
      })
    }
  }

go new error this time “Uncaught TypeError: Cannot read property ‘send’ of null”

message.author.send if you want send it via DM.
message.channel.send if you want send it at current channel.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.