diff --git a/Gwendolyn.py b/Gwendolyn.py index a82bc44..966d8f0 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -23,17 +23,26 @@ meanWords = ["stupid", "bitch", "fuck", "dumb", "idiot"] # Runs Four in a Row async def fiar(channel,command,user): - response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user) + try: + response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user) + except: + logThis("Error parsing command (error code 1410)") + await channel.send(response) logThis(response,str(channel)) if showImage: if deleteImage: - oldImage = await deleteMessage("fourInARow"+str(channel),channel) + try: + oldImage = await deleteMessage("fourInARow"+str(channel),channel) + except: + logThis("Error deleting message (error code 1401)") oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png")) if gameDone == False: if gwendoTurn: - await asyncio.sleep(1) - response, showImage, deleteImage, gameDone, gwendoTurn = fourInARowAI(str(channel)) + try: + response, showImage, deleteImage, gameDone, gwendoTurn = fourInARowAI(str(channel)) + except: + logThis("AI error (error code 1420)") await channel.send(response) logThis(response,str(channel)) if showImage: @@ -501,187 +510,194 @@ async def parseCommands(message,content): # Runs a game of Blackjack elif content.startswith("blackjack"): - # Starts the game - if content == "blackjack" or content == "blackjack ": - cardsLeft = 0 - if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): - with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: - for _ in f: - cardsLeft += 1 - - # Shuffles if not enough cards - if cardsLeft < blackjackMinCards: - blackjackShuffle(blackjackDecks,str(message.channel)) - logThis("Shuffling the blackjack deck...",str(message.channel)) - await message.channel.send("Shuffling the deck...") - - new_message = blackjackStart(str(message.channel)) - if new_message == "started": + try: + # Starts the game + if content == "blackjack" or content == "blackjack ": + cardsLeft = 0 + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: + for _ in f: + cardsLeft += 1 + + # Shuffles if not enough cards + if cardsLeft < blackjackMinCards: + blackjackShuffle(blackjackDecks,str(message.channel)) + logThis("Shuffling the blackjack deck...",str(message.channel)) + await message.channel.send("Shuffling the deck...") + + new_message = blackjackStart(str(message.channel)) + if new_message == "started": - new_message = "Blackjack game started. Use \""+commandPrefix+"blackjack bet [amount]\" to enter the game within the next 30 seconds." - await message.channel.send(new_message) - oldImage = await message.channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(message.channel)+".png")) - - with open("resources/games/oldImages/blackjack"+str(message.channel), "w") as f: - f.write(str(oldImage.id)) - - await asyncio.sleep(30) - - gamedone = False - - with open("resources/games/games.json", "r") as f: - data = json.load(f) - if len(data["blackjack games"][str(message.channel)]["user hands"]) == 0: - gamedone = True - await message.channel.send("No one entered the game. Ending the game.") - gameID = data["blackjack games"][str(message.channel)]["id"] - - # Loop of game rounds - if gamedone == False: - logThis("!blackjack calling blackjackLoop()",str(message.channel)) - await blackjackLoop(message.channel,1,gameID) - else: - new_message = blackjackFinish(str(message.channel)) + new_message = "Blackjack game started. Use \""+commandPrefix+"blackjack bet [amount]\" to enter the game within the next 30 seconds." await message.channel.send(new_message) - else: - await message.channel.send(new_message) + oldImage = await message.channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(message.channel)+".png")) - # Entering game and placing bet - elif content.startswith("blackjack bet"): - commands = content.split(" ") - try: - amount = int(commands[2]) - except: - logThis("I didn't understand that",str(message.channel)) - response = "I didn't understand that" - else: - response = blackjackPlayerDrawHand(str(message.channel),message.author.display_name,amount) - await message.channel.send(response) - - # Hitting - elif content.startswith("blackjack hit"): - if content == "blackjack hit" or content == "blackjack hit ": - response = blackjackHit(str(message.channel),message.author.display_name) - else: + with open("resources/games/oldImages/blackjack"+str(message.channel), "w") as f: + f.write(str(oldImage.id)) + + await asyncio.sleep(30) + + gamedone = False + + with open("resources/games/games.json", "r") as f: + data = json.load(f) + if len(data["blackjack games"][str(message.channel)]["user hands"]) == 0: + gamedone = True + await message.channel.send("No one entered the game. Ending the game.") + gameID = data["blackjack games"][str(message.channel)]["id"] + + # Loop of game rounds + if gamedone == False: + logThis("!blackjack calling blackjackLoop()",str(message.channel)) + await blackjackLoop(message.channel,1,gameID) + else: + new_message = blackjackFinish(str(message.channel)) + await message.channel.send(new_message) + else: + await message.channel.send(new_message) + + # Entering game and placing bet + elif content.startswith("blackjack bet"): + commands = content.split(" ") + try: + amount = int(commands[2]) + except: + logThis("I didn't understand that",str(message.channel)) + response = "I didn't understand that" + else: + response = blackjackPlayerDrawHand(str(message.channel),message.author.display_name,amount) + await message.channel.send(response) + + # Hitting + elif content.startswith("blackjack hit"): + if content == "blackjack hit" or content == "blackjack hit ": + response = blackjackHit(str(message.channel),message.author.display_name) + else: + commands = content.split(" ") + try: + handNumber = int(commands[2]) + except: + handNumber = 0 + response = blackjackHit(str(message.channel),message.author.display_name,handNumber) + + if response.startswith("accept"): + await message.add_reaction("👍") + try: + if response[6] == "T": + with open("resources/games/games.json", "r") as f: + gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] + logThis("Hit calling blackjackLoop()",str(message.channel)) + await blackjackLoop(message.channel,int(response[7:])+1,gameID) + except: + logThis("Something fucked up") + await message.channel.send("something fucked up",str(message.channel)) + else: + await message.channel.send(response) + + + # Standing + elif content.startswith("blackjack stand"): + if content == "blackjack hit" or content == "blackjack hit ": + response = blackjackStand(str(message.channel),message.author.display_name) + else: + commands = content.split(" ") + try: + handNumber = int(commands[2]) + except: + handNumber = 0 + response = blackjackStand(str(message.channel),message.author.display_name,handNumber) + + if response.startswith("accept"): + await message.add_reaction("👍") + try: + if response[6] == "T": + with open("resources/games/games.json", "r") as f: + gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] + logThis("Stand calling blackjackLoop()",str(message.channel)) + await blackjackLoop(message.channel,int(response[7:])+1,gameID) + except: + logThis("Something fucked up",str(message.channel)) + await message.channel.send("something fucked up") + else: + await message.channel.send(response) + + # Doubling bet + elif content.startswith("blackjack double"): commands = content.split(" ") try: handNumber = int(commands[2]) except: handNumber = 0 - response = blackjackHit(str(message.channel),message.author.display_name,handNumber) + response, roundDone = blackjackDouble(str(message.channel),message.author.display_name,handNumber) - if response.startswith("accept"): - await message.add_reaction("👍") - try: - if response[6] == "T": - with open("resources/games/games.json", "r") as f: - gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] - logThis("Hit calling blackjackLoop()",str(message.channel)) - await blackjackLoop(message.channel,int(response[7:])+1,gameID) - except: - logThis("Something fucked up") - await message.channel.send("something fucked up",str(message.channel)) - else: await message.channel.send(response) - - # Standing - elif content.startswith("blackjack stand"): - if content == "blackjack hit" or content == "blackjack hit ": - response = blackjackStand(str(message.channel),message.author.display_name) - else: - commands = content.split(" ") try: - handNumber = int(commands[2]) - except: - handNumber = 0 - response = blackjackStand(str(message.channel),message.author.display_name,handNumber) - - if response.startswith("accept"): - await message.add_reaction("👍") - try: - if response[6] == "T": + if roundDone[0] == "T": with open("resources/games/games.json", "r") as f: gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] - logThis("Stand calling blackjackLoop()",str(message.channel)) - await blackjackLoop(message.channel,int(response[7:])+1,gameID) + logThis("Double calling blackjackLoop()",str(message.channel)) + await blackjackLoop(message.channel,int(roundDone[1:])+1,gameID) except: logThis("Something fucked up",str(message.channel)) await message.channel.send("something fucked up") - else: + + # Splitting hand + elif content.startswith("blackjack split"): + response, roundDone = blackjackSplit(str(message.channel),message.author.display_name) + await message.channel.send(response) - - # Doubling bet - elif content.startswith("blackjack double"): - commands = content.split(" ") - try: - handNumber = int(commands[2]) - except: - handNumber = 0 - response, roundDone = blackjackDouble(str(message.channel),message.author.display_name,handNumber) - await message.channel.send(response) - - try: + #try: if roundDone[0] == "T": with open("resources/games/games.json", "r") as f: gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] - logThis("Double calling blackjackLoop()",str(message.channel)) + logThis("Split calling blackjackLoop()",str(message.channel)) await blackjackLoop(message.channel,int(roundDone[1:])+1,gameID) - except: - logThis("Something fucked up",str(message.channel)) - await message.channel.send("something fucked up") - - # Splitting hand - elif content.startswith("blackjack split"): - response, roundDone = blackjackSplit(str(message.channel),message.author.display_name) + #except: + # logThis("Something fucked up") + # await message.channel.send("something fucked up") - await message.channel.send(response) + # Returning current hi-lo value + elif content.startswith("blackjack hilo") and message.author.display_name == "Nikolaj": + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/hilo/"+str(message.channel)+".txt", "r") as f: + data = f.read() + else: + data = "0" + await message.channel.send(data) - #try: - if roundDone[0] == "T": - with open("resources/games/games.json", "r") as f: - gameID = json.load(f)["blackjack games"][str(message.channel)]["id"] - logThis("Split calling blackjackLoop()",str(message.channel)) - await blackjackLoop(message.channel,int(roundDone[1:])+1,gameID) - #except: - # logThis("Something fucked up") - # await message.channel.send("something fucked up") + # Shuffles the blackjack deck + elif content.startswith("blackjack shuffle"): + blackjackShuffle(blackjackDecks,str(message.channel)) + logThis("Shuffling the blackjack deck...",str(message.channel)) + await message.channel.send("Shuffling the deck...") + + + # Tells you the amount of cards left + elif content.startswith("blackjack cards"): + cardsLeft = 0 + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: + for _ in f: + cardsLeft += 1 + + decksLeft = round(cardsLeft/52,1) + await message.channel.send(str(cardsLeft)+" cards, "+str(decksLeft)+" decks") - # Returning current hi-lo value - elif content.startswith("blackjack hilo") and message.author.display_name == "Nikolaj": - if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): - with open("resources/games/hilo/"+str(message.channel)+".txt", "r") as f: - data = f.read() else: - data = "0" - await message.channel.send(data) - - # Shuffles the blackjack deck - elif content.startswith("blackjack shuffle"): - blackjackShuffle(blackjackDecks,str(message.channel)) - logThis("Shuffling the blackjack deck...",str(message.channel)) - await message.channel.send("Shuffling the deck...") - - - # Tells you the amount of cards left - elif content.startswith("blackjack cards"): - cardsLeft = 0 - if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): - with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: - for _ in f: - cardsLeft += 1 - - decksLeft = round(cardsLeft/52,1) - await message.channel.send(str(cardsLeft)+" cards, "+str(decksLeft)+" decks") - - else: - await message.channel.send("I didn't quite understand that") + logThis("Not a command (error code 1301)") + await message.channel.send("I didn't quite understand that (error code 1301)") + except: + logThis("Something went wrong (error code 1300)") # Runs a game of four in a row elif content.startswith("fourinarow"): - command = content.replace("fourinarow","") - await fiar(message.channel,command,message.author.display_name) + try: + command = content.replace("fourinarow","") + await fiar(message.channel,command,message.author.display_name) + except: + logThis("Something went wrong (error code 1400)") diff --git a/resources/errorCodes.txt b/resources/errorCodes.txt index 3a4e368..e1061c1 100644 --- a/resources/errorCodes.txt +++ b/resources/errorCodes.txt @@ -87,4 +87,7 @@ 1300 - Unspecified 14 - Four in a row -600 - Unspecified \ No newline at end of file +1400 - Unspecified +1401 - Error deleting old image +1410 - Unspecified parsing error +1420 - Unspecified AI error \ No newline at end of file