Как считать время проведенное в голосовом канале? | discord.py
У меня есть код, который реагирует на вход в голосовой, и выход из него. При входе пишется "1", при выходе "0". Как мне его изменить, чтобы он еще и считал время проведенное в голосовом канале.
@bot.event
async def on_voice_state_update(member, before, after):
if before.channel is None and after.channel is not None:
print('1')
elif before.channel is not None and after.channel is None:
print('0')
Ответы (2 шт):
Автор решения: Sergey
→ Ссылка
считал время проведенное в голосовом канале
А в чём пробела-то ? Засепкаем время при входе, аналогично - при выходе. И берём их разницу:
import time
if before.channel is None and after.channel is not None:
print('1')
t1 = time.time()
elif before.channel is not None and after.channel is None:
t2 = time.time()
print('0')
print(t2-t1)
Автор решения: VOLK
→ Ссылка
import time
@bot.event
async def on_ready():
print('Bot Connected')
global tdict
tdict = {}
await bot.change_presence(activity = discord.Game('r!help'))
@bot.event
async def on_voice_state_update(member, before, after):
author = member.id
if before.channel is None and after.channel is not None:
print('1')
t1 = time.time()
tdict[author] = t1
elif before.channel is not None and after.channel is None and author in tdict:
t2 = time.time()
print('0')
print(t2-tdict[author])