Discord.js 编辑重启消息
Posted
技术标签:
【中文标题】Discord.js 编辑重启消息【英文标题】:Discord.js edit restart message 【发布时间】: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));
【问题讨论】:
【参考方案1】:Message.reply()
返回一个Promise,用另一个Message 解析。要使用发送的消息,您必须访问返回的值。请注意以下示例中的 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);
【讨论】:
以上是关于Discord.js 编辑重启消息的主要内容,如果未能解决你的问题,请参考以下文章
如何根据 Discord.js 中的反应编辑消息(创建列表和切换页面)