【问题标题】:Type Error: Cannot Read property 'content' of undefined- Discord.js类型错误:无法读取 undefined-Discord.js 的属性“内容”
【发布时间】:2021-01-24 13:06:31
【问题描述】:

我用 discord.js 制作了一个机器人来检测数组 blacklisted 的任何元素何时被提及。它会删除消息并传递警告。我的朋友尝试变通方法并尝试编辑消息以说坏话,(它没有读取)。所以我问了一些朋友,他们建议将检测放在一个函数中,当我使用 client.on 时检测消息更新。我目前在运行时收到错误。

下面是相关代码:

function find(msg) {
 var blacklisted = [
  'words',
  'that',
  'I',
  'cant',
  'say',
  'on',
  'this',
  'website',
 ];

 let foundInText = false;
 for (var i in blacklisted) {
  if (
   msg.content
    .replace(/\s/g, '')
    .replace(/[^A-Za-z0-9]/g, '')
    .toLowerCase()
    .includes(blacklisted[i].toLowerCase())
  )
   foundInText = true;
 }

 if (foundInText) {
  msg.delete();
  msg.reply('Watch your language, dont be a bad boy');
 }
}

client.on('messageUpdate', (oldMessage, newMessage) => {
 find();
});

【问题讨论】:

  • 我之前没有使用过不和谐,但我认为你没有将任何东西传递给 find()。他抱怨味精未定义。
  • 那你觉得我应该怎么做?
  • 您只需要传递msg 参数。将find() 更改为find(newMessage)。另外,为什么你有这么多String.prototype.replace() 函数?据我所知,你不需要它们中的任何一个,
  • 尝试 client.on('messageUpdate',(oldMessage,newMessage) => { find(oldMessage) });
  • @NomairGhanem 我试过了,这次它说“msg is undefined”参考 msg.delete()

标签: javascript node.js discord discord.js


【解决方案1】:

正如 cmets 中所指出的,您需要将消息对象传递给函数。此外,您检查单词的方法并没有真正起作用,并导致机器人简单地删除消息,无论来自 blacklisted 的想要的单词是否在其中。

试试这个

function find(msg) {
 var blacklisted = [
  'words',
  'that',
  'I',
  'cant',
  'say',
  'on',
  'this',
  'website',
 ];

 let foundInText = false;
 // Convert the content of the message into an array
 // Check if blacklisted contains the element
 msg.content.toLowerCase().split(/ +/g).forEach((element) => {
  if (blacklisted.includes(element)) {
   return (foundInText = true);
  }
 });

 if (foundInText) {
  msg.delete();
  msg.reply('Watch your language, dont be a bad boy');
 }
}

client.on('messageUpdate', (oldMessage, newMessage) => {
 // Return here so the bot doesn't react to it's own messages
 if (message.author.bot) return;

 // Pass the newMessage because we need the message after
 // it has been modified
 find(newMessage);
});

client.on('message', (message) => {
 // Return here so the bot doesn't react to it's own messages
 if (message.author.bot) return;

 // Pass the message
 find(message);

 // The rest of your code
});

【讨论】:

  • 我认为 String.prototype.split 函数不是 OP 的最佳选择。例如,如果其中一个词是'hello',那么使用您的方法,'helloo' 仍然可以通过。 OPs 原来的方法有什么问题?
  • 当我测试它时,它一直在删除消息,即使blacklisted 中的单词不在其中。
  • 好吧,我的意思是我的原始方法仍然允许在说密码时使用顽皮的 A 字。因为人们必须能够输入“password”、“pass”、“bass”、“sass”。类似的事情也会触发我的,尽管我并不真正关心这个问题。你的String.prototype.split 还能在words that have symbols and white space to fool it 上工作吗?我只关心那条消息和过去的消息。
  • 嗯,在我看来,在某些时候使用机器人进行基本单词之外的内容审核变得不切实际。如果您希望您的机器人识别具有肮脏含义的符号或表情符号,我们正在转向机器学习,因为人类总是能够从完全无害的单词中做出肮脏或顽皮的联想。话虽如此,如果您将这些符号添加到blacklisted,它应该适用于符号。空格更加困难,因为机器人需要知道哪些字母属于一起,这让我们回到了机器学习。
  • @Worthy Alpaca 在我的原始代码中,我暗示了.replace(/\s/g, '') .replace(/[^A-Za-z0-9]/g, '') .toLowerCase()。我可以在这个上使用它吗?
猜你喜欢
  • 2016-10-17
  • 2017-07-06
  • 2021-07-27
  • 2021-02-16
  • 2023-02-23
  • 2021-08-27
  • 1970-01-01
  • 2021-04-15
  • 2018-06-15
相关资源
最近更新 更多