コード例 #1
0
ファイル: Program.cs プロジェクト: Dimi1010/DiscordVerifyBot
        public async Task MainAsync()
        {
            await _commandHandler.SetupAsync();

            //Fetches the Bot Token From File
            string Bot_Token = "";

            using (var DH = new SettingsDataHandler())
            {
                Bot_Token = DH.GetSettings().BotToken;
            }

            //Awaits Bot Login / Start Confirmation
            await _client.LoginAsync(TokenType.Bot, Bot_Token);

            await _client.StartAsync();

            _quitEvent.WaitOne();

            Log.Logger.Information(
                "User initiated shutdown.");

            await _client.LogoutAsync();

            await _client.StopAsync();

            await Task.Delay(millisecondsDelay : 1000);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Dimi1010/DiscordVerifyBot
        public Program()
        {
            Console.CancelKeyPress += (sender, eArgs) =>
            {
                _quitEvent.Set();
                eArgs.Cancel = true;
            };

            //Creates and/or Updates the database when the program is strated
            using (var DbContext = new SQLiteDatabaseContext())
            {
                DbContext.Database.Migrate();
                //int maxRetries = 3;
                //for(int i = 0; i < maxRetries; i++)
                //{
                //    try
                //    {
                //        DbContext.Database.Migrate();
                //        break;
                //    }
                //    catch (System.NotSupportedException e)
                //    {
                //        DbContext.Database.EnsureDeleted();
                //    }
                //}
            }

            Settings settings;

            using (var DH = new SettingsDataHandler())
            {
                settings = DH.GetSettings();

                #region Logger Creation

                //Gets and converts Log Levels to match between Serilog and integrated Discord.Net Logger
                var LogLevelSerilog = Serilog.Events.LogEventLevel.Information;
                var LogLevelDiscord = LogSeverity.Info;

                if (Enum.IsDefined(typeof(Serilog.Events.LogEventLevel), settings.LogLevel))
                {
                    LogLevelSerilog = (Serilog.Events.LogEventLevel)settings.LogLevel;
                    new LogLevelConverter().SerilogToDiscordNet(LogLevelSerilog, out LogLevelDiscord);
                }

                LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch()
                {
                    MinimumLevel = LogLevelSerilog
                };

                var loggerConfiguration = new LoggerConfiguration()
                                          .MinimumLevel.Is(LogLevelSerilog)
                                          .Enrich.With(new ThreadIdEnricher())
                                          .Enrich.With(new ProcessIdEnricher())
                                          .WriteTo.Console(
                    outputTemplate: "{Timestamp:HH:mm} [{Level}] [{ProcessId}-{ThreadId}] {Message}{NewLine}{Exception}");
                if (Convert.ToBoolean(settings.RollingLogRetainedFiles))
                {
                    loggerConfiguration.WriteTo.File(
                        path: DH.GetLogFilePath(),
                        rollingInterval: RollingInterval.Day,
                        retainedFileCountLimit: settings.RollingLogRetainedFiles,
                        outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {Message}{NewLine}{Exception}");
                }

                Log.Logger = loggerConfiguration.CreateLogger();
                Log.Debug("Logger Created");

                #endregion

                _client = new DiscordSocketClient(new DiscordSocketConfig
                {
                    LogLevel = LogLevelDiscord
                });

                _commandService = new CommandService(new CommandServiceConfig
                {
                    CaseSensitiveCommands = false,
                    DefaultRunMode        = RunMode.Async,
                    LogLevel = LogLevelDiscord
                });
            }

            _serviceProvider = new ServiceProviderFactory(_client, _commandService).Build();

            _commandHandler = new CommandHandler(
                client: _client,
                commandService: _commandService,
                serviceProvider: _serviceProvider,
                replyService: _serviceProvider.GetRequiredService <IReplyService>()
                );

            _client.Log   += OnClientLogAsync;
            _client.Ready += OnClientReadyAsync;
        }