I've written some code down below. My main experience coding is with Python, but I got hooked on a discord.js tutorial and decided to give discord bot coding a try (I know that there's a Python option, but I've already invested a lot of time into this, so unless if it's not feasible, then I will most likely not switch over). I'm still taking a while to get the concept of async/await, hopefully I've applied it correctly in the code. It currently works just asking one question, but I can't seem to continue the operation, even though the counter seems to add fine. I was thinking of a recursive solution, but whenever I do a while
loop or a recursion function it still starts on the first question again either way. My main concerns would be whether or not I'm going in a path that can get me to ask multiple questions.
tl;dr: The code I've written down below allows the bot to record one response, but when I try to make it repeat using a continuous recursive function, the code just starts back at question 1.
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
client.login(token);
const quiz = require('./q.json');
var num = 0;
var item = quiz[num];
var messg = "";
//var line = []; ignore this, this is for recording the responses onto a different application later
client.on('message', async message => {
if (message.author.bot) return;
if (message.content === '!test') {
collec(message);
}
})
async function collec(message) {
message.reply(item.question);
//checks to see if the author that used commmand is the same + if it's an option
//isPossibleOption() = just constantly gets rid of one element until it fits w/ criteria
const filter = m => m.author.id === message.author.id && isPossibleOption(m.content, item.answers);
var collector = new Discord.MessageCollector(message.channel, filter, { max: 1 });
collector.on("collect", async msg => {
messg = msg.content;
//await recordAnswer(messg, num); record it onto google sheets / check for q
message.channel.send('Next question!');
nextQ();
})
collector.on("end", collected => {});
}
async function isPossibleOption (message, file) {
var i;
for (i = 0; i < file.length; i++) {
if (message.toLowerCase() == file[i]) {
return true;
}
}
return false;
}
function nextQ () {
num++;
item = quiz[num];
}
question from:
https://stackoverflow.com/questions/65877569/discord-js-is-there-a-method-for-discord-bots-to-recursively-ask-questions-li 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…