首页 > 解决方案 > 如何让机器人在使用 !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"
}

标签: javascriptdiscord.js

解决方案


你可以运行最后给出代码的硬币并在之前运行所有命令。以及何时!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);
}

推荐阅读