問題描述
我正在嘗試創(chuàng)建一個建議命令,如果用戶在服務器中鍵入 =suggest
,它將在他們的 DM 中創(chuàng)建一個提示,要求不同的規(guī)范.
但是,我不確定如何獲取用戶輸入并在嵌入中使用該輸入.我該怎么做?
例如:機器人會在 DM 的 中詢問你希望標題是什么?",然后用戶會用他們想要的標題進行響應,并將該輸入設置為標題.p>
你可以使用消息收集器.我使用
我剛剛注意到您想通過 DM 發(fā)送它.在這種情況下,您可以 .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;//等待消息發(fā)送并抓取返回的消息//所以我們可以添加消息收集器常量發(fā)送 = 等待 message.author.send(`**${questions.length} 的第 1 題:**
您希望 ${questions[current].field} 是什么?`,);常量過濾器=(響應)=>response.author.id === message.author.id;//在發(fā)送原始問題的 DM 頻道中發(fā)送常量收集器 = sent.channel.createMessageCollector(過濾器,{最大值:問題.長度,時間:分鐘 * 60 * 1000,});//每次收集消息時觸發(fā)收集器.on('收集', (消息) => {//添加答案并增加當前索引問題[當前++].answer = message.content;const hasMoreQuestions = 當前 <問題.長度;如果(有更多問題){消息.作者.發(fā)送(`**問題 ${current + 1} of ${questions.length}:**
你希望 ${questions[current].field} 是什么?`,);}});//當超時或達到限制時觸發(fā)collector.on('end', (collected, reason) => {如果(原因 === '時間'){返回消息.作者.發(fā)送(`我并不是說你很慢,但你只在 ${MINUTES} 分鐘內(nèi)回答了 ${questions.length} 中的 ${collected.size} 個問題.我放棄了.`,);}常量嵌入 = 新 MessageEmbed().setTitle(問題[0].answer).setDescription(問題[1].answer).setAuthor(問題[2].answer).setColor(問題[3].answer);message.author.send('這是你的嵌入:');message.author.send(嵌入);//發(fā)送確認消息message.author.send('你想發(fā)表嗎?`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)) {//發(fā)布嵌入,例如在頻道中發(fā)送等//然后讓成員知道你已經(jīng)完成了message.author.send('太好了,嵌入已發(fā)布.');} 別的 {//沒什么可做的,只是讓他們知道發(fā)生了什么message.author.send('發(fā)布嵌入被取消.');}}).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.
這篇關(guān)于如何根據(jù)通過提示從用戶輸入收集的輸入進行自定義嵌入 - Discord.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!