简体   繁体   English

让discord bot只用一个命令发送许多消息

[英]Have a discord bot send many messages with only one command

I am making a cultist bot for discord, and I want it to send a message that repeats a message five times when an appropriate command is given !praise . 我正在制作一个不和谐的文化机器人,我希望它在给出适当的命令时发送一条重复消息五次的消息!赞

All the other commands run correctly (below I put one such command, !saviour , as an example), however, the !praise command never gives any output. 所有其他命令都正确运行(下面我放了一个这样的命令, `救世主 ,作为一个例子),然而, !praise命令永远不会给出任何输出。

I was hoping it would iterate through the loop, sending a message each time, as you would expect from a for loop. 我希望它会遍历循环,每次都发送一条消息,正如你for循环所期望的那样。

What stops the loop from running and how can I fix it? 什么阻止循环运行,我该如何解决?

bot.on('message', function(user, userID, channelID, message, evt) {
  if (message.substring(0, 1) === '!') {
    let args = message.substring(1).split(' ');
    let cmd = args[0];

    args = args.splice(1);
    switch (cmd) {
      case 'saviour':
        bot.sendMessage({
          to: channelID,
          message: 'Our current lord and saviour is named asghjahero'
        });
        break; //the above case works fine
      case 'praise':
        for (let i = 1; i === 5; i++) {
          bot.sendMessage({
            to: channelID,
            message: 'All Hail!'
          });
        }
        break;
    }
  }
});

要回答标题中的问题,您只需在侦听器中运行bot.sendMessage两次。

Take a look at the condition in your for loop ( i === 5 ). 看看for循环中的条件( i === 5 )。 The loop will execute as long as that's true. 只要这是真的,循环就会执行。 But you start the loop off by assigning i = 1 , and 1 is not equal to 5, even for large values of 1. 但是你通过指定i = 1启动循环,并且1不等于5,即使对于大的值1也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord Bot 发送多条消息,但我只希望它发送一条 - Discord Bot sends multiple messages but I only want it to send one 我的 Discord 机器人多次响应一个命令,我想知道是否有办法只发送一次 - My Discord bot reponds multiple time to one command and I want to know if there's a way to send it only once Discord bot 发送消息特定通道错误 - Discord bot send messages specific channel error 尝试在 Discord 机器人中发送随机消息(javascript) - Trying to send a random messages in a Discord bot (javascript) Discord bot 无法发送带有斜杠命令的消息 - Discord bot cannot send messages with slash commands 一旦discord bot重新连接而不是一个,如何修复发送的多个消息? - How to fix multiple messages being send once a discord bot reconnects instead of one? Discord Bot无需命令即可发送消息 - Discord Bot send message without command 尝试为 Discord bot 设置多个命令文件 - Trying to have multiple command files for Discord bot 我希望机器人只发送随机消息,但一条消息只发送一次 - I want a bot to send random messages only but one message only once 使用 Discord 机器人回复一个命令的一系列替代响应的正确代码是什么,但它是随机顺序? - What's the right code to use to have a Discord bot reply with an array of alternative responses to one command, but it a random order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM