【问题标题】:I'm a beginner in discord botting and I'm using discord.js in node.js in visual studio. I can't figure out how to solve this error我是 discord botting 的初学者,我在 Visual Studio 的 node.js 中使用 discord.js。我不知道如何解决这个错误
【发布时间】:2020-10-03 00:58:45
【问题描述】:

我正在尝试创建一个执行此操作的不和谐机器人

如果消息中*有趣,它会将其从消息中分离出来,并发送与字符串“我不是爸爸。我是坏不和谐机器人”相结合的所有其他内容

const Discord = require('discord.js');
const bot2 = new Discord.Client();

const token2 = 'Not showing my token';
const mark = '*';
bot2.on('message', msg =>{
   let args = msg.content.substring(mark.length).split(" ")
   if(args[0] === "interesting"){
      let argus = args.content.substring("interesting".length).split(" ")
      var thee = concat(argus, " I'm not dad. I'm bad discord bot")
      msg.channel.send(thee)
   }


})




bot2.login(token2)

我更改了令牌,所以这里不显示。这是我运行此程序时遇到的错误。 c:\Users\Artashes\Desktop\bots\letus\index.js:9 让 argus = args.content.substring("interesting".length).split("") ^

TypeError: 无法读取未定义的属性“子字符串”

我终于明白没有定义的东西是 args.content。

我尝试将其更改为 args 无济于事。

我也尝试过 args.prototype 但出现了同样的错误。

我该如何解决这个错误

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    这一行是你的问题:

    let argus = args.content.substring("interesting".length).split(" ")
    

    您使用的是args 而不是msg。因为args 是一个数组,所以字段.content 返回undefined。如果您希望对用户发送的消息进行子串化,请执行msg.content.substring(.....

    虽然我在这里,但我也应该添加

    1. concat 不是一种可以突然调用的方法。你需要像这样使用它:array.concat(stringOrArray)
    2. 您需要向msg.channel.send(...) 提供一个字符串,您可以在字符串数组上使用.join(" ") 来实现。

    我强烈建议你看看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

    翻译成你的代码

    let argus = args.content.substring("interesting".length).split(" ")
    // Changed to
    let argus = msg.content.substring("interesting".length).split(" ");
    
    var thee = concat(argus, " I'm not dad. I'm bad discord bot")
    // Changed to
    var thee = argus.concat(" I'm not dad. I'm bad discord bot").join(" ");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2014-10-31
      • 2023-03-20
      • 2023-01-22
      • 1970-01-01
      相关资源
      最近更新 更多