首页 > 解决方案 > 如何进行倒计时编辑而不是一一发送消息?

问题描述

我正在做一个倒计时命令,到目前为止,这是我的代码:

let count = args[1]

const counter = setInterval(() => {
  if (count > 0) {
    message.channel.send(count)
    count--
  } else {
    clearInterval(counter)
  }
}, 1000)

现在,计时器发送的数字如下:

1
2
3
4
5

我只希望它在一条消息中,然后在不和谐中进行编辑。

以下是在 Discord.JS 中编辑的方法(示例):

   message.channel.send('hi').then(m => {

     m.edit('edited!')
    })

知道怎么做吗?

标签: javascriptnode.jsdiscorddiscord.jsbots

解决方案


这应该做

let count = args[1]
let CountDownMessage;
const counter = setInterval(async () => {
  if (count == args[1]) { //For the first time only
    await message.channel.send(count).then( msg => { CountDownMessage = msg }
    count -- ;
  }else if (count > 0) {
    CountDownMessage.edit(count);
    count -- ;
  } else {
    CountDownMessage.edit("Time's up");
    clearInterval(counter);
  }
}, 1000);

现在您不必每次都创建一个函数


推荐阅读