【问题标题】:Discord.js edit restart messageDiscord.js 编辑重启消息
【发布时间】:2019-11-17 10:19:17
【问题描述】:

无论如何我可以在机器人重新启动后如何编辑我的消息,我希望他发送一条消息,现在重新启动,重新启动后它应该将消息编辑为完成:white_checkmark:

console.log(message.author.tag + ' restarted The bot')
message.reply('You restarted the bot, wait a few seconds') // <-- this message should be edited
bot.channels.get("593824605144088586").send(message.author.tag + ' restarted the bot')
bot.channels.get("593824605144088586").send('---------------------------------------------------')
setTimeout(function () { resetBot() }, 5000);


function resetBot() {
    restarted = true;
    bot.channels.get("593824605144088586").send('Restarting...')
        .then(msg => bot.destroy())
        .then(() => bot.login(auth.token));
}

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    Message.reply() 返回一个Promise,用另一个Message 解析。要使用发送的消息,您必须访问返回的值。请注意以下示例中的 scoperestartMsg

    console.log(`${message.author.tag} restarted the bot.`);
    
    message.reply('You restarted the bot, please wait.')
      .then(restartMsg => {
        const logsChannel = bot.channels.get('593824605144088586');
        if (!logsChannel) return console.error('Logs channel missing.');
    
        logsChannel.send(`${message.author.tag} restarted the bot.\n---`)
          .then(() => {
            setTimeout(() => {
              logsChannel.send('Restarting...')
                .then(() => bot.destroy())
                .then(() => bot.login(auth.token))
                .then(() => restartMsg.edit('Restart successful.'));
            }, 5000);
          });
      })
      .catch(console.error);
    

    Async/await 等价物:

    // Asynchronous context (meaning within an async function) needed to use 'await.'
    
    try {
      console.log(`${message.author.tag} restarted the bot.`);
    
      const restartMsg = await message.reply('You restarted the bot, please wait.');
    
      const logsChannel = bot.channels.get('593824605144088586');
      if (!logsChannel) return console.error('Logs channel missing.');
    
      await logsChannel.send(`${message.author.tag} restarted the bot.\n---`);
    
      setTimeout(async () => {
        await logsChannel.send('Restarting...');
    
        await bot.destroy();
        await bot.login(auth.token);
    
        await restartMsg.edit('Restart successful.');
      }, 5000);
    } catch(err) {
      console.error(err);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-25
      • 2020-09-08
      • 1970-01-01
      • 2021-10-30
      • 2017-12-30
      • 2021-07-02
      • 1970-01-01
      • 2021-11-09
      相关资源
      最近更新 更多