Skip to content

Commit

Permalink
Adding "Users Served" counter. Resolves #2
Browse files Browse the repository at this point in the history
  • Loading branch information
FailedSave committed Feb 27, 2019
1 parent 4dacc1c commit 37161e1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bot.py
Expand Up @@ -3,6 +3,8 @@
import fate
from setting_storage import settings_from_user_id, target_settings_from_user_id
import setting_storage
import stats_storage
import stats
import settings
import json
import help
Expand All @@ -17,6 +19,7 @@ async def on_ready():
print(client.user.id)
print('------')
setting_storage.load_settings()
stats_storage.load_stats()

@client.event
async def on_message(message):
Expand All @@ -43,6 +46,9 @@ async def on_message(message):
if message.content.startswith('!tviewsettings'):
await settings.handle_view_settings(client, message, message.author.name, message.channel, target_settings_from_user_id(message.author))

if message.content.startswith('!stats'):
await stats.handle_view_stats(client, message, message.author.name, message.channel)

if message.content.startswith('!save'):
await setting_storage.save_settings()

Expand All @@ -54,6 +60,7 @@ async def autosave_task():
while not client.is_closed:
await asyncio.sleep(300)
await setting_storage.save_settings()
await stats_storage.save_stats()

with open('.connections.json') as json_data:
connections = json.load(json_data)
Expand Down
5 changes: 5 additions & 0 deletions fate.py
Expand Up @@ -4,6 +4,7 @@
import asyncio
import random
import json
import stats_storage

delay = 0.9

Expand All @@ -14,6 +15,7 @@ async def handle_fate(client, message, name, channel, settings):
for string in get_fate_strings(settings):
await client.send_message(channel, string)
await asyncio.sleep(delay)
stats_storage.increment_fate()

async def handle_target_fate(client, message, name, channel, settings):
words = message.content.split(None, 1)
Expand All @@ -36,6 +38,8 @@ async def handle_target_fate(client, message, name, channel, settings):
for string in get_fate_strings(settings):
await client.send_message(channel, string)
await asyncio.sleep(delay)
stats_storage.increment_fate()


async def handle_whisper_fate(client, message, name, channel, settings):
words = message.content.split(None, 1)
Expand Down Expand Up @@ -64,6 +68,7 @@ async def handle_whisper_fate(client, message, name, channel, settings):
target_fate_string = target_fate_string + string + "\n"

await client.send_message(message.author, target_fate_string)
stats_storage.increment_fate()

def get_fate_strings(settings: Settings) -> list:
strings = []
Expand Down
6 changes: 6 additions & 0 deletions setting_storage.py
Expand Up @@ -25,6 +25,12 @@ def find_settings_from_name(name) -> Settings:
return settings
return None

def get_users_count():
return len(settings_dict.keys())

def get_helpless_users_count():
return len([item for item in settings_dict.values() if item.helpless])

async def save_settings():
output = open('settings.pkl', 'wb')

Expand Down
10 changes: 10 additions & 0 deletions stats.py
@@ -0,0 +1,10 @@
import stats_storage
import setting_storage

async def handle_view_stats (client, message, name, channel):
stats_string = f"""
**Fates Served:** {stats_storage.stats["fates_delivered"]}
**Users:** {setting_storage.get_users_count()}
**Helpless Users:** {setting_storage.get_helpless_users_count()}
"""
await client.send_message(channel, stats_string)
23 changes: 23 additions & 0 deletions stats_storage.py
@@ -0,0 +1,23 @@
import pickle
import os

stats = {}

def increment_fate():
stats["fates_delivered"] += 1

async def save_stats():
output = open('stats.pkl', 'wb')

pickle.dump(stats, output)
output.close()

def load_stats():
global stats
if (os.path.isfile('stats.pkl')):
input = open('stats.pkl', 'rb')
stats = pickle.load(input)
input.close()
else:
stats = {}
stats["fates_delivered"] = 0

0 comments on commit 37161e1

Please sign in to comment.