javascript - 将命令的通道列入白名单
问题描述
我正在制作一个不和谐的机器人。这是自动执行的命令之一。
如果有人键入.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);
}
}
解决方案
您可以检查是否message.channel.id
等于您的 ID 之一,如果不等于,请忽略它。
module.exports.run = (client, message) => {
if (['ID 1 here', 'ID 2 here'].includes(message.channel.id)) return;
};
推荐阅读
- jboss - 无法访问 Drools 中的嵌套对象
- node.js - 如何处理事件发射器节点js中的流
- tensorflow - 字典观察空间 Acme DQN 代理
- laravel - 如何通过 public_id 获取 cloudinary 文件 url/其他属性?
- python - Python - 保存具有多个标签的数组的有效方法
- laravel - 从 Laravel 发送数组并使用 axios 在 Vuejs 中获取对象
- vue.js - Datagrid DxExport“fileName”属性更改了我传递给它的日期格式
- vuepress - 从 VuePress 中的自定义组件链接到图像
- wordpress - 无法解析 acf 字段中的简码(wordpress)
- mongodb - 使用 Mongo 聚合计算潜在成本