Discord.js редактировать перезапустить сообщение - PullRequest
0 голосов
/ 07 июля 2019

Есть ли способ, как я могу отредактировать свое сообщение после перезапуска бота, я хочу, чтобы он отправил сообщение, перезагружаясь сейчас, и после перезапуска он должен отредактировать сообщение до Done: 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));
}

1 Ответ

0 голосов
/ 08 июля 2019

Message.reply() возвращает Обещание , разрешается с другим сообщением . Чтобы использовать отправленное сообщение, вам нужно получить доступ к возвращенному значению. Обратите внимание на scope из restartMsg в приведенных ниже примерах.

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);
}
...