将命令通道列入白名单

时间:2018-11-17 00:57:32

标签: javascript discord discord.js

我正在开发一个不和谐的机器人。这是自动执行的命令之一。

如果有人键入.team buy 1,然后保存来自另一个漫游器的数据,应该怎么做。

我想将此命令列入2个特定通道的白名单,由它们的通道ID标识,如果该消息不在2个通道中,则忽略该消息。 我该如何编辑代码?

const fs = require("fs");
module.exports.run = (client, message) => {
  if ([509042284793430032, 501784044649054231].includes(message.channel.id)) return;

  try {
    //check if it's a different message //like when a user enters "team buy 234"
    if (message.embeds[0].description.indexOf("❓") === 0) return;
    //retrieve the team data
    var teamData = JSON.parse(fs.readFileSync(client.config.dataFolder + "/teamUpgrades.json", "utf8"));
    //get the current purchases data from the message
    var arr = message.embeds[0].description.split("\n");
    //loop and save the data in "items" object
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == "") continue;
      if (arr[i].indexOf("Unlocks") > -1) continue; //skip locked items
      var opt = arr[i].split("|"); //item's info
      var name = opt[0].trim();
      if (name.indexOf("**") > -1)
        name = name.substring(name.indexOf("**") + 2, name.length - 2).trim(); //bold
      else
        name = name.split(" ")[1]; //not bold
      var price = opt[1].trim();
      price = price.substring(3, price.length - 1);
      price = parseInt(price.split(",").join(""));
      var count = opt[2].trim();
      count = parseInt(count.substring(1, count.length - 2).split(",").join(""));
      var eps = opt[3].trim();
      eps = parseFloat(eps.split(" ")[0].substring(1));
      //if the item doesn't exist, create it
      if (!teamData.items[name]) teamData.items[name] = {};
      teamData.items[name].price = price;
      teamData.items[name].eps = eps;
      teamData.items[name].count = count;
    }
    //the best item to buy, let's give it a very high number first
    var minItem = {
      name: "",
      min: Number.MAX_SAFE_INTEGER
    };
    for (var name in teamData.items) {
      //The average price/eps
      var average = Number(teamData.items[name].price) / Number(teamData.items[name].eps);
      //if the current item is less than the minimum item, replace it.
      if (average < minItem.min) {
        minItem.name = name;
        minItem.min = average;
      }
    }
    //write the current data into the json file
    fs.writeFileSync(client.config.dataFolder + "/teamUpgrades.json", JSON.stringify(teamData));
    message.channel.send(minItem.name);
  } catch (err) {
    console.log(err);
  }
}

1 个答案:

答案 0 :(得分:0)

您可以检查message.channel.id是否等于您的ID之一,否则请忽略它。

module.exports.run = (client, message) => {
  if (['ID 1 here', 'ID 2 here'].includes(message.channel.id)) return;
};