提问者:小点点

我是discord botting的初学者,我正在visual Studio中使用node.js中的discord.js。我想不出如何解决这个错误


我想制造一个不和谐的机器人

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

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(“international”。length)。split(“”)^

TypeError:无法读取未定义的属性“substring”

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

我试着把它改成args,但没有用。

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

如何修复此错误


共1个答案

匿名用户

这行是你的问题:

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

您使用的是args而不是msg。由于args是数组,因此字段.content返回未定义。如果要查找用户发送的消息的子字符串,请执行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(" ");