【问题标题】:Discord bot Error Javascript [closed]Discord bot错误Javascript [关闭]
【发布时间】:2018-03-04 09:23:57
【问题描述】:

有人知道这个 javascript 机器人的问题吗?

窗口 (Start.bat) 只是打开和关闭。主要错误在第 100 - 115 行! 我在我的不和谐服务器上使用它,我需要一个机器人来禁止单词。该机器人被编辑为禁止单词,但现在它不起作用。 (第 100 - 115 行) 我希望有人知道这个问题。

它是为名为 The parrot 的机器人制作的,我需要一个工作代码。

代码:

const Discord = require('discord.js');
const bot = new Discord.Client();
var fs = require("fs");
var oldAuthor;

// ADD YOUR BOT'S TOKEN HERE
const token = " *censored* ";

bot.on('ready', () => {
  
});

bot.on('message', message => {
	// Makes sure the first word is ~createcommand
	var checkMessage = message.content.split(" ");
	if(checkMessage[0] == "~lagkommando")
	{
		// commandText gets grabbed by splitting the string with |
		// commandName gets grabbed by splitting the string with spaces
		// command Name must have '~' in it just so you can't use any word you
		// want
		var commandText = message.content.split("|",2);
		var commandName = message.content.split(" ");
		if(commandName[1].charAt(0) == "~")
			{
				checkExistingCommand(commandText,commandName);
				message.channel.sendMessage("Command " + commandName[1] + " has been created");
			} else {
				message.channel.sendMessage("Command must contain '~'");
			}
	}

	/*
	 * Checks the commands.txt file to see if anyone posted the command.
	 * commands.txt is split with semi-colons. For loop to check every single
	 * command. If there is a match, then it opens up the txt file associate
	 * with that command. If there are multiple pictures then the user should
	 * type $random{} and then type in all the pictures in the brackets
	 * separated by semi-colons. If there is no $random{} then it just sends the
	 * message.
	 */
	fs.readFile('./commands/commands.txt','utf8',function(err,f){
		var com = f.toString().split(";");
		for(i = 0; i < com.length; i++)
		{
			if(message.content == com[i])
			{
				if(com[i] == "~commands")
					{
						message.channel.sendMessage(com);
						break;
					}
				if(com[i] == "~help")
					{
						message.channel.sendMessage("Kommandoer: ~help, ~commands, ~database, ~Hei, ~memes og ~problem");
						break;
					}
				var command = "./commands/" + com[i] + ".txt";
				fs.readFile(command,'utf8', function(err,f){
				try{
					var com2 = f.toString().split(";");
					var num = Math.random() * ((com2.length - 1) - 0) + 0;
					message.channel.sendMessage(com2[Math.floor(num)]);
				}
				catch(err) {
					console.error("",err);
				}
				});
			}
		}
	});
  
});

function checkExistingCommand(commandText,commandName)
{
	var com = commandName[1];
	var desc = commandText[1];
	var CE = false;
	fs.readFile('./commands/commands.txt','utf8',function(err,f){
		var findCommands = f.toString().split(";");
		for(i = 0; i < findCommands.length; i++)
		{
			if(com == findCommands[i])
			{
				CE = true;
			}
		}
		if(CE == true)
		{
			createCommand(desc,true,com);
		} else if (CE == false)
		{
			createCommand(desc,false,com);
		}
	});
	
}

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

    var sender = message.author;
    var msg = message.content.toUpperCase();
    var prefix = '>'

    if (sender.id === ' *Censored* ') { 
    return;
    }

    if (msg.includes('noob')) {
        message.delete();
        message.author.send('The word noob is banned, next time YOU can be banned! ')
    }

}

// Appends and/or creates the text files.
function createCommand(desc,b,com)
{
	var fileName = "./commands/" + com + ".txt";
	if(b == true)
	{
		fs.writeFile(fileName,desc,function(err){
		if(err) {
			return console.error(err);
		}
		});
	} else if (b == false){
		fs.appendFile('./commands/commands.txt',com+';',(err) =>
		{
		if(err) throw err;
		});
		
		fs.writeFile(fileName,desc,function(err){
		if(err) {
			return console.error(err);
		}
		});
	}
	return;
}

bot.login(token);

【问题讨论】:

  • 在 Start.bat 的最后一行添加“pause>nul”以查看错误。
  • 这样吗? "节点 "run.js" pause>nul"
  • 对不起,我的意思是在底部换一个新行,上面什么都没有,但是暂停>Nul
  • 这是出现的文本。 SyntaxError: missing ) 在 Object.runInThisContext (vm.js:97:10) 在 Object.Module._compile (module.js:542:28) 在 Object.Module._extensions 的 createScript (vm.js:56:10) 的参数列表之后..js (module.js:579:10) 在 Module.load (module.js:487:32) 在 tryModuleLoad (module.js:446:12) 在 Function.Module._load (module.js:438:3 ) 在 Module.runMain (module.js:604:10) 在运行时 (bootstrap_node.js:389:7) 在启动时 (bootstrap_node.js:149:9) 我如何阅读它,所以我可以重新编码机器人?

标签: javascript discord


【解决方案1】:

我知道你需要一个更好的文本编辑器。

在第 115 行。(在为您的Client 收到消息的事件处理程序处)。您错过了); 的结束行。

另外,以防万一您不知道,我意识到您将 msg 字符串变量设置为大写,然后再将其与命令的小写字符串进行比较。
请注意 includes()方法区分大小写。

我正在为我的文本编辑器使用 Visual Studio Code。如果它没有显示语法高亮和其他东西,请检查编辑器的右下角,它应该在那里显示 Javascript。

如果没有,请将其更改为 Javascript,它应该会正确显示语法突出显示和内容。

【讨论】:

  • 现在,在 Visual Studio 代码中它是 ----------------- ')' 预期的。 (154, 17)---- 和--- ',' 预期。 (154,1) --- 和 ',' 预期。 (129, 1)
  • 我解决了这个问题...但现在它是 "events.js:216 throw new TypeError('"listener" argument must be a function'); ^"
猜你喜欢
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2017-11-05
  • 2019-11-10
  • 2018-05-02
  • 2021-07-04
  • 2018-02-15
相关资源
最近更新 更多