我正在尝试创建一个建议命令,如果用户在服务器中键入 =suggest
,它将在他们的 DM 中创建一个提示,要求不同的规范.
但是,我不确定如何获取用户输入并在嵌入中使用该输入.我该怎么做?
例如:机器人会在 DM 的 中询问你希望标题是什么?",然后用户会用他们想要的标题进行响应,并将该输入设置为标题.p>
你可以使用消息收集器.我使用
我刚刚注意到您想通过 DM 发送它.在这种情况下,您可以 .send()
第一条消息,等待它被解析,这样你就可以使用 channel.createMessageCollector()
.我添加了一些评论来解释它:
const prefix = '!';client.on('message', async (message) => {如果 (message.author.bot || !message.content.startsWith(prefix)) 返回;const args = message.content.slice(prefix.length).split(/+/);常量命令 = args.shift().toLowerCase();常量分钟 = 5;如果(命令==='建议'){常量问题 = [{答案:空,字段:'标题'},{答案:空,字段:'描述'},{答案:空,字段:'作者姓名'},{答案:空,字段:'颜色'},];让电流 = 0;//等待消息发送并抓取返回的消息//所以我们可以添加消息收集器常量发送 = 等待 message.author.send(`**${questions.length} 的第 1 题:**
您希望 ${questions[current].field} 是什么?`,);常量过滤器=(响应)=>response.author.id === message.author.id;//在发送原始问题的 DM 频道中发送常量收集器 = sent.channel.createMessageCollector(过滤器,{最大值:问题.长度,时间:分钟 * 60 * 1000,});//每次收集消息时触发收集器.on('收集', (消息) => {//添加答案并增加当前索引问题[当前++].answer = message.content;const hasMoreQuestions = 当前 <问题.长度;如果(有更多问题){消息.作者.发送(`**问题 ${current + 1} of ${questions.length}:**
你希望 ${questions[current].field} 是什么?`,);}});//当超时或达到限制时触发collector.on('end', (collected, reason) => {如果(原因 === '时间'){返回消息.作者.发送(`我并不是说你很慢,但你只在 ${MINUTES} 分钟内回答了 ${questions.length} 中的 ${collected.size} 个问题.我放弃了.`,);}常量嵌入 = 新 MessageEmbed().setTitle(问题[0].answer).setDescription(问题[1].answer).setAuthor(问题[2].answer).setColor(问题[3].answer);message.author.send('这是你的嵌入:');message.author.send(嵌入);//发送确认消息message.author.send('你想发表吗?`y[es]/n[o]`');message.author.dmChannel.awaitMessages(//设置一个过滤器只接受 y、yes、n 和 no(m) =>['y', 'yes', 'n', 'no'].includes(m.content.toLowerCase()),{最大值:1},).then((coll) => {让响应 = coll.first().content.toLowerCase();if (['y', 'yes'].includes(response)) {//发布嵌入,例如在频道中发送等//然后让成员知道你已经完成了message.author.send('太好了,嵌入已发布.');} 别的 {//没什么可做的,只是让他们知道发生了什么message.author.send('发布嵌入被取消.');}}).catch((coll) => console.log('handle error'));});}});
PS:您可以在 DiscordJS.guide一个>.
I'm attempting to make a suggestions command, where if the user types =suggest
in the server, it would create a prompt in their DM's, asking for different specifications.
However, I am unsure how to obtain user input and use that input in an embed. How would I do this?
For example: The bot would ask in DM's "What would you like the title to be?", and then the user would respond with their desired title, and it would set that input as the title.
You can use a message collector. I used channel.awaitMessages
here:
const prefix = '!';
client.on('message', (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'suggest') {
// send the question
message.channel.send('What would you like the title to be?');
// use a filter to only collect a response from the message author
const filter = (response) => response.author.id === message.author.id;
// only accept a maximum of one message, and error after one minute
const options = { max: 1, time: 60000, errors: ['time'] };
message.channel
.awaitMessages(filter, options)
.then((collected) => {
// the first collected message's content will be the title
const title = collected.first().content;
const embed = new MessageEmbed()
.setTitle(title)
.setDescription('This embed has a custom title');
message.channel.send(embed);
})
.catch((collected) =>
console.log(`After a minute, only collected ${collected.size} messages.`),
);
}
});
I've just noticed that you wanted to send it in a DM. In that case, you can .send()
the first message, wait for it to be resolved, so you can add a message collector in the channel using channel.createMessageCollector()
. I've added some comments to explain it:
const prefix = '!';
client.on('message', async (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const MINUTES = 5;
if (command === 'suggest') {
const questions = [
{ answer: null, field: 'title' },
{ answer: null, field: 'description' },
{ answer: null, field: 'author name' },
{ answer: null, field: 'colour' },
];
let current = 0;
// wait for the message to be sent and grab the returned message
// so we can add the message collector
const sent = await message.author.send(
`**Question 1 of ${questions.length}:**
What would you like the ${questions[current].field} be?`,
);
const filter = (response) => response.author.id === message.author.id;
// send in the DM channel where the original question was sent
const collector = sent.channel.createMessageCollector(filter, {
max: questions.length,
time: MINUTES * 60 * 1000,
});
// fires every time a message is collected
collector.on('collect', (message) => {
// add the answer and increase the current index
questions[current++].answer = message.content;
const hasMoreQuestions = current < questions.length;
if (hasMoreQuestions) {
message.author.send(
`**Question ${current + 1} of ${questions.length}:**
What would you like the ${questions[current].field} be?`,
);
}
});
// fires when either times out or when reached the limit
collector.on('end', (collected, reason) => {
if (reason === 'time') {
return message.author.send(
`I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
);
}
const embed = new MessageEmbed()
.setTitle(questions[0].answer)
.setDescription(questions[1].answer)
.setAuthor(questions[2].answer)
.setColor(questions[3].answer);
message.author.send('Here is your embed:');
message.author.send(embed);
// send a message for confirmation
message.author.send('Would you like it to be published? `y[es]/n[o]`');
message.author.dmChannel
.awaitMessages(
// set up a filter to only accept y, yes, n, and no
(m) => ['y', 'yes', 'n', 'no'].includes(m.content.toLowerCase()),
{ max: 1 },
)
.then((coll) => {
let response = coll.first().content.toLowerCase();
if (['y', 'yes'].includes(response)) {
// publish embed, like send in a channel, etc
// then let the member know that you've finished
message.author.send('Great, the embed is published.');
} else {
// nothing else to do really, just let them know what happened
message.author.send('Publishing the embed is cancelled.');
}
})
.catch((coll) => console.log('handle error'));
});
}
});
PS: You can learn more about message collectors on DiscordJS.guide.
这篇关于如何根据通过提示从用户输入收集的输入进行自定义嵌入 - Discord.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!