【发布时间】:2021-08-24 02:48:16
【问题描述】:
我正在尝试制作一个可以编辑自己的消息的机器人,但我不知道如何保留发送的消息。
我想做这样的事情:
- 我触发命令
- 机器人回答,例如“是”
- 一秒钟后,“是”被替换为“否”
所以我想我需要将“是”消息保留在变量中以便能够对其进行编辑,我该怎么做?
【问题讨论】:
-
您需要向我们展示您迄今为止所做的尝试。 SO 不是编码服务。
标签: javascript node.js discord.js
我正在尝试制作一个可以编辑自己的消息的机器人,但我不知道如何保留发送的消息。
我想做这样的事情:
所以我想我需要将“是”消息保留在变量中以便能够对其进行编辑,我该怎么做?
【问题讨论】:
标签: javascript node.js discord.js
您可以通过链接.then() 来跟踪您的消息。例如:
message.channel.send("Hello from StackOverflow!").then((msg) => {
// Your logic ...
// ...
msg.edit("Hello!");
});
【讨论】:
当您的机器人 sends 发送消息(例如,message.channel.send('...'))时,该方法返回 Promise 或 Message。我不会进一步解释 Promises 在 JavaScript 中的工作原理,但请记住,在使用 discord.js 进行开发时,您会非常需要它们,它是现代 JavaScript 中的重要知识。
所以基本上,有两种方法可以访问您发送的消息。
.then():message.channel.send('...').then((msg) => {
/* The sent message is stored in the msg variable, do whatever you want with it
However, note that you cannot use it outside of this .then() block in any way */
});
async/await:const msg = await message.channel.send('...');
// Now, your sent message is stored in the msg variable
请记住,await 关键字只能在 async 函数中使用。因此,由于我们的大多数 discord.js 代码都在事件中,您可以像这样使您的事件监听器:
client.on('message', async function() { /* ... */ });
只需将async 关键字放入其中即可使用await 关键字。
再次强调,掌握 Promises 对 discord.js 和一般来说都非常重要。 除了我在解释中添加的链接之外,this article of the discord.js guide 可能会对您有很大帮助。
【讨论】: