【问题标题】:My messages repeat many times in node.js/discord.js我的消息在 node.js/discord.js 中重复了很多次
【发布时间】:2020-05-12 01:45:01
【问题描述】:

我正在制作一个 ONUW(One Night Ultimate Werewolf)Discord 机器人,可以让你玩游戏。

对于游戏,我需要一个 2 分半钟的计时器。我试着做一个。它运行命令,然后倒计时,但它至少重复消息 8 次。我希望它会说“你的时间已经开始了!跳到 ONUW 讨论语音频道,告诉每个人你的角色,或者试着欺骗别人。记住,有些人可能会撒谎,所以你不知道他们到底是谁!”一次,然后在 2.5 分钟后,它会说“时间到了!投票给你想杀死的人!”,再一次,只有一次。

这是完整的代码文件。

//required libraries
const Discord = require('discord.js');
const client = new Discord.Client();

//What to do when the bot starts up.
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  client.user.setActivity("with my ding-a-ling")
});

//prefix
var prefix = ("~")

//checks api latency and latency
client.on('message', message => {
  if (message.author.bot) return;

  if (message.content === prefix + 'ping') {
    message.channel.send(":ping_pong: Pinging...").then((ping) => { ping.edit(`:ping_pong: Pong!\nLatency is ${Math.floor(ping.createdAt - message.createdAt)}\nAPI latency is ${Math.round(client.ping)}ms`); });
  }
}
);

//starts a new game and broadcasts that
client.on('message', message => {
  if (message.author.bot) return;

  if (message.content === prefix + 'newgame') {
    if (message.member.roles.has("641725420957335562")) {
      client.channels.get('642070729805791232').send("Sending out message....").then((newgame) => { newgame.edit(`Hey, <@&641727807663374345>! A new game is starting. Do ~ready to apply`) })
    }

    client.on('message', message => {
      if (message.author.bot) return;

      if (message.content !== prefix + 'ready') {
        return;
      }

      if (message.content === prefix + 'ready') 
      {message.channel.send(`<@${message.author.id}> is ready!`)
        var roles = Array("Doppelganger", "Werewolf", "Minion", "Mason", "Seer", "Robber", "Troublemaker", "Drunk", "Insomniac", "Villager", "Tanner", "Hunter", "Bartender", "Wolf", "Egotist")
        var role = roles[Math.floor(Math.random() * roles.length)];message.author.send("Your role is..." + role + "!")
      }


      client.on('message', message => {
        if (message.content === prefix + 'begin') {
          function begin() {
            // stuff you want to happen right away
            message.channel.send('The game will begin in...')
            }
          function beginCount() {
            // all the stuff you want to happen after that pause
            message.channel.send('3')
              }
          function beginCount2() {
            message.channel.send('2')
          }
          function beginCount3() {
            message.channel.send('1')
          }
          function nightFall() {
            message.channel.send('Night has fallen!')
          }
          function countdownRun() {
            message.channel.send('~countdown').then(message => { messsage.delete(1000)})
          }
          // call the first chunk of code right away
          begin();
          setTimeout(beginCount, 2000);
          setTimeout(beginCount2, 2000);
          setTimeout(beginCount3, 2000);
          setTimeout(nightFall, 2000);
          setTimeout(countdownRun, 2000);
      };

      client.on('message', message => {
        if (message.content === prefix + 'countdown') {
          function function1() {
            // stuff you want to happen right away
            message.channel.send('Your time has begun! Hop over to ONUW Discussion Voice Channel and tell everyone your role, or try and trick people. Remember, some people may lie so you don\'t know who they really are!')
            }
            function function2() {
              // all the stuff you want to happen after that pause
              message.channel.send('Times up! Vote for who you want to kill!')
              }
              // call the first chunk of code right away
              function1();
              // call the rest of the code and have it execute after 3 seconds
              setTimeout(function2, 150000);
              }
        })
      });
    })}
  });


//logs messages into console
client.on("message", async message => {
  console.log(`${message.author.username} said: ${message.content}`);
});

//Test command if we need it
client.on('message', message => {
  if (message.author.bot) return;

  if (message.content === prefix + 'test') {
    message.channel.send("test reply")
  }
});



client.login(process.env.TOKEN);

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 这段代码有重复。在client.on('message', message =&gt; { 之后使用switch(message) { 语句以使代码更具可读性是一个想法吗?

标签: node.js discord discord.js


【解决方案1】:

您重复bot.on ('message') 块太多次,同时它们相互嵌套。 尝试使用命令处理程序方法。

一些指南如何创建它:Guide 1Guide 2

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多