Ask coding questions

← Back to all posts
DB Set userState to true doesn't work
Kuma4K (0)

I am writing a Discord bot and i want it to track the time a user is connected to a Channel.
So i take the time a user Connects to a Channel and disconnects a channel, calculate the diffrence und add this to the overall time the user spend in channels.
But if the user swaps channels without disconnecting on the new connection a new time would be saved, so i want to give a user a State and if that state is 0 a new time will be tracked, if its 1 no new time is saved.
i create and access the Key by using the User DiscodID and a String stored in the variable userState. WIth this i access the key and want to store the new value, everything works except the value stays null.

exports.userConnect = function userConnect(newVoiceState) {
	let user = newVoiceState.member.user.tag
	let channel =  newVoiceState.channel.name
	let time = Date.now()
	console.log(`${user} connected to ${channel}.`);
	console.log(time)
	var userState = user + "State" 

	db.get(userState).then(value => {		
		if(!value){
                        //this doens't work, value stays null
			db.set(userState, true) 
		}
		if(value == false){
			console.log("set to true")
			db.set(userState, true)
		}
		if(value == true){
			console.log("new connected")
		}		
		console.log(value) // null
	})
}