-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (44 loc) · 1.59 KB
/
main.py
File metadata and controls
60 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import pickle
from os import path
from time import time
import discord
from secrets import TOKEN
pickleName = 'voiceChannelTime.pickle'
if path.exists(pickleName):
with open(pickleName, 'rb') as handle:
voiceChannelLog = pickle.load(handle)
else:
voiceChannelLog = {}
voiceChannelActive = {}
client = discord.Client()
@client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
@client.event
async def on_voice_state_update(member, before, after):
user = member.name
if user not in voiceChannelLog:
voiceChannelLog[user] = 0
if before.channel is None and after.channel is not None:
print(user + " joined")
voiceChannelActive[user] = time()
if after.channel is None and before.channel is not None:
print(user + "left")
voiceChannelLog[user] += time() - voiceChannelActive[user]
print(voiceChannelLog)
with open(pickleName, 'wb') as handle:
pickle.dump(voiceChannelLog, handle, protocol=pickle.HIGHEST_PROTOCOL)
def printLeaderboard(voiceChannelLog):
sortLog = sorted(voiceChannelLog.items(), key=lambda x: x[1], reverse=True)
outStr = 'Voice Channel Leaders\n----------------------\n'
for i, user in enumerate(sortLog):
s = '{}. {} - {} mins. \n'.format(i + 1, user[0], round(user[1]/60))
outStr += s
return outStr
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$leaderboard'):
await message.channel.send(printLeaderboard(voiceChannelLog))
client.run(TOKEN)