Exemplo n.º 1
0
        internal static async Task <Result> GetFiles(BotSettings bSettings, DBSettings dbSettings)
        {
            using (var dbClient = new DropboxClient(Constants._DBTOKEN_))
            {
                if (File.Exists(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}BotSettings.json"))
                {
                    bSettings = new BotSettings(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}BotSettings.json");
                }
                else
                {
                    if (!Directory.Exists(Constants._WORKDIR_ + $"{Constants.slashType}Data"))
                    {
                        Directory.CreateDirectory(Constants._WORKDIR_ + $"{Constants.slashType}Data");
                    }
                    using (var response = await dbClient.Files.DownloadAsync("/Data/BotSettings.json"))
                    {
                        var f = File.Create(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}BotSettings.json");
                        using (var rw = new StreamWriter(f))
                        {
                            Console.WriteLine($"Creating file {Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}BotSettings.json"}");
                            rw.Write(await response.GetContentAsStringAsync());
                        }
                    }
                    bSettings = new BotSettings(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}BotSettings.json");
                }

                if (File.Exists(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}DBSettings.json"))
                {
                    dbSettings = new DBSettings(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}DBSettings.json");
                }
                else
                {
                    if (!Directory.Exists(Constants._WORKDIR_ + $"{Constants.slashType}Data"))
                    {
                        Directory.CreateDirectory(Constants._WORKDIR_ + $"{Constants.slashType}Data");
                    }
                    using (var response = await dbClient.Files.DownloadAsync("/Data/DBSettings.json"))
                    {
                        var f = File.Create(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}DBSettings.json");
                        using (var rw = new StreamWriter(f))
                        {
                            Console.WriteLine($"Creating file {Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}DBSettings.json"}");
                            rw.Write(await response.GetContentAsStringAsync());
                        }
                    }
                    dbSettings = new DBSettings(Constants._WORKDIR_ + $"{Constants.slashType}Data{Constants.slashType}DBSettings.json");
                }
            }
            var r = new Result();

            r.botSettings = bSettings;
            r.dbSettings  = dbSettings;
            return(r);
        }
Exemplo n.º 2
0
 public DBSettings(string jsonFileLoc)
 {
     using (StreamReader r = new StreamReader(jsonFileLoc))
     {
         string     json = r.ReadToEnd();
         DBSettings d    = JsonConvert.DeserializeObject <DBSettings>(json);
         this.password = d.password;
         this.port     = d.port;
         this.host     = d.host;
         this.db       = d.db;
         this.username = d.username;
         this.instance = d.instance;
     }
 }
Exemplo n.º 3
0
        private async Task MainAsync()
        {
            var res = Setup.GetFiles(bSettings, dbSettings);

            res.Wait();
            bSettings  = res.Result.botSettings;
            dbSettings = res.Result.dbSettings;

            Client = new DiscordSocketClient(new DiscordSocketConfig
            {
                LogLevel = LogSeverity.Debug
            });

            Commands = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
                DefaultRunMode        = RunMode.Async,
                LogLevel = LogSeverity.Debug
            });

            Provider = new ServiceCollection()
                       .AddSingleton(Client)
                       .AddSingleton(Commands)
                       .BuildServiceProvider();

            Client.MessageReceived += Client_MessageReceived;
            await Commands.AddModulesAsync(Assembly.GetEntryAssembly(), null);

            Client.Ready           += Client_Ready;
            Client.Log             += Client_Log;
            Client.JoinedGuild     += Client_JoinedGuild;
            Client.LeftGuild       += Client_LeftGuild;
            Client.ReactionAdded   += Client_ReactionAdded;
            Client.ReactionRemoved += Client_ReactionRemoved;

            if (!Directory.Exists(LogWriter.LogFileLoc.Replace($"Logs{Constants.slashType}Log", $"Logs{Constants.slashType}")))
            {
                Directory.CreateDirectory(LogWriter.LogFileLoc.Replace($"Logs{Constants.slashType}Log", $"Logs{Constants.slashType}"));
            }
            DBControl.dbSettings = dbSettings;

            await Client.LoginAsync(TokenType.Bot, bSettings.token);

            await Client.StartAsync();

            Timer t = new Timer();

            t.AutoReset = true;
            async void handler(object sender, ElapsedEventArgs e)
            {
                List <Poll> tempList = GlobalVars.Polls;

                foreach (Poll p in tempList)
                {
                    await GlobalVars.Polls.Single(i => i == p).Update();
                }
            }

            t.StartTimer(handler, 5000);

            GlobalVars.CurrencyList = await Setup.GetCurrencies(Constants._CURRENCYLAYER_);

            GlobalVars.CurrencyLastUpdated = DateTime.Now;

            Timer CurT = new Timer();

            CurT.AutoReset = true;
            async void CurHandler(object sender, ElapsedEventArgs e)
            {
                GlobalVars.CurrencyList = await Setup.GetCurrencies(Constants._CURRENCYLAYER_);

                GlobalVars.CurrencyLastUpdated = DateTime.Now;
            }

            CurT.StartTimer(CurHandler, 86400000);

            GlobalVars.GameObj = new MortyGame(dbSettings);
            GlobalVars.Client  = Client;

            await Task.Delay(-1);
        }