【问题标题】:Problems trying to create a addmod command尝试创建 addmod 命令时出现问题
【发布时间】:2018-02-13 03:45:20
【问题描述】:

您好,我目前正在使用 Discord.js 创建自己的 Discord 机器人并使用 node.js 在命令行中进行测试

我想知道是否有人可以帮助我创建一个只有“Mods”才能使用的命令,我说的是机器人模块,所以机器人的所有者可以 +addmod USER 将该用户添加到机器人将使用的一些数据文件中pull from 以检查用户在运行“Mod only”命令时是否在上面。

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

【问题讨论】:

  • 我有点困惑,当一个模组在聊天中输入command 时,机器人会运行一些东西,对吗?如果可以,我可以帮忙吗?
  • @turmuka 是的,但是必须使用 +addmod USERNAME 将用户添加到 mod 列表中,然后机器人将检查他们是否在文本文件或其他东西上。

标签: node.js discord discord.js


【解决方案1】:

这并不复杂。如果要从文本文件中检查,请使用fs 模块并将其与机器人功能结合使用。我希望这能让您对如何做到这一点有所了解。

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

client.login('mybot@example.com', '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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2021-04-01
    • 2019-04-19
    • 2014-08-01
    • 2020-09-02
    • 1970-01-01
    相关资源
    最近更新 更多