有没有人知道这个javascript机器人的问题?
窗口(Start.bat)只是打开和关闭。主要错误在100 - 115行! 我在我的discord服务器上使用它,我需要一个机器人来禁止单词。机器人被编辑禁止文字,但现在它不起作用。 (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);