【问题标题】:discord.js spam command troublesdiscord.js 垃圾邮件命令问题
【发布时间】:2018-07-14 19:40:31
【问题描述】:
  bot.on('message', message => {
    if (message.content === 'spam') {
        message.channel.send('spam');
        while (message.channel.send('spam')) {
            if (message.content === 'stop spam') {
                return message.channel.send('stopped');
            }
        }
    }
});

我对 javascript 还是很陌生,所以我不确定这是否可能像我尝试过的那样,我查看了 w3schools developers.mozilla,甚至还有一些已经在这里提出的问题;我尝试使用 do while 和 for 循环,并且我尝试了多个版本的代码

最终目标是,如果用户发送“垃圾邮件”这个词,机器人应该不断发送“垃圾邮件”这个词并一直这样做,直到机器人被关闭或用户发送“停止垃圾邮件”这个词

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    以下是您应该了解的有关您正在使用的代码的一些事项:

    1. message.channel.send 返回一个Promise,所以你不能把它放在while 循环中,因为你需要有truefalseBoolean)的东西。
    2. 现在您正在尝试检查消息的内容是否等于'stop spam',而您在if-statement 中检查内容是否等于'spam' - 所以你永远不会进入内部if-statement

    我建议多练习一些基本的 javascript,然后转向 Node.js,然后再回到 Discord.js - 但是,看到垃圾邮件发送者的工作对你来说可能很酷,所以我写了一些垃圾邮件代码给你可以使用 - 看看这个:

    首先,创建一个名为spamCtrl.js 的新文件,如下所示(有关发生情况的说明,请参见代码中的 cmets):

    let spamming = false;
    let spamChannel = undefined;
    
    // spam function repeats until variable spamming is false
    function spam() {
        return new Promise((resolve, reject) => {
    
            // add check to make sure discord channel exists
            if (!spamChannel)
                reject('Channel is undefined!');
    
            // send message on spam channel
            spamChannel.send('spam')
            .then(msg => {
                // wait 100 ms until sending next spam message
                setTimeout(() => {
                    // continue spamming if spamming variable is true
                    if (spamming) {
                        spam()
                        .then(resolve) // not entirely necessary, but good practice
                        .catch(console.log); // log error to console in case one shows up
                    }
    
                    // otherwise, just resolve promise to end this looping
                    else {
                        resolve();
                    }
                }, 100)
            })
            .catch(console.log);
    
        });
    }
    
    // public functions that will be used in your index.js file
    module.exports = {
        // pass in discord.js channel for spam function
        setChannel: function(channel) {
            spamChannel = channel;
        },
    
        // set spam status (true = start spamming, false = stop spamming)
        setStatus: function (statusFlag) {
            // get current status
            let currentStatus = spamming;
    
            // update spamming flag
            spamming = statusFlag;
    
            // if spamming should start, and it hasn't started already, call spam()
            if (statusFlag && currentStatus != statusFlag) {
                spam();
            }
        },
    
        // not used in my commands, but you may find this useful somewhere
        getStatus: function() {
            return spamming;
        }
    };
    

    接下来,将该文件导入您的 index.js 文件(必须与您的 spamCtrl.js 文件位于同一目录中 - 除非您更改下面的 require 语句)。

    // in index.js file, get controller for spam messages
    let spamCtrl = require('./spamCtrl');
    

    最后一步:在您的 index.js 文件(或您处理垃圾邮件命令的任何位置)中设置您的命令(可以随意重命名):

    // 2 commands together make spamming work :)
    case '?SPAM':
        spamCtrl.setChannel(message.channel);
        spamCtrl.setStatus(true);
        break;
    case '?STOP-SPAM':
        spamCtrl.setStatus(false);
        break;
    

    如果您想对任何事情进行任何额外的解释,或者如果您想在这里和那里看到一些调整,请告诉我。

    【讨论】:

      【解决方案2】:

      尝试改用变量。您不能在 while 循环中使用 message.channel.send('spam')

      var spam = false;
      if (message.content === 'spam') {
          if (message.author.id !== bot.user.id) { // Replace bot with the instance of your bot Client.
              spam = true;
          } else {
              if(spam) {
                  message.channel.send('spam');
              }
          }
          if (message.content === 'stop spam') {
              if(spam) {
                  message.channel.send('stopped');
              }
              spam = false;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-23
        • 2015-05-18
        • 1970-01-01
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        相关资源
        最近更新 更多