2017-09-04 100 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