From 4994c40bd3d2f1da07d6d859894cc536bd92d415 Mon Sep 17 00:00:00 2001 From: Kowlin Date: Fri, 15 Apr 2016 23:23:14 +0200 Subject: [PATCH] Added support for Bot tokens (#138) Added support for Bot tokens --- cogs/utils/settings.py | 18 ++++++++++++++- red.py | 50 ++++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 15 deletions(-) diff --git a/cogs/utils/settings.py b/cogs/utils/settings.py index bb5ce3cbc..cb132b828 100644 --- a/cogs/utils/settings.py +++ b/cogs/utils/settings.py @@ -8,11 +8,18 @@ class Settings: def __init__(self,path=default_path): self.path = path self.check_folders() - self.default_settings = {"EMAIL" : "EmailHere", "PASSWORD" : "PasswordHere", "OWNER" : "id_here", "PREFIXES" : [], "default":{"ADMIN_ROLE" : "Transistor", "MOD_ROLE" : "Process"}} + self.default_settings = {"EMAIL" : "EmailHere", "PASSWORD" : "", "OWNER" : "id_here", "PREFIXES" : [], "default":{"ADMIN_ROLE" : "Transistor", "MOD_ROLE" : "Process"}, "LOGIN_TYPE" : "email"} if not fileIO(self.path,"check"): self.bot_settings = self.default_settings self.save_settings() else: + current = fileIO(self.path, "load") + if current.keys() != self.default_settings.keys(): + for key in self.default_settings.keys(): + if key not in current.keys(): + current[key] = self.default_settings[key] + print("Adding " + str(key) + " field to red settings.json") + fileIO(self.path, "save", current) self.bot_settings = fileIO(self.path,"load") if "default" not in self.bot_settings: self.update_old_settings() @@ -106,6 +113,15 @@ class Settings: ret.update({server:self.bot_settings[server]}) return ret + @property + def login_type(self): + return self.bot_settings["LOGIN_TYPE"] + + @login_type.setter + def login_type(self,value): + self.bot_settings["LOGIN_TYPE"] = value + self.save_settings() + def get_server(self,server): if server is None: return self.bot_settings["default"].copy() diff --git a/red.py b/red.py index 3f06af7e0..0ff4b8965 100644 --- a/red.py +++ b/red.py @@ -345,17 +345,36 @@ def check_folders(): def check_configs(): if settings.bot_settings == settings.default_settings: - print("Red - First run configuration") - print("If you don't have one, create a NEW ACCOUNT for Red. Do *not* use yours. (https://discordapp.com)") - settings.email = input("\nEmail> ") - settings.password = input("\nPassword> ") - - if not settings.email or not settings.password: - input("Email and password cannot be empty. Restart Red and repeat the configuration process.") + print("Welcome to Red. A modular Discord bot!") + print("Please choose on how you want to log in, you can either choose a normal or bot account.") + print("For more information on bot accounts visit: https://discordapp.com/developers/docs/topics/oauth2#bot-vs-user-accounts") + print("If you don't know that you NEED a bot-tagged account, don't use one. There are more than just cosmetic differences. If still in doubt, select email") + print("Please choose if you want to use a Email or Token.") + settings.login_type = input("\n'email' or 'token'> ").lower() + # Deny empty logintype! + if not settings.login_type: + input("You have to choose either 'email' or 'token'") exit(1) - - if "@" not in settings.email: - input("You didn't enter a valid email. Restart Red and repeat the configuration process.") + # Check if input is either email or token, if email to on to email & password. else go to token input. + if settings.login_type == "email": + print("If you don't have one, create a NEW ACCOUNT for Red. Do *not* use yours. (https://discordapp.com)") + settings.email = input("\nEmail> ") + settings.password = input("\nPassword> ") + if not settings.email: + input("Email cannot be empty. Restart Red and repeat the configuration process.") + exit(1) + if not settings.password: + input("Password cannot be empty. Restart Red and repeat the configuration process.") + exit(1) + elif settings.login_type == "token": + print("Get your bot token from: https://discordapp.com/developers/applications/me") + print("If you don't have a application or a bot token, make one and a bot account then copy the token.") + settings.email = input("\nToken> ") + if not settings.email: + input("Token cannot be empty. Restart Red and repeat the configuration process.") + exit(1) + else: + input("Please enter 'email' or 'token'. Restart Red and repeat the configuration process.") exit(1) print("\nChoose a prefix (or multiple ones, one at once) for the commands. Type exit when you're done. Example prefix: !") @@ -365,7 +384,7 @@ def check_configs(): new_prefix = input("Prefix> ") if new_prefix.lower() != "exit" and new_prefix != "": prefixes.append(new_prefix) - #Remember we're using property's here, oh well... + # Remember we're using property's here, oh well... settings.prefixes = sorted(prefixes, reverse=True) print("\nIf you know what an User ID is, input *your own* now and press enter.") @@ -488,8 +507,11 @@ def main(): if settings.owner == "id_here": print("Owner has not been set yet. Do '{}set owner' in chat to set yourself as owner.".format(bot.command_prefix[0])) else: - owner.hidden = True # Hides the set owner command from help - yield from bot.login(settings.email, settings.password) + owner.hidden = True # Hides the set owner command from help + if settings.login_type == "token": + yield from bot.login(settings.email) + else: + yield from bot.login(settings.email, settings.password) yield from bot.connect() if __name__ == '__main__': @@ -498,7 +520,7 @@ if __name__ == '__main__': loop.run_until_complete(main()) except discord.LoginFailure: print("Invalid login credentials. Restart Red and configure it properly.") - os.remove('data/red/settings.json') # Hopefully this won't backfire in case of discord servers' problems + os.remove('data/red/settings.json') # Hopefully this won't backfire in case of discord servers' problems except Exception as e: print(e) loop.run_until_complete(bot.logout())