public static string HandleTokenDecrypt() { if (BotToken.UsingPassword()) { bool retry = true; while (retry) { Console.Write("Enter your password: "******"Checksum mismatch. (New bot version or corrupted file)"); return(PromptAndStoreToken()); } else if (!verified) { Console.Write("Could not decrypt token with that password. Try again? (y/n) "); string yesno = Console.ReadLine(); retry = (yesno.Length > 0 && yesno.Substring(0, 1).ToLower() == "y"); } else { Console.WriteLine("Token decrypted."); return(BotToken.GetTokenString()); } } // User decided to stop retrying string pathToTokenFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "token").ToString(); Console.WriteLine($"Store a new token by deleting the token file at {pathToTokenFile}"); } else { bool verified = BotToken.DecryptAndVerify(); if (!verified) { Console.WriteLine("Checksum mismatch. (New bot version or corrupted file)"); return(PromptAndStoreToken()); } else { Console.WriteLine("Token decrypted."); return(BotToken.GetTokenString()); } } // Couldn't get a token return(""); }
public async Task MainAsync(string[] cmdArgs) { // List of items to clean up when the bot exits _disposables = new List <IDisposable>(); ArgParse args = new ArgParse(); args.AddDefs(_ARG_DEFS); args.Parse(cmdArgs); bool encryptOnlyMode = args.GetFlag("encrypt-only"); string givenPassword = args.GetValue("password"); string givenToken = args.GetValue("token"); string token = String.Empty; if (encryptOnlyMode) { Console.WriteLine("Encrypt-only mode."); if (givenToken != String.Empty) { if (givenPassword != String.Empty) { Console.WriteLine("Using password."); } else { Console.WriteLine("Not using password."); } StoreToken(givenToken, givenPassword); Console.WriteLine("Token stored successfully."); } else { Console.WriteLine("Encrypt-only mode must be used with a token. Run with option --token <string>."); } return; } else if (givenToken != String.Empty) { Console.WriteLine("Storing token."); if (givenPassword != String.Empty) { Console.WriteLine("Using password."); } else { Console.WriteLine("Not using password."); } StoreToken(givenToken, givenPassword); token = givenToken; } else { // Handle user-mode BotToken.ReadToken(); switch (BotToken.GetTokenState()) { case BotTokenState.Unchecked: case BotTokenState.Valid: Console.WriteLine("Loading Discord bot token."); token = HandleTokenDecrypt(); break; case BotTokenState.Missing: case BotTokenState.Outdated: case BotTokenState.Corrupted: token = PromptAndStoreToken(); break; } } if (token == String.Empty) { Console.WriteLine("Stopping. (Press any key to end)"); Console.ReadKey(); return; } var config = new DiscordSocketConfig(); config.AlwaysDownloadUsers = true; var client = new DiscordSocketClient(config); client.Log += Log; client.Ready += Ready; client.MessageReceived += MessageReceived; _adminList = new AdminList(client); _disposables.Add(_adminList); Server watsonLocal = new Server("localhost", 9000, false, DefaultRoute, false); watsonLocal.AddStaticRoute("get", "/hello/", HelloRoute); watsonLocal.AddStaticRoute("get", "/queue/", QueueRoute); _msgQueue = new Queue(); await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); await Task.Delay(-1); }