Skip to content

Commit 2491f94

Browse files
Fetch points from json; if absent, use hardcoded values
1 parent 25b9777 commit 2491f94

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

bot.py

+46-24
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from distutils.command.check import check
1+
from json import load as json_load
22
from os import environ, getenv, listdir
33
from os.path import dirname
4-
from requests import get as requests_get
54

65
import discord
76
from discord.ext import commands
87
from dotenv import load_dotenv
98
from firebase_admin import credentials, db, initialize_app
9+
from requests import get as requests_get
1010

1111
# Read environment variables set at ./.env
1212
if not load_dotenv(f"{dirname(__file__)}/.env"):
@@ -286,6 +286,46 @@ async def delete_data(ctx, name):
286286
print(e.__traceback__)
287287

288288

289+
def fetch_points_for_each_task() -> dict[str, int]:
290+
"""
291+
Reads file at `points.json` to understand how many points to give for each task. If file is absent or error comes up, uses hardcoded values
292+
"""
293+
try:
294+
with open(f"{dirname(__file__)}/points.json") as f:
295+
points_dict: dict[str, int] = json_load(f)
296+
if not isinstance(points_dict, dict) or not points_dict:
297+
raise ValueError
298+
299+
except (FileNotFoundError, ValueError) as e:
300+
print(
301+
f"Could not read points.json in current directory so using hardcoded points. Error - {e}"
302+
)
303+
points_dict = {
304+
"pull request": 20,
305+
"info": 40,
306+
"blog": 60,
307+
"sm posting": 7,
308+
"weekly work": 5,
309+
"idea": 3,
310+
"brochure": 10,
311+
"news": 40,
312+
"demos": 20,
313+
"oc volunteer": 30,
314+
"oc assigned": 20,
315+
"oc no work": 10,
316+
"oc manager": 50,
317+
"wtf": 75,
318+
"discord": 10,
319+
"marketing": 20,
320+
"mini project": 100,
321+
"complete project": 200,
322+
"promotion medium": 25,
323+
"promotion large": 50,
324+
}
325+
326+
return points_dict
327+
328+
289329
@bot.command()
290330
@commands.has_any_role("Leaderboard", "Cabinet Member")
291331
async def contribution(ctx, name, task):
@@ -297,28 +337,6 @@ async def contribution(ctx, name, task):
297337
if value["Name"].casefold() == name.casefold():
298338
selector = leaderboard_ref.child(key)
299339
ref = selector.get()
300-
points_dict = {
301-
"pull request": 20,
302-
"info": 40,
303-
"blog": 60,
304-
"sm posting": 7,
305-
"weekly work": 5,
306-
"idea": 3,
307-
"brochure": 10,
308-
"news": 40,
309-
"demos": 20,
310-
"oc volunteer": 30,
311-
"oc assigned": 20,
312-
"oc no work": 10,
313-
"oc manager": 50,
314-
"wtf": 75,
315-
"discord": 10,
316-
"marketing": 20,
317-
"mini project": 100,
318-
"complete project": 200,
319-
"promotion medium": 25,
320-
"promotion large": 50,
321-
}
322340

323341
rating = ref["Rating"] + points_dict[task.casefold()]
324342
contributions = ref["Contributions"] + 1
@@ -430,4 +448,8 @@ def fetch_spreadsheet(speadsheet_id: str):
430448
...
431449

432450

451+
# Fetch points for each task
452+
points_dict: dict[str, int] = fetch_points_for_each_task()
453+
454+
# Run the bot
433455
bot.run(getenv("BOT_TOKEN"))

0 commit comments

Comments
 (0)