【问题标题】:Send several waiting messages after a waiting message [closed]在等待消息后发送几条等待消息[关闭]
【发布时间】:2019-05-11 07:25:10
【问题描述】:

我正在寻求您的帮助。 我正在尝试发送一条等待回复的消息,然后当用户在时限内回答时,它会发送另一条类似这样的消息。 我不知道你是否理解我。 如果您需要更多信息,请不要回答。

真诚的。

我当前的代码:

const filter = m => m.author.id === message.author.id && m.author.id !== bot.user.id

// CHOISIR LE NOM

            message.reply("S'il vous plaît choisissez un nom pour le tournoi.").then(r => r.delete(10000));
            message.channel.awaitMessages(filter, {
              max: 1,
              time: 10000
            }).then(collected => { 
              if(collected.first().content === "stop" || collected.first().content === "cancel"){
                return message.reply("Création du tournoi annulé.")
              }
              let tournamentname = collected.first().content;
              db.collection("tournois").findOneAndUpdate({"tournamentInformations.status": "active"}, {
                $set: {
                  "tournamentInformations.tournamentName": tournamentname
                  }}, {upsert: true}, function(err,doc) { if (err) { throw err; } });
              message.channel.send(":white_check_mark: | Vous avez défini le nom du tournoi à "+tournamentname);
                }).catch(err => {
                  console.log(err)
                })

// CHOISIR L'ORGANISATEUR

            message.reply("S'il vous plaît choisissez le nom de l'organisateur.").then(r => r.delete(10000));
            message.channel.awaitMessages(filter, {
              max: 1,
              time: 10000
            }).then(collected => { 
              if(collected.first().content === "stop" || collected.first().content === "cancel"){
                return message.reply("Création du tournoi annulé.")
              }
              let organisateur = collected.first().content;
              db.collection("tournois").findOneAndUpdate({"tournamentInformations.status": "active"}, {
                $set: {
                  "tournamentInformations.organizedBy": organisateur
                  }}, {upsert: true}, function(err,doc) { if (err) { throw err; } });
              message.channel.send(":white_check_mark: | Vous avez défini l'organisateur à "+organisateur);
                }).catch(err => {
                  console.log(err)
                })

【问题讨论】:

标签: javascript node.js discord.js


【解决方案1】:

您似乎需要 .awaitMessages.then 排序 - 我强烈建议您在发布之前至少 30 分钟在谷歌上搜索您的问题,并事先积极搜索 official Discord.js documentation

我可以提供一个简单的例子:

message.channel.send('First question').then(async (first) => {
            message.channel.awaitMessages(filter, { maxMatches: 1, time: 60000, errors: ['time']}).then(async (collected) => {
                await first.delete()
                await collected.first().delete()
            }).then(async () => {
                message.channel.send('Question 2').then(async (second) => {
                    message.channel.awaitMessages(filter, { maxMatches: 1, time: 60000, errors: ['time']}).then(async (collected) => {
                        await second.delete()
                        await collected.first().delete()
}).catch(collected => {
message.channel.send('Your answer timed out')
})

}).catch(collected => {
message.channel.send('Your answer timed out')
})

这是做什么的(草率地,当我复制并粘贴这个 sn-p 并从我自己的代码中对其进行编辑时,它可能缺少标记)是发送第一条消息 'First question' 并允许 60 秒的响应时间范围。如果有答案,它会删除问题提示以及答案(您可以根据需要存储答案),然后继续对第二个问题执行相同的操作,依此类推。如果 60 秒的时间范围到期,它将结束序列并告诉用户时间已到。

希望这有帮助,如果有帮助,请将其标记为正确。

(P.S.如果你熟悉函数,可以把所有这些都塞进一个来压缩和简化你的代码,我只是给你提供了这样的例子,以免混淆你)

【讨论】:

  • 谢谢!之后如果你想给我它就完美了,我不会拒绝的。 :p
  • @Niromash 如果您不熟悉其用法,这对您的代码和学习会有些不利,因为您会遇到更多问题。但是现在你知道了,如果你花时间学习函数等,它是可以压缩的。
  • 我已经学了一点,我知道其中一些,当然很简单,但我想看看它是如何工作的......
  • 本质上是一样的,只是不用一遍又一遍地输入相同的代码。
  • 谢谢,这就是函数 x) 的目的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 2021-10-13
  • 2013-12-07
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多