【问题标题】:Discord.js crash upon command "Uncaught TypeError"Discord.js 在命令“Uncaught TypeError”时崩溃
【发布时间】:2021-04-02 14:42:34
【问题描述】:

你好,我是编码和处理 Bot 命令的新手,它接受用户输入并将其保存到文本文件中。但是在加载它时会显示“未捕获 TypeError: Cannot read property 'send' of null”

我对各种事物进行了一些更改:

application =collected.array()[0].content; 当 .content 在那里时,它至少会运行几次然后崩溃 Uncaught TypeError: Cannot read property ‘content’ of undefined

let appString = "" + application原来是用toString()函数

fs.appendFile(./see/letters(${fileSearch}).txt, appString + "\n", function (err) added $fileSearch 因为我认为让很多用户保存到我的本地计算机文件会让人不知所措

我们将不胜感激任何关于这件事的帮助,因为我尝试的每一件事都会让它崩溃,但似乎都是一些 Uncaught TypeError

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;
        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")
        
      })
    }
  }

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    早上好。

    我正在使用您给定的代码进行一些测试,并进行了一些更改。

    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].content;
                let fileSearch = Math.floor(Math.random() * 11) + 1
                let appString = "";
                appString += application;
                var fs = require('fs')
                fs.appendFile(`./see/letters(${fileSearch}).txt`, appString + "\n", function (err) {
                    if (err) {
                        console.log(err);
                    }
                })
                // Ask the next question
                console.log(application)
                return message.member.send("Got it, It was recieved")
            })
     }
    }
    

    首先我想说application = collected.array()[0].content; 对我来说绝对没问题,每次我使用它。

    反正我把let appString = "" + application;改成

      let appString = "";
      appString += application;
    

    它创建了一个变量appString,这是一个字符串,因为我们用""对其进行了初始化。在第二行中,我们用从application 获得的内容填充当前的空字符串。你也可以这样显示它:appString = appString + application。这是因为appString 是空的并且将application 的值放入空字符串中。现在字符串不再为空,并且包含来自application 的内容。这是填充字符串的常用方法。

    我还更改了您的 if else 语句

    fs.appendFile(`./see/letters(${fileSearch}).txt`, appString + "\n", function (err){
          if (err) {
    
          } else {
    
          }
        })
    

    进入

    fs.appendFile(`./see/letters(${fileSearch}).txt`, appString + "\n", function (err) {
                if (err) {
                    console.log(err);
                }
            })
    

    因为你有 if else,但什么也没做。现在,如果出现错误,它会在您的控制台上打印出来。

    我希望这是不稳定的,并有助于解决您的问题。

    【讨论】:

      猜你喜欢
      • 2020-04-07
      • 2021-04-07
      • 2020-04-08
      • 1970-01-01
      • 2021-05-04
      • 2020-09-10
      • 2021-04-04
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多