如何修复Node.JS中的javascript语法错误
好的,我正在为我的discord服务器开发一个机器人。这将是一个反垃圾邮件机器人。我发现这个开源的简单反垃圾邮件机器人是用Node.JS编写的 链接到项目,有两个链接:如何修复Node.JS中的javascript语法错误,javascript,node.js,bots,discord,discord.js,Javascript,Node.js,Bots,Discord,Discord.js,好的,我正在为我的discord服务器开发一个机器人。这将是一个反垃圾邮件机器人。我发现这个开源的简单反垃圾邮件机器人是用Node.JS编写的 链接到项目,有两个链接: 当我尝试运行bot.js时,控制台中会出现以下错误: maxDuplicatesWarning = 7,// Maximum amount of duplicate messages a user can send in a timespan before getting warned ^^^^^^^^^^^
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
我已经尝试删除MaxDuplicateSwang行,但它只是给了我一个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;
});
}
}
}
const authors=[];
var=[];
var=[];
var messagelog=[];
/**
*向discord服务器添加简单的垃圾邮件保护。
*@param{Bot}Bot-discord.js客户端/Bot
*@param{object}options-可选(自定义配置选项)
*@return{[type]}[description]
*/
module.exports=功能(机器人,选项){
//设置选项
const warnBuffer=(options&&options.prefix)| | 3;)
常量maxBuffer=(options&&options.prefix)| | 5;)
常量间隔=(选项和选项.interval)| | 1000;)
const warningMessage=(options&&options.warningMessage)| | |“停止播放ellers kutter jeg av deg kuken的垃圾邮件。”;
const banMessage=(options&&options.banMessage)| |“针对垃圾邮件的广告横幅,noen andre som vil?”;
常量maxDuplicatesWarning=(选项和选项.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());
推({
“时间”:现在,
“作者”:msg.author.id
});
messagelog.push({
“消息”:msg.content,
“作者”:msg.author.id
});
//检查同一消息已发送多少次。
var-msgMatch=0;
对于(var i=0;i现在-间隔){
匹配++;
if(matched==warnBuffer&!warning.includes(msg.author.id)){
警告(msg,msg.author.id);
}
else if(匹配==maxBuffer){
如果(!banked.includes(msg.author.id)){
ban(msg,msg.author.id);
}
}
}
else if(作者[i].时间<现在-间隔){
作者:剪接(i);
warned.splice(warned.indexOf(作者[i]);
禁止.splice(警告.indexOf(作者[i]);
}
如果(messagelog.length>=200){
messagelog.shift();
}
}
}
});
/**
*警告用户
*@param{Object}msg
*@param{string}userid userid
*/
函数警告(消息,用户ID){
warned.push(msg.author.id);
msg.channel.send(msg.author+“”+警告消息);
}
/**
*根据用户id禁止用户
*@param{Object}msg
*@param{string}userid userid
*@return{boolean}真或假
*/
功能禁止(消息、用户ID){
对于(var i=0;imember.user.id==msg.author.id);
如果(用户){
user.ban().then((成员)=>{
msg.channel.send(msg.author+“”+banMessage);
返回true;
}).catch(()=>{
msg.channel.send(“du har ikke rettighter tilåbanne”+msg.author+“用于垃圾邮件”);
返回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)
const warnBuffer=(options&&options.prefix)| 3;)
^
SyntaxError:意外标记)
在createScript上(vm.js:80:10)
在Object.runInThisContext(vm.js:139:10)
在模块处编译(Module.js:599:28)
在Object.Module.\u extensions..js(Module.js:646:10)
在Module.load(Module.js:554:32)
在tryModuleLoad时(module.js:497:12)
在Function.Module.\u加载(Module.js:489:3)
at Module.require(Module.js:579:17)
根据需要(内部/module.js:11:18)
反对。(C:\Users\Administrator\Desktop\Bots\fambot\bot.js:9:17)
它应该是:
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!`)
});
尝试使用utf-8字符集。我在我的项目中得到了这样的结果。。您有“=”应该在哪里“:”这是基本的JavaScript。不要盲目地复制和粘贴代码。最好先理解它。@TestHard还有很多错误。请先尝试自己解决它们。我真的建议在尝试这种规模的项目之前,先学习好一点JavaScript——如果你不理解语法,你只会不断地被自己绊倒。或者,如果您想坚持使用它,我建议您将代码缩减到最低限度,并逐节重新引入,这样您就可以看到问题的起因。OMG!你的语法错误太多了!请先检查语法@TestHard-将代码块粘贴到其中并修复它显示给您的所有错误。如何将其更改为utf-8字符?如何在底部屏幕的phpstorm或webstorm中进行更改