I've been coding a discord bot for a while now and I wanted to make a voice activity tracking.
The problem is, there's almost no documentation about that on the net. I used this as base but I changed a lot of code in it to make it "per server". I have at least 2 issues.
The 1st one is because of datetime, for exemple, if a user join a voice channel at 11:45 and leave it at 00:45, the result will be
"guild_id": {
"user_id": "-1 day, ..."
}
because the operation is 11 - (~25).
My seconde issue is also because of datetime,
if the time while a user as been in a vocal is more than 24hours, i'll get this kind of error:
Here's my code :
@commands.Cog.listener()
async def on_voice_state_update(self, member, before, after):
with open('data/voice_leaderboard.json', 'r') as file:
voice_data = json.load(file)
new_user = str(member.id)
guild_id = str(member.guild.id)
# Update existing user
if new_user in voice_data[guild_id]:
voice_leave_time = datetime.datetime.now().time().strftime('%H:%M:%S')
voice_join_time = voice_data[guild_id][new_user]
calculate_time = (datetime.datetime.strptime(voice_leave_time, '%H:%M:%S') - datetime.datetime.strptime(voice_join_time, '%H:%M:%S'))
voice_data[guild_id][new_user] = str(calculate_time)
with open('data/voice_leaderboard.json', 'w') as update_user_data:
json.dump(voice_data, update_user_data, indent=4)
# Add new user
else:
if member.bot:
return
else:
new_voice_join_time = datetime.datetime.utcnow().strftime('%H:%M:%S')
voice_data[guild_id][new_user] = new_voice_join_time
with open('data/voice_leaderboard.json', 'w') as new_user_data:
json.dump(voice_data, new_user_data, indent=4)
and here's a part of the json file :
{
"749948728248631297": {
"437265873401544705": "0:06:49"
},
I don't have any idea of how I could fix those 2 issues exept maybe by using discord.js but that's not what I want so if anyone have any idea of how I can do, please help me