From b971c0d5220559d92d201357fa8154642bc74be3 Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Mon, 27 Jul 2020 18:19:14 +0200 Subject: [PATCH] :moneybag: Added money-based commands --- Gwendolyn.py | 25 ++++++++++++++++++++++++- funcs/__init__.py | 2 +- funcs/games/__init__.py | 1 + funcs/games/money.py | 40 ++++++++++++++++++++++++++++++++++++++++ funcs/games/trivia.py | 27 +++++++++++++++------------ resources/help/help.txt | 4 ++++ 6 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 funcs/games/money.py diff --git a/Gwendolyn.py b/Gwendolyn.py index 6abaeb6..41551a1 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -268,7 +268,7 @@ async def on_message(message): elif message.content.lower().startswith("!trivia "): command = message.content.lower().replace("!trivia ","") - response = funcs.triviaOtherThing(message.author.name,str(message.channel),command) + response = funcs.triviaOtherThing(message.author.name.lower(),str(message.channel),command) if response.startswith("Locked in "): await message.add_reaction("👍") else: @@ -277,6 +277,29 @@ async def on_message(message): funcs.logThis("I didn't understand that") await message.channel.send("I didn't understand that") + + #Checks your point balance + elif message.content.lower().startswith("!balance"): + funcs.logThis(message.author.name+" ran \""+message.content+"\"") + response = funcs.checkBalance(message.author.name.lower()) + await message.channel.send(message.author.name + " has " + str(response) + " points") + + #gives money to other player + elif message.content.lower().startswith("!give "): + funcs.logThis(message.author.name+" ran \""+message.content+"\"") + commands = message.content.lower().split(" ") + if len(commands) >= 3: + try: + amount = int(commands[2]) + response = funcs.giveMoney(message.author.name.lower(),commands[1],amount) + await message.channel.send(response) + except: + funcs.logThis("I didn't quite understand that") + await message.channel.send("I didn't quite understand that") + else: + funcs.logThis("I didn't understand that") + await message.channel.send("I didn't understand that") + # Is a bit sassy sometimes meanWords = ["stupid", "bitch", "fuck", "dumb", "idiot"] diff --git a/funcs/__init__.py b/funcs/__init__.py index a054d51..34a195c 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -6,6 +6,6 @@ from .lookup import spellFunc, monsterFunc from .other import nameGen, tavernGen, movieFunc -from .games import triviaStart, triviaOtherThing, triviaCountPoints +from .games import triviaStart, triviaOtherThing, triviaCountPoints, checkBalance, giveMoney from .roll import roll_dice \ No newline at end of file diff --git a/funcs/games/__init__.py b/funcs/games/__init__.py index 2ac73e2..f4d3ef3 100644 --- a/funcs/games/__init__.py +++ b/funcs/games/__init__.py @@ -1 +1,2 @@ +from .money import checkBalance, giveMoney from .trivia import triviaCountPoints, triviaStart, triviaOtherThing \ No newline at end of file diff --git a/funcs/games/money.py b/funcs/games/money.py new file mode 100644 index 0000000..249cd2a --- /dev/null +++ b/funcs/games/money.py @@ -0,0 +1,40 @@ +import json + +from funcs import logThis + +def checkBalance(user): + with open("resources/games.json", "r") as f: + data = json.load(f) + + if user in data["users"]: + return data["users"][user] + else: return 0 + +def addMoney(user,amount): + with open("resources/games.json", "r") as f: + data = json.load(f) + + if user in data["users"]: + points = data["users"][user] + data["users"][user] = points + amount + else: + data["users"][user] = amount + + with open("resources/games.json", "w") as f: + json.dump(data,f,indent=4) + +def giveMoney(user,targetUser,amount): + with open("resources/games.json", "r") as f: + data = json.load(f) + + if user in data["users"]: + if data["users"][user] >= amount: + addMoney(user,-1 * amount) + addMoney(targetUser,amount) + return "Transferred the points" + else: + logThis("They didn't have enough points") + return "You don't have that many points" + else: + logThis("They didn't have enough points") + return "You don't have that many points" diff --git a/funcs/games/trivia.py b/funcs/games/trivia.py index 0be0da8..ebaa0d2 100644 --- a/funcs/games/trivia.py +++ b/funcs/games/trivia.py @@ -2,6 +2,7 @@ import json import urllib import random +from . import money from funcs import logThis def triviaStart(channel : str): @@ -24,7 +25,19 @@ def triviaStart(channel : str): with open("resources/games.json", "w") as f: json.dump(triviaFile,f,indent=4) - question = data["results"][0]["question"].replace("'","\'").replace(""","\"") + replacements = {"'": "\'", + """: "\"", + "“": "\"", + "”": "\"", + "é": "é"} + question = data["results"][0]["question"] + + for key, value in replacements.items(): + question = question.replace(key,value) + + for answer in answers: + for key, value in replacements.items(): + answer = answer.replace(key,value) return question, answers, correctAnswer else: @@ -50,10 +63,6 @@ def triviaOtherThing(user : str, channel : str, command : str): return user+" has already answered this question" else: return "There's no question right now" - elif command == "p": - items = {k: v for k, v in sorted(data["users"].items(), key=lambda item: item[1])} - - return "\n".join(['%s: %s' % (key, value) for (key, value) in items.items()]) else: return "I didn't quite understand that" @@ -66,14 +75,8 @@ def triviaCountPoints(channel : str): if channel in data["trivia questions"]: for player, answer in data["trivia questions"][channel]["players"].items(): if answer == data["trivia questions"][channel]["answer"]: - if player in data["users"]: - points = data["users"][player] - data["users"][player] = points + 1 - else: - data["users"][player] = 1 + money.addMoney(player,1) - with open("resources/games.json", "w") as f: - json.dump(data,f,indent=4) else: logThis("Couldn't find the question") diff --git a/resources/help/help.txt b/resources/help/help.txt index 74c2c80..3ad6cd1 100644 --- a/resources/help/help.txt +++ b/resources/help/help.txt @@ -18,6 +18,10 @@ **!trivia** - Lader dig spille et spil trivia. +**!balance** - Viser dig hvor mange point du har. + +**!give** - Lader dig give point til andre. + **!swchar** - Lader dig lave en Star Wars karakter. **!swroll** - Lader dig rulle Star Wars terninger.