From cf5e07851bea158afb4e647c9fcc17f04d8339f1 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Sun, 3 Jan 2016 15:58:04 +0100 Subject: [PATCH] Name restrictions removed, settings check (...) Name restrictions have been removed. It no longer force upon the user the .Name() format. Settings are now checked every boot for consistency. Slot machine now shows credits left every play --- dataIO.py | 33 ++++++++++++++++++++- economy.py | 2 ++ red.py | 86 ++++++++++++++++++++++++++++-------------------------- 3 files changed, 79 insertions(+), 42 deletions(-) diff --git a/dataIO.py b/dataIO.py index da82d7ecf..f17a4a111 100644 --- a/dataIO.py +++ b/dataIO.py @@ -1,6 +1,8 @@ import json import logging +default_settings = ('{"TRIVIA_ADMIN_ONLY": false, "EDIT_CC_ADMIN_ONLY": false, "PASSWORD": "PASSWORDHERE", "FILTER": true, "CUSTOMCOMMANDS": true, ' + + '"TRIVIAMAXSCORE": 10, "TRIVIADELAY": 15, "LOGGING": true, "EMAIL": "EMAILHERE", "ADMINROLE": "Transistor"}') logger = logging.getLogger("__main__") @@ -39,4 +41,33 @@ def loadWords(): with open("words.dat", "r") as f: for line in f: w += line - return w \ No newline at end of file + return w + +def loadAndCheckSettings(): + to_delete = [] + try: + current_settings = fileIO("settings.json", "load") + default = json.loads(default_settings) + if current_settings.keys() != default.keys(): + logger.warning("Something wrong detected with settings.json. Starting check...") + for field in default: + if field not in current_settings: + logger.info("Adding " + field + " field.") + current_settings[field] = default[field] + for field in current_settings: + if field not in default: + logger.info("Removing " + field + " field.") + to_delete.append(field) + for field in to_delete: + del current_settings[field] + logger.warning("Your settings.json was deprecated (missing or useless fields detected). I fixed it. " + + "If the file was missing any field I've added it and put default values. You might want to check it.") + fileIO("settings.json", "save", current_settings) + return current_settings + except IOError: + fileIO("settings.json", "save", json.loads(default_settings)) + logger.error("Your settings.json is missing. I've created a new one. Edit it with your settings and restart me.") + exit(1) + except: + logger.error("Your settings.json seems to be invalid. Check it. If you're unable to fix it delete it and I'll create a new one the next start.") + exit(1) \ No newline at end of file diff --git a/economy.py b/economy.py index a63ce8e44..0d3dff82b 100644 --- a/economy.py +++ b/economy.py @@ -204,8 +204,10 @@ async def slotMachine(message, bid): else: await client.send_message(message.channel, "{}{} `Nothing! Lost bet.` ".format(display_reels, message.author.mention)) withdrawMoney(message.author.id, bid) + await client.send_message(message.channel, "`Credits left: {}`".format(str(checkBalance(message.author.id)))) return True addMoney(message.author.id, bid) + await client.send_message(message.channel, "`Current credits: {}`".format(str(checkBalance(message.author.id)))) ####################################### diff --git a/red.py b/red.py index ff56f59c1..765139f6c 100644 --- a/red.py +++ b/red.py @@ -26,8 +26,6 @@ import youtubeparser from sys import modules -settings = dataIO.fileIO("settings.json", "load") - help = """**Commands list:** !flip - Flip a coin !rps [rock or paper o scissors] - Play rock paper scissors @@ -112,12 +110,14 @@ async def on_message(message): await talk(message) if message.channel.id not in shush_list: - if message.content.lower() == settings["NAME"].lower() + "?": - await client.send_message(message.channel, "`" + choice(greetings) + "`") - elif message.content == settings["NAME"].upper(): + if message.content == client.user.name.upper() or message.content == client.user.name.upper() + "?": await client.send_message(message.channel, "`" + choice(greetings_caps) + "`") + elif message.content.lower() == client.user.name.lower() + "?": + await client.send_message(message.channel, "`" + choice(greetings) + "`") + elif message.content == client.user.mention + " ?" or message.content == client.user.mention + "?": + await client.send_message(message.channel, "`" + choice(greetings) + "`") elif message.content == "!flip": - await client.send_message(message.channel, "`" + settings["NAME"] + " flips a coin and... " + choice(["HEADS!`", "TAILS!`"])) + await client.send_message(message.channel, "*flips a coin and... " + choice(["HEADS!*", "TAILS!*"])) elif message.content.startswith("!rps"): await rpsgame(message) elif message.content == "!proverb": @@ -247,11 +247,8 @@ async def on_message(message): @client.async_event async def on_ready(): - logger.info(settings["NAME"] + " is online. (" + client.user.id + ")") + logger.info("I'm online " + "(" + client.user.id + ")") await gameSwitcher.changeGame(now=True) - if client.user.name != settings["NAME"]: - name = "." + settings["NAME"] + "()" - await client.edit_profile(settings["PASSWORD"], username=name) # cns = threading.Thread(target=console, args=[]) # cns.start() # console, WIP @@ -951,9 +948,13 @@ async def sendPlaylist(message): async def shutdown(message): if isMemberAdmin(message): - await client.send_message(message.channel, "`" + settings["NAME"] + " shutting down... See you soon.` :hand:") + await client.send_message(message.channel, client.user.name + " shutting down... See you soon. :hand:") await client.logout() - exit(1) + try: + exit(1) + except SystemExit: #clean exit + logger.info("Shutting down as requested by " + message.author.id + "...") + pass else: await client.send_message(message.channel, "`I don't take orders from you.`") @@ -1041,13 +1042,9 @@ async def changeName(message): msg = message.content.split() if len(msg) == 2: try: - name = "." + msg[1] + "()" - await client.edit_profile(settings["PASSWORD"], username=name) - settings["NAME"] = msg[1] - dataIO.fileIO("settings.json", "save", settings) - logger.info("Saved settings.") - except: - pass + await client.edit_profile(settings["PASSWORD"], username=msg[1]) + except Exception as e: + print(e) else: await client.send_message(message.channel, "`!name [new name]`") else: @@ -1135,10 +1132,6 @@ def console(): traceback.print_exc() print("\n") -logger = loggerSetup() - -dataIO.logger = logger - def loadDataFromFiles(loadsettings=False): global proverbs, commands, trivia_questions, badwords, badwords_regex, shush_list @@ -1152,7 +1145,7 @@ def loadDataFromFiles(loadsettings=False): # logger.info("Loaded " + str(len(trivia_questions)) + " questions.") badwords = dataIO.fileIO("filter.json", "load") - logger.info("Loaded " + str(len(badwords)) + " words.") + logger.info("Loaded " + str(len(badwords)) + " lists of filtered words.") badwords_regex = dataIO.fileIO("regex_filter.json", "load") logger.info("Loaded " + str(len(badwords_regex)) + " regex lists.") @@ -1164,31 +1157,42 @@ def loadDataFromFiles(loadsettings=False): global settings settings = dataIO.fileIO("settings.json", "load") +def main(): + global ball, greetings, greetings_caps, stopwatches, trivia_sessions, message, gameSwitcher, uptime_timer, musicPlayer, currentPlaylist + global logger, settings -loadDataFromFiles() + logger = loggerSetup() + dataIO.logger = logger -ball = ["As I see it, yes", "It is certain", "It is decidedly so", "Most likely", "Outlook good", - "Signs point to yes", "Without a doubt", "Yes", "Yes – definitely", "You may rely on it", "Reply hazy, try again", - "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", - "Don't count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"] + settings = dataIO.loadAndCheckSettings() -greetings = ["Hey.", "Yes?", "Hi.", "I'm listening.", "Hello.", "I'm here."] -greetings_caps = ["DON'T SCREAM", "WHAT", "WHAT IS IT?!", "ì_ì", "NO CAPS LOCK"] + loadDataFromFiles() -stopwatches = {} + ball = ["As I see it, yes", "It is certain", "It is decidedly so", "Most likely", "Outlook good", + "Signs point to yes", "Without a doubt", "Yes", "Yes – definitely", "You may rely on it", "Reply hazy, try again", + "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", + "Don't count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"] -trivia_sessions = [] + greetings = ["Hey.", "Yes?", "Hi.", "I'm listening.", "Hello.", "I'm here."] + greetings_caps = ["DON'T SCREAM", "WHAT", "WHAT IS IT?!", "ì_ì", "NO CAPS LOCK"] -message = "" + stopwatches = {} -gameSwitcher = botPlays() + trivia_sessions = [] -if "economy" in modules: - economy.initialize(client) + message = "" -uptime_timer = int(time.perf_counter()) + gameSwitcher = botPlays() -musicPlayer = None -currentPlaylist = None + if "economy" in modules: + economy.initialize(client) -client.run(settings["EMAIL"], settings["PASSWORD"]) \ No newline at end of file + uptime_timer = int(time.perf_counter()) + + musicPlayer = None + currentPlaylist = None + + client.run(settings["EMAIL"], settings["PASSWORD"]) + +if __name__ == '__main__': + main() \ No newline at end of file