async def print_info_from_file(path, hobchannel): #special case for completed: only list hobby names, no info if (path == PATH_COMPLETE): jsondata = futils.get_json_from_file(path) outstr = "" for key in sorted(jsondata.keys()): outstr += "- " + key + "\n" outstr = outstr.strip() await hobchannel.send(outstr) return try: jsondata = futils.get_json_from_file(path) except json.decoder.JSONDecodeError: jsondata = None outstr = "" #text file, print line-by-line if jsondata == None: with open(path) as f: for line in f.readlines(): outstr += line #else, it's json, print it else: outstr = helpers.prettify_dict(jsondata).strip() #discord only allows messages up to 2k - to not spam the channel, #we'll just upload the file if it's longer if len(outstr) < 2000: await hobchannel.send(outstr) else: await upload_file_to_channel(path, hobchannel)
async def pick_hobby_from_later(hobby, hobchannel): currentjson = futils.get_json_from_file(PATH_CURRENT) #if we already have a hobby, don't overwrite it if currentjson["name"] != JSON_NO_HOBBY: current = currentjson["name"] await hobchannel.send( f"The current hobby is {current}. Use !complete, !veto, or !later to close this hobby." ) return laterjson = futils.get_json_from_file(PATH_LATER) #existing hobby info from later.json try: newhobbyjson = laterjson[hobby] except KeyError: await hobchannel.send( f"Could not find {hobby} in later.json (was KeyError).") return #idk if i need this still if (newhobbyjson == None): await hobchannel.send( f"Could not find {hobby} in later.json (was None).") return vetoers = newhobbyjson["vetoers"] notes = newhobbyjson["notes"] with open(PATH_CURRENT, "w") as currentfile: newjson = { "name": hobby.capitalize(), "vetoers": vetoers, "notes": notes } currentfile.write(json.dumps(newjson)) #remove from later.json del laterjson[hobby] with open(PATH_LATER, "w") as laterfile: json.dump(laterjson, laterfile) await hobchannel.send(f"{hobby} has been picked as the new hobby!")
async def handle_veto(author, hobchannel): current = info.get_current_hobby_name() vetoes = info.get_current_vetoes() vetoers = info.get_current_vetoers() nick = author.nick if (nick == None): nick = author.name if (author.name in vetoers): await hobchannel.send(f"{nick} has already vetoed {current}!") return if (vetoes + 1) >= NUM_VETOES_TO_SKIP: #creating this now means we don't have to do anything #fancy to get an 'and' in the output string vetoersstr = ", ".join(vetoers) #add user to vetoers vetoers.append(author.name) jsondata = futils.get_json_from_file(PATH_CURRENT) jsondata["vetoers"] = vetoers with open(PATH_CURRENT, "w") as currentfile: json.dump(jsondata, currentfile) helpers.move_current_to_other_file(PATH_VETOED, hobchannel) await hobchannel.send( f"{current} has been vetoed by {vetoersstr}, and {author.name}!") else: vetoers.append(author.name) #use username, not nickname jsondata = futils.get_json_from_file(PATH_CURRENT) jsondata["vetoers"] = vetoers with open(PATH_CURRENT, "w") as currentfile: json.dump(jsondata, currentfile) await hobchannel.send(f"{nick} has voted to veto {current}!")
async def print_hobby_info(hobby, hobchannel): completejson = futils.get_json_from_file(PATH_COMPLETE) try: hobbyjson = completejson[hobby] date = hobbyjson["date"] vetoers = hobbyjson["vetoers"] notes = hobbyjson["notes"] outstr = f"{hobby} (Completed {date})\nVetoed by: {vetoers}\n\~\~\~Notes\~\~\~\n{notes}" await hobchannel.send(outstr) except KeyError: await hobchannel.send(f"Could not find {hobby} in `complete.json`. " + \ "Keep in mind that my brain is small and it has to match perfectly, including capitalization.")
async def add_note_to_current(note, authorname, hobchannel): note = helpers.unembed_links(note) jsondata = futils.get_json_from_file(PATH_CURRENT) current = info.get_current_hobby_name() notes = info.get_current_notes() notes += ("\n" if len(notes) > 0 else "") + note + f" ({authorname})" jsondata["notes"] = notes with open(PATH_CURRENT, "w") as currentfile: json.dump(jsondata, currentfile) await hobchannel.send(f"Added note to {current}. Full notes:\n{notes}")
async def new_hobby(hobchannel): currentjson = futils.get_json_from_file(PATH_CURRENT) #if we already have a hobby, don't overwrite it if currentjson["name"] != JSON_NO_HOBBY: current = currentjson["name"] await hobchannel.send( f"The current hobby is {current}. Use !complete, !veto, or !later to close this hobby." ) return numhobbies = futils.file_length(PATH_TODO) newhobbynum = randint(1, numhobbies) with open(PATH_TODO, "r") as todofile: lines = todofile.readlines() #pick a new hobby newhobby = lines[newhobbynum].strip() #remove the new hobby from the todo list with open(PATH_TODO, "w") as todofile: for line in lines: if line.strip() != newhobby: todofile.write(line) with open(PATH_CURRENT, "w") as currentfile: newjson = {"name": newhobby, "vetoers": [], "notes": ""} currentfile.write(json.dumps(newjson)) lastmsg = await hobchannel.send("Next week's hobby is") sleep(1) await lastmsg.edit(content="Next week's hobby is .") sleep(1) await lastmsg.edit(content="Next week's hobby is . .") sleep(1) await lastmsg.edit(content="Next week's hobby is . . .") sleep(1) await hobchannel.send( f"\~\~\~\~\~\~\~\~\~\~ {newhobby}! \~\~\~\~\~\~\~\~\~\~")
def get_current_hobby_name(): currentjson = futils.get_json_from_file(PATH_CURRENT) return currentjson["name"]
def get_current_notes(): currentjson = futils.get_json_from_file(PATH_CURRENT) return currentjson["notes"]
def get_current_vetoers(): currentjson = futils.get_json_from_file(PATH_CURRENT) return currentjson["vetoers"]