首页 > 解决方案 > 将命令的通道列入白名单

问题描述

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

如果有人键入.team buy 1,应该做的是保存来自另一个机器人的数据。

我想将此命令列入由其频道 ID 标识的 2 个特定频道的白名单,如果该消息不在 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);
  }
}

标签: javascriptdiscorddiscord.js

解决方案


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

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

推荐阅读