[英]How to make a DM prompt in Discord.js?
I've just learned how to send a DM from a Discord.js bot, but how can I turn it into a prompt?我刚刚学会了如何从 Discord.js bot 发送 DM,但如何将其转换为提示? My code looks like this at the moment:我的代码目前看起来像这样:
client.on('message', message => {
if (message.channel.type == "dm") {
if (message.author.bot) return;
if (message.content == "hello") {
message.author.send("Hello!");
}
}
});
But how can I store data about what's happening in that dm?但是我如何存储有关该 dm 中发生的事情的数据? If I was making a bot like Application Bot, it would choose what to reply with based on previous dms, how can I make that work?如果我正在制作一个像 Application Bot 这样的机器人,它会根据以前的 dms 选择要回复的内容,我该如何使其工作? Would I have to use a database?我必须使用数据库吗? Thanks!谢谢!
You can do this using a message collector, either with awaitMessages
or createMessageCollector
:您可以使用消息收集器执行此操作,使用awaitMessages
或createMessageCollector
:
message.author.send("Hello!");
// With awaitMessages:
message.dmChannel.awaitMessages(
// This is a filter function that allows you to only receive certain messages.
// Return true to receive the message.
// This filter will accept all messages.
msg => true,
// Options. This example makes the promise resolve after 1 message has been
collected.
{max: 1}
).then(messages => {
// messages is a Collection of messages
const msg = messages.first();
// do something...
});
// With createMessageCollector:
const collector = message.dmChannel.createMessageCollector(msg => true, {max: 1})
collector.on("collect", msg => {
// do something...
});
You can also use ES2017's async
/ await
syntax with awaitMessages
:您还可以使用ES2017的async
/ await
语法与awaitMessages
:
// Note the async here
client.on('message', async message => {
if (message.channel.type == "dm") {
if (message.author.bot) return;
if (message.content == "hello") {
message.author.send("Hello!");
const messages = await message.dmChannel.awaitMessages(msg => true, {max: 1});
const msg = messages.first();
// do something...
}
}
});
For more information see 'Collectors' on the discord.js guide .有关更多信息,请参阅discord.js 指南中的“收集器” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.