-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_metrics.py
57 lines (41 loc) · 1.3 KB
/
discord_metrics.py
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
import discord
import time
import io
import json
import os
import sys
client = discord.Client()
def setup():
client.loop.create_task(get_users())
client.run('')
async def get_users():
online = 0
idle = 0
offline = 0
await client.wait_until_ready()
startTime = time.time()
while not client.is_closed:
for server in client.servers:
for member in server.members:
if str(member.status) == 'online':
online += 1
elif str(member.status) == 'idle':
idle += 1
await client.close()
totalUsers = online + idle
updateJson('app-cache/discordusers.json', totalUsers)
def returnCount(total):
print('Total is {}'.format(str(total)))
updateJson('app-cache/discordusers.json', total)
return str(total)
def updateJson(path, new_id):
newPath = ensureAbsPath(path)
with open(newPath, 'r') as f:
data = json.load(f) # Load json data into the buffer
tmp = data['users']
data['users'] = new_id
with open(newPath, 'w+') as f:
f.write(json.dumps(data)) # Write the new user count to the cache
def ensureAbsPath(path):
botRootDir = os.path.dirname(os.path.abspath(sys.argv[0])) + '/'
return path if os.path.isabs(path) else botRootDir + path