Пример #1
0
            public static void SetAccessLevel(ulong id, MessageSystem.AccessLevel accessLevel = MessageSystem.AccessLevel.User)
            {
                using (Database.GAFContext context = new Database.GAFContext())
                {
                    var buser = context.BotUsers.First(bm => (ulong)bm.DiscordId == id);

                    if (buser == null)
                    {
                        return;
                    }

                    buser.AccessLevel = (short)accessLevel;

                    context.BotUsers.Update(buser);
                    context.SaveChanges();
                }
            }
Пример #2
0
        private static void ProcessConsoleArg(string arg)
        {
            switch (arg.ToLower())
            {
            //Creates empty config json in (Install/config.json)
            case "-createconfig":
                CreateConfig();
                return;

            //Creates installation sql's to execute against db (Install/)
            case "-createsql":
                CreateSQL();
                return;

            case "-createdbstring":
                CreateDBString();
                return;

            //Executes all sql's against the db (Install/)
            case "-installsql":
                InstallSQL();
                return;

            case "-installcfg":
            case "-installconfig":
                InstallConfig();
                return;

            case "-finished":
            case "-exit":
            case "-abort":
                Exit();
                return;

            case "-waitforkey":
                Console.WriteLine("(Press any key to continue)");
                Console.ReadKey();
                return;

            case "-waitforline":
                Console.WriteLine("(Press 'Enter' to continue)");
                Console.ReadLine();
                return;

            case "-emptyLine":
            case "-writeLine":
                Console.WriteLine();
                return;

            case "-cls":
            case "-clear":
                Console.Clear();
                return;
            }

            if (arg.ToLower().StartsWith("-print:"))
            {
                string toPrint = arg.Remove(0, 7).Replace('_', ' ');
                Console.WriteLine(toPrint);
                return;
            }
            else if (arg.ToLower().StartsWith("-colorfore:"))
            {
                string color = arg.Remove(0, 11);

                if (!Enum.TryParse(color, out ConsoleColor result))
                {
                    Console.WriteLine("Could not find color: " + color);
                    return;
                }

                Console.ForegroundColor = result;
            }
            else if (arg.ToLower().StartsWith("-colorback:"))
            {
                string color = arg.Remove(0, 11);

                if (!Enum.TryParse(color, out ConsoleColor result))
                {
                    Console.WriteLine("Could not find color: " + color);
                    return;
                }

                Console.BackgroundColor = result;
            }

            void CreateConfig()
            {
                BotConfig config = new BotConfig()
                {
                    AnalyzeChannel               = 1234567890,
                    AutoSaveTime                 = TimeSpan.FromMinutes(15),
                    OsuApiKeyEncrypted           = "Api Key",
                    DiscordClientSecretEncrypted = "Client Secret",
                    DiscordGuildId               = 1234567890,
                    OsuIrcHost = "irc.ppy.sh",
                    OsuIrcPasswordEncrypted = "Irc Password",
                    OsuIrcPort           = 6667,
                    OsuIrcUser           = "******",
                    RefereeRoleId        = 1234567890,
                    SetVerifiedName      = false,
                    SetVerifiedRole      = true,
                    VerifiedRoleId       = 1234567890,
                    WarmupMatchCount     = 2,
                    WebsiteHost          = "Address",
                    WebsitePassEncrypted = "Password",
                    WebsiteUser          = "******",
                    WelcomeChannel       = 1234567890,
                    WelcomeMessage       = "Welcome to our discord!",
                    CurrentSeason        = "Season 1"
                };
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(config, Newtonsoft.Json.Formatting.Indented);
                string file = Path.Combine(CurrentPath, "Install/config.json");

                FileInfo fi = new FileInfo(file);

                if (!fi.Directory.Exists)
                {
                    fi.Directory.Create();
                }
                else if (fi.Exists)
                {
                    fi.Delete();
                }

                File.WriteAllText(fi.FullName, json);
            }

            void CreateSQL()
            {
                FileInfo sqlFile = new FileInfo(Path.Combine(CurrentPath, "Install/db.sql"));

                if (!sqlFile.Directory.Exists)
                {
                    sqlFile.Directory.Create();
                }
                else if (sqlFile.Exists)
                {
                    sqlFile.Delete();
                }

                File.WriteAllText(sqlFile.FullName, Resources.db);
            }

            void CreateDBString()
            {
                FileInfo dbConFile = new FileInfo(Path.Combine(CurrentPath, "dbconnection.string"));

                if (dbConFile.Exists)
                {
                    dbConFile.Delete();
                }

                File.WriteAllText(dbConFile.FullName, Resources.DefaultDBConnectionString);
            }

            void InstallSQL()
            {
                FileInfo sqlFile = new FileInfo(Path.Combine(CurrentPath, "Install/db.sql"));

                List <string>   sql             = File.ReadAllLines(sqlFile.FullName).Where(l => !string.IsNullOrEmpty(l)).ToList();
                AsciiLoadingBar asciiLoadingBar = new AsciiLoadingBar(250, sql.Count, 0, "Installing SQL");

                asciiLoadingBar.Enable();

                using (Database.GAFContext context = new Database.GAFContext())
                {
                    for (int i = 0; i < sql.Count; i++)
                    {
                        string line = sql[i];

                        if (string.IsNullOrEmpty(line))
                        {
                            continue;
                        }

                        try
                        {
                            context.Database.ExecuteSqlRaw(line);
                        }
                        catch (Exception ex)
                        {
                            asciiLoadingBar.Disable();
                            Console.WriteLine(line);
                            throw ex;
                        }

                        asciiLoadingBar.Increment();
                        //asciiLoadingBar.RefreshManually();
                    }

                    context.SaveChanges();
                }
                asciiLoadingBar.RefreshManually();
                asciiLoadingBar.Disable();
            }

            void InstallConfig()
            {
                FileInfo  configFile = new FileInfo(Path.Combine(CurrentPath, "Install/config.json"));
                string    json       = File.ReadAllText(configFile.FullName);
                BotConfig config     = Newtonsoft.Json.JsonConvert.DeserializeObject <BotConfig>(json);

                config.DiscordClientSecretEncrypted = EncryptString(config.DiscordClientSecretEncrypted);
                config.OsuApiKeyEncrypted           = EncryptString(config.OsuApiKeyEncrypted);
                config.OsuIrcPasswordEncrypted      = EncryptString(config.OsuIrcPasswordEncrypted);
                config.WebsitePassEncrypted         = EncryptString(config.WebsitePassEncrypted);

                using (Database.GAFContext context = new Database.GAFContext())
                {
                    context.BotConfig.Add(config);
                    context.SaveChanges();
                }
            }

            void Exit()
            {
                Environment.Exit(0);
            }
        }