Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I can't stop interval declared above the if statement.

const Discord = require('discord.js')
const mysql = require('mysql2')

        const con = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '',
            database: 'nodemysql'
        })

module.exports = client =>{ 

    client.on('voiceStateUpdate', (oldVoiceState, newVoiceState) => {

        const user = newVoiceState

        var intCoin = setInterval(() =>{
            con.query(`UPDATE coins SET coins = coins + 5 WHERE userid = '${user.id}'`)
            console.log('added coins')
        }, 3000)

        function stopcoins(){
            clearInterval(intCoin)
        }

    if(newVoiceState.channel){
        console.log('joined vc')
        if(!newVoiceState.selfMute){
            console.log('notmuted')
            con.query(`SELECT * FROM coins WHERE userid = '${user.id}'`, (err, rows) =>{
                if(err) {
                    console.log(err)
                }
                if(rows.length <1){
                    con.query(`INSERT INTO coins (userid, coins) VALUES ('${user.id}', 0)`)
                    console.log(`added member: ${user.member.user.username}`)  
                } else {
                    intCoin
                }
            })
        }
    } else {
        stopcoins()
        console.log('stop')
    }
    })
}

I tried everything I found. Functions, clearing interval and also changing var to null (in funtion I declared that var is changing into con.query etc.)

EDIT.1 It's my output in console after joining the VC and after some seconds leaving it

joined vc
notmuted
added coins
added coins
added coins
added coins
stop
added coins
added coins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...