示例#1
0
        public static async Task Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Async(a => a.RollingFile("rollinglog.log", fileSizeLimitBytes: 50000000).MinimumLevel.Information())
                         .WriteTo.LiterateConsole()
                         .MinimumLevel.Debug()
                         .CreateLogger();

            if (args.Length != 0 && (args[0].Contains("--silent") || args[0].Contains("-s")))
            {
                _silentStartup = true;
            }
            else
            {
                Log.Information("Are you sure you shouldn't be using the --silent argument?");
            }

            Client = new DiscordSocketClient(new DiscordSocketConfig
            {
                LogLevel            = LogSeverity.Debug,
                MessageCacheSize    = 0,
                AlwaysDownloadUsers = true,
                HandlerTimeout      = 2000
            });

            _settings = File.Exists(SettingsPath)
                ? JsonConvert.DeserializeObject <BotSettings>(File.ReadAllText(SettingsPath))
                : new BotSettings
            {
                CommandPrefix            = '!',
                DiscordEmail             = "email",
                DiscordPassword          = "******",
                DiscordToken             = "Token",
                GiantBombApiKey          = "GbAPIKey",
                YoutubeApiKey            = "",
                DatabaseConnectionString = "",
                OwnerId          = 0,
                MarkovChainStart = false,
                MarkovChainDepth = 2,
                GiantBombLiveStreamRefreshRate = 60000,
                GiantBombVideoRefreshRate      = 60000
            };

            _rankConfigs = File.Exists(RankConfigPath)
                ? JsonConvert.DeserializeObject <RankConfigs>(File.ReadAllText(RankConfigPath))
                : new RankConfigs
            {
                GuildConfigs = new Dictionary <ulong, GuildRanks>()
            };

            _dbFactory = new DiscordContextFactory();

            _kiteChat = new KiteChat(Client, _dbFactory,
                                     _settings.MarkovChainStart,
                                     _settings.GiantBombApiKey,
                                     _settings.YoutubeApiKey,
                                     _settings.GiantBombLiveStreamRefreshRate,
                                     _silentStartup,
                                     _settings.GiantBombVideoRefreshRate,
                                     _settings.MarkovChainDepth);

            _commandService = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
                DefaultRunMode        = RunMode.Sync,
                LogLevel      = LogSeverity.Verbose,
                SeparatorChar = ' ',
                ThrowOnError  = true //Throws exceptions up to the commandhandler in sync commands
            });

            Client.Log          += LogDiscordMessage;
            _commandService.Log += LogDiscordMessage;

            Client.MessageReceived += (msg) =>
            {
                Log.Verbose("MESSAGE {Channel}{tab}{User}: {Content}", msg.Channel.Name, "\t", msg.Author.Username,
                            msg.ToString());
                return(Task.CompletedTask);
            };

            Client.GuildMembersDownloaded += async(guild) =>
            {
                var sw = new Stopwatch();
                sw.Start();
                await _dbFactory.SyncGuild(guild).ConfigureAwait(false);

                sw.Stop();
            };

            Client.JoinedGuild += (server) =>
            {
                Log.Information("Connected to {Name}", server.Name);
                return(Task.CompletedTask);
            };

            Client.UserUnbanned += (user, guild) =>
            {
                Console.WriteLine($"{user.Username}#{user.Discriminator}");
                Console.WriteLine(user);
                return(Task.CompletedTask);
            };

            int connections = 0;

            Client.Connected += () =>
            {
                Console.WriteLine($"Connected for the {connections++} time.");
                return(Task.CompletedTask);
            };

            int disconnections = 0;

            Client.Disconnected += (exception) =>
            {
                Console.WriteLine($"Disconnected for the {disconnections++} time.");
                return(Task.CompletedTask);
            };

            Console.WriteLine("LoginAsync");
            await Client.LoginAsync(TokenType.Bot, _settings.DiscordToken).ConfigureAwait(false);

            Console.WriteLine("ConnectAsync");
            await Client.StartAsync().ConfigureAwait(false);

            Client.Ready += OnReady;

            Client.UserUpdated += async(before, after) => await CheckUsername(before, after).ConfigureAwait(false);

            Client.GuildMemberUpdated += async(before, after) => await CheckNickname(before, after).ConfigureAwait(false);

            await Task.Delay(-1).ConfigureAwait(false);
        }