【问题标题】:How do I fix javascript SyntaxError in Node.JS如何修复 Node.JS 中的 javascript SyntaxError
【发布时间】:2018-05-09 09:08:30
【问题描述】:

好的,我目前正在为我的 discord 服务器开发一个机器人。这将是一个反垃圾邮件机器人。我发现这个用 Node.JS 编写的开源简单反垃圾邮件机器人。

项目链接,有两个链接:

当我尝试运行 bot.js 时,我在控制台中收到此错误:

    maxDuplicatesWarning = 7,// Maximum amount of duplicate messages a user can send in a timespan before getting warned
  ^^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid shorthand property initializer
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

我尝试删除 maxDuplicatesWarning 行,但它只是给我一个 maxDuplicatesBan 错误。

这是我的 bot.js:

const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = "!"

client.on('ready', () => {
  console.log(`Bot service launched. Bot ${client.user.tag} is successfully activated!`);
client.user.setStatus("dnd");
client.user.setGame('Fidget Spinner');

var anti_spam = require("discord-anti-spam");

antispam(bot, {
  warnBuffer: 3, //Maximum amount of messages allowed to send in the interval time before getting warned.
  maxBuffer: 5, // Maximum amount of messages allowed to send in the interval time before getting banned.
  interval: 1000, // Amount of time in ms users can send a maximum of the maxBuffer variable before getting banned.
  warningMessage: "stopp å spamme eller kutter jeg av deg kuken.", // Warning message send to the user indicating they are going to fast.
  banMessage: "har blitt bannet for spamming, noen andre som vil?", // Ban message, always tags the banned user in front of it.
  maxDuplicatesWarning = 7,// Maximum amount of duplicate messages a user can send in a timespan before getting warned
  maxDuplicatesBan = 10 // Maximum amount of duplicate messages a user can send in a timespan before getting banned
});

client.login('private');

这里是 anti_spam.js:

const authors = [];
var warned = [];
var banned = [];
var messagelog = [];

/**
 * Add simple spam protection to your discord server.
 * @param  {Bot} bot - The discord.js CLient/bot
 * @param  {object} options - Optional (Custom configuarion options)
 * @return {[type]}         [description]
 */
module.exports = function (bot, options) {
  // Set options
  const warnBuffer = (options && options.prefix) || 3;)
  const maxBuffer = (options && options.prefix) || 5;)
  const interval = (options && options.interval) || 1000;)
  const warningMessage = (options && options.warningMessage) || "stop å spamme ellers kutter jeg av deg kuken.";
  const banMessage = (options && options.banMessage) || "har blit bannet for spamming, noen andre som vil?";
  const maxDuplicatesWarning = (options && options.duplicates || 7;)
  const maxDuplicatesBan = (options && options.duplicates || 10;)

  bot.on('message', msg => {

    if(msg.author.id != bot.user.id){
      var now = Math.floor(Date.now());
      authors.push({
        "time": now,
        "author": msg.author.id
      });
      messagelog.push({
        "message": msg.content,
        "author": msg.author.id
      });

      // Check how many times the same message has been sent.
      var msgMatch = 0;
      for (var i = 0; i < messagelog.length; i++) {
        if (messagelog[i].message == msg.content && (messagelog[i].author == msg.author.id) && (msg.author.id !== bot.user.id)) {
          msgMatch++;
        }
      }
      // Check matched count
      if (msgMatch == maxDuplicatesWarning && !warned.includes(msg.author.id)) {
        warn(msg, msg.author.id);
      }
      if (msgMatch == maxDuplicatesBan && !banned.includes(msg.author.id)) {
        ban(msg, msg.author.id);
      }

      matched = 0;

      for (var i = 0; i < authors.length; i++) {
        if (authors[i].time > now - interval) {
          matched++;
          if (matched == warnBuffer && !warned.includes(msg.author.id)) {
            warn(msg, msg.author.id);
          }
          else if (matched == maxBuffer) {
            if (!banned.includes(msg.author.id)) {
              ban(msg, msg.author.id);
            }
          }
        }
        else if (authors[i].time < now - interval) {
          authors.splice(i);
          warned.splice(warned.indexOf(authors[i]));
          banned.splice(warned.indexOf(authors[i]));
        }
        if (messagelog.length >= 200) {
          messagelog.shift();
        }
      }
    }
  });

  /**
   * Warn a user
   * @param  {Object} msg
   * @param  {string} userid userid
   */
  function warn(msg, userid) {
    warned.push(msg.author.id);
    msg.channel.send(msg.author + " " + warningMessage);
  }

  /**
   * Ban a user by the user id
   * @param  {Object} msg
   * @param  {string} userid userid
   * @return {boolean} True or False
   */
  function ban(msg, userid) {
    for (var i = 0; i < messagelog.length; i++) {
      if (messagelog[i].author == msg.author.id) {
        messagelog.splice(i);

      }
    }

    banned.push(msg.author.id);

    var user = msg.channel.guild.members.find(member => member.user.id === msg.author.id);
    if (user) {
      user.ban().then((member) => {
        msg.channel.send(msg.author + " " +banMessage);
        return true;
     }).catch(() => {
        msg.channel.send("du har ikke rettigheter til å banne " + msg.author + " for spamming.");
        return false;
     });
    }
  }

}

