0
votes

I am a total noob regarding discord bots, but I am trying my best to learn and just try to do the best I can. I am trying to program a bot which lets you take a test in a channel. If you solve 4/5 questions you gain a special role. It seems to work fine if one user is doing it, but as soon as 2 or more users try to take a quiz at the same time, the answers of the questions will be wrong and the amount of questions send into the channel may also vary and I can not figure out why. As far as I understand the message variable gets overwritten as soon as a message will input. But as far as I am concerned javascript runs on a single thread so I do not understand why this is happening. If anyone would be able to help me and/or has some constructive critism i would gladly appreciate it. :)

const meineid = '202469056668762113'
const quiz = require('../quiz.json')
const standartEmbed = new Discord.MessageEmbed()
    .setColor('#0099ff')

module.exports = {
    name: 't',
    description: 't',
    execute(message, client) {

        let x = message.member.roles.cache.find(role => role.name === 'Regeln')

        if (x===undefined)  {
                assign(message, client)}
        else {message.channel.send("bruder was los mit dir")}
    }
}

async function assign (message, client) {
    if (message.guild.channels.cache.find(c => c.name === message.author.id)) {
        message.reply("Channel existiert bereits")
    }

    else {
        await message.guild.channels.create(message.author.id, {
            type: 'text',
            permissionOverwrites: [
            {
                id: message.guild.id,
                deny: ['VIEW_CHANNEL'],
            },
            {
                id: meineid, 
                allow: ['VIEW_CHANNEL'],
            },
            {
                id: client.user.id, 
                allow: ['VIEW_CHANNEL'],
            },
            {
                id: message.author.id, 
                deny: ['SEND_MESSAGES'],
                allow: ['VIEW_CHANNEL'],
            },
            ],
        })
        message.guild.channels.cache.find(c => c.name === message.author.id).send("<@" + message.author.id + ">")
        test(message)

        standartEmbed.fields = []
        message.guild.channels.cache.find(c => c.name === 'bot-log').send(standartEmbed
            .addFields(
                { name: "Created channel: ", value: message.author.id },
            )
            .setTimestamp())             
    }
}      



async function test(message) {

    let y=0

    for (z=0; z < 5; z++) {   

        let i = Math.floor(Math.random() * quiz.length)
        item = quiz[i]
        quiz.splice(i, 1)
        const reactions = ['????','????','????']
        const quizEmbed = new Discord.MessageEmbed()
            .setColor('#0099ff')
        const filter = (reaction, user) => {
            return reactions.includes(reaction.emoji.name) && user.id === message.author.id
    }

    await message.guild.channels.cache.find(c => c.name === message.author.id.toString()).send(quizEmbed
        .setTitle(item.question)
        .addFields(
            { name: reactions[0], value: item.options[0] },
            { name: reactions[1], value: item.options[1] },
            { name: reactions[2], value: item.options[2] },
        )
        .setTimestamp()
    )
    .then(async function(message) {
        reactions.forEach(r => message.react(r))

        await message.awaitReactions(filter, {max:1, time: 11110000})
            .then(function(collected) {
                if (collected.first().emoji.name == item.answer) {
                    message.channel.send("Richtig! :)")
                    y++
                    return y
                } else message.channel.send("Falsch! :(")
                    return y
            }).catch(collected => {
                z=5
        })
        return y
    })

}
}```
1

1 Answers

0
votes

where are you closing this for statement?

    for (z=0; z < 5; z++) {