Allows a bot to ask questions and a user to input responses via a series of Q&As. Start the command by typing `q&a` then answer the questions. When all answers are given it runs a final success action.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const event = context.params.event
const { channel_id, content, author } = event
const key = `q&a_${author.id}`
// ** DEV ** Overwrite with your questions
const command = "q&a"
const questions = [
"What's your name?",
"How old are you?",
"What's your favourite color?",
]
// ** DEV ** Overwrite your success action
// Answers are provided in the same order as the question list
async function onSuccess(answers) {
return lib.discord.channels['@0.2.0'].messages.create({
channel_id, content: ``,
embed: {
title: 'Questions & Answers',
fields: answers.map((_, i) => ({
name: `Q: ${questions[i]}`,
value: `A: ${answers[i]}`,
})),
}
});
}
let { answering, answers } = await lib.utils.kv['@0.1.16'].get({ key }) ?? {
answering: false,
answers: [],
};
// Are we starting a new q&a or answering questions? If not ignore this message.
if (!(content.trim() === command || answering)) return
// Add new answer
if (answering) answers = [...answers, content]
// Ask next question or finish
const question = questions[answers.length]
if (question) {
await lib.utils.kv['@0.1.16'].set({ key, value: { answering: true, answers } });
await lib.discord.channels['@0.2.0'].messages.create({
channel_id, content: question,
});
}
else {
await lib.utils.kv['@0.1.16'].clear({ key });
await onSuccess(answers)
}