有人可以帮我解决这个问题吗? 谢谢:D

新更新!

现在我收到此错误:

const warnBuffer = (options && options.prefix) || 3;)
                                                      ^

SyntaxError: Unexpected token )
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\Users\Administrator\Desktop\Bots\fambot\bot.js:9:17)

【问题讨论】:

  • 老兄......你有“=”应该是“:”
  • 这是基本的 JavaScript。不要盲目复制粘贴代码。最好先了解一下。
  • @TestHard 还有很多错误。请先尝试自己解决。
  • 我真的建议在尝试这种规模的项目之前更好地学习 JavaScript - 如果你不理解语法,你只会经常被自己绊倒。或者,如果您想坚持使用它,我建议您将代码缩减到最低限度并逐节重新引入,这样您就可以看到问题出在哪里。

标签: javascript node.js bots discord discord.js


【解决方案1】:

应该是:

antispam(bot, {
  warnBuffer: 3, //Maximum amount of messages allowed to send in the interval time before getting warned.
  maxBuffer: 5, // Maximum amount of messages allowed to send in the interval time before getting banned.
  interval: 1000, // Amount of time in ms users can send a maximum of the maxBuffer variable before getting banned.
  warningMessage: "stopp å spamme eller kutter jeg av deg kuken.", // Warning message send to the user indicating they are going to fast.
  banMessage: "har blitt bannet for spamming, noen andre som vil?", // Ban message, always tags the banned user in front of it.
  maxDuplicatesWarning: 7,// Maximum amount of duplicate messages a user can send in a timespan before getting warned
  maxDuplicatesBan: 10 // Maximum amount of duplicate messages a user can send in a timespan before getting banned
});

您必须使用: 而不是= 为对象中的键赋值

更新:

client.on('ready', () => {
  console.log(`Bot service launched. Bot ${client.user.tag} is successfully activated!`)
});

【讨论】:

  • 天啊!你有太多的语法错误!请先检查您的语法!
  • @TestHard - 将代码块粘贴到 jshint.com 并修复它显示的所有错误。
【解决方案2】:

尝试使用 utf-8 字符集。我在我的项目中遇到了这样的情况

【讨论】:

  • 如何将其更改为 utf-8 字符?
  • 我在底部屏幕中更改了 phpstorm 或 webstorm
猜你喜欢
  • 2016-09-01
  • 2022-06-27
  • 1970-01-01
  • 2019-08-29
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 2020-03-03
  • 2017-11-05
相关资源
最近更新 更多