亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在discord.js 中創建定時布爾值

如何在discord.js 中創建定時布爾值

泛舟湖上清波郎朗 2023-06-29 22:37:01
例如,每當有人說“早上好”時,我希望我的機器人回復“早上好”。我已經弄清楚了所有這些,但現在我想在上面放一個計時器,它變得有點復雜。我希望計時器適用于服務器上的所有人。例如,如果有人說早上好,那么我們讓機器人等待 3 秒鐘。我嘗試了幾種不同的解決方案,但沒有一個有效,所以我想看看是否可以在這里獲得任何幫助。let goodmorning = true;client.on("message", (message) => {if (!message.author.bot) {     if (message.content == "good morning") {            if (goodmorning == true) {                message.channel.send("Good morning");                setInterval(() =>                goodmorning = false, 3000);            } else {                setTimeout(goodmorning = true, 3000);                }     }}});我還嘗試了在網上找到的其他解決方案。我在第 3 行收到“語法錯誤:意外標識符”,盡管第 5 行非常相似:client.on("message", (message) => {if (!message.author.bot) {long lastTrueTime;boolean timedgm() {        long now = System.currentTimeMillis();     if (message.content == "good morning") {            lastTrueTime = now;            return true;        }        if (lastTrueTime+3000<now)            return false;        return true;}        message.channel.send("Good morning");  }});感謝您提前提供的所有幫助。
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

看來您想要此命令的全局冷卻時間。


這可以通過立即將值設置為 false,然后 3000 毫秒后將值恢復為 true 來輕松完成


let goodmorning = true;

client.on("message", (message) => {

  if (!message.author.bot) {

    if (message.content == "good morning") {

      if (goodmorning == true) {

        message.channel.send("Good morning");

        goodmorning = false;

        setTimeout(() => goodmorning = true, 3000);

      }

    }

  }

});


查看完整回答
反對 回復 2023-06-29
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號