javascript - 如何让机器人在使用 !coins 命令时忽略给我硬币?
问题描述
所以我在做硬币系统,但我遇到了一个小问题。通常,每当我输入一些东西时,我都会得到 1 个硬币,但是当我使用 !coins 命令检查我有多少硬币时,我不希望机器人给我硬币。我希望它在使用此命令时忽略给我硬币。那么我应该添加什么?
这是我在 index.js 中的代码:
if (message.channel.id === "528734148718886922"){
if(!coins[message.author.id]){
coins[message.author.id] = {
coins: 0
};
}
let coinAmt = Math.floor(Math.random() * 1) + 1;
let baseAmt = Math.floor(Math.random() * 1) + 1;
console.log(`${coinAmt}; ${baseAmt}`);
if(coinAmt === baseAmt){
coins[message.author.id] = {
coins: coins[message.author.id].coins + coinAmt
};
fs.writeFile("./coins.json", JSON.stringify(coins), (err) => {
if (err) console.log(err)
});
let coinEmbed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#6666ff")
.addField("", `You earned ${coinAmt} Scrap`)
message.channel.send(coinEmbed).then(message => {message.delete(100000)});
}
}
coin.js 文件:
const Discord = require("discord.js");
let coins = require("../coins.json");
module.exports.run = async (bot, message, args) =>{
//coins
if(!coins[message.author.id]){
coins[message.author.id] = {
coins: 0
};
}
let uCoins = coins[message.author.id].coins;
let coinEmbed = new Discord.RichEmbed()
.setAuthor(message.author.username)
.setColor("#6666ff")
.addField("", uCoins);
message.channel.send(coinEmbed).then(message => {message.delete(50000)});
}
module.exports.help = {
name: "coins"
}
解决方案
你可以运行最后给出代码的硬币并在之前运行所有命令。以及何时!coins
运行。您可以跳过硬币代码。
var giveCoins = true;
function giveCoinsFunction(ID){...}
function ranWhenMessageIsSent(m){
giveCoins = true;
if(m.content.toLowerCase() === `!coins`){
giveCoins = false;
coins.run(bot,m,m.content.toLowerCase().split(" ");
}
if(giveCoins) giveCoinsFunction(m.author.id);
}
推荐阅读
- angular - Jhipster 启动非常慢 - 启动空白引导应用程序需要 2 小时
- python - 使用 Pandas(Python) 从 Google Drive 读取非常大的 csv 文件
- sql - 如何在sql中找到count的最大值和最小值
- javascript - 如何在 Selenium Webdriver Javascript 中保存为网页?
- flutter - Flutter - 如何在多个地方重用一些代码
- elasticsearch - 如何在弹性搜索中匹配两个索引之间的数据
- amazon-web-services - 如何引用 AWS CDK 中的现有 VPC 终端节点?
- syntax-error - Pine 脚本问题第 10 行:输入“行尾没有续行”时出现语法错误
- python - Python PIP 卡在已卸载的 python 版本中
- angular - ngx-admin NbDummyAuthStrategy 登录后重定向