2017-09-04 99 views
1

你好我目前使用Discord.js如何使用Node.jsDiscord.js问题试图创建一个ADDMOD命令

我想创建在命令行中我自己不和谐的机器人和测试,如果有人可以帮助我创建一个只有“Mods”可以使用的命令,我在谈论机器人mod,所以机器人的所有者可以做+ addmod USER将该用户添加到机器人将从中拉出来的一些数据文件以检查用户是否在它上面运行了“Mod only”命令。

链接到源代码:https://ghostbin.com/paste/tnovx

+0

我有点困惑,当一个MOD命令输入聊天时,机器人会运行一些东西,对吗?如果可以,我可以帮忙吗? – turmuka

+0

@turmuka是的,但用户必须使用+ addmod USERNAME添加到mod列表中,然后bot会检查它们是否在文本文件或so​​mthing上。 – Ryan

回答

0

这不是复杂。如果您想从文本文件中检查,请使用fs模块并将其与机器人功能结合使用。我希望这能够让你对如何完成这件事有一点认识。

var fs = require('fs'); 
const Discord = require('discord.js'); 
var client = new Discord.Client(); 

client.login('[email protected]', 'password', callbackOutput); 

//get data from file 
var fileData = fs.readFileSync(filename); //put eachname to a new line 
var userNames = filedata.toString().split('\n'); // gets all the names in an array from all lines 

client.on('message', function(message) { 
    if(message == "myCommand"){ 
     //handle command stuff 

     for(var i = 0; i < userNames.length; i++){ 
      if(userNames[i] == message.author.name){ 
      var theUserIsMod = true; 
      console.log(message.author); //info about sender 
      message.author.userIsMod = true; //set the attribute userIsMod for later 
      } 
     } 

     if(theUserIsMod == true){ 
      //if the user is mod do more stuff 
     } 
     else{ 
      console.log("user is not mod"); 
      return false; //exit the program 
     } 
    }else{ 
     console.log("command not found"); 
    } 
}); 

你的txt文件应该是这样的,把他们都到一个新行,因为我已经使用split()功能,或者如果你有更好的主意,你可以使用太

users.txt

user1Name 
Josh 
thePredator 
user2