Example #1
0
        public static async Task Main()
        {
            _config = new DiscordSocketConfig
            {
                LogLevel            = LogSeverity.Info,
                AlwaysDownloadUsers = true,
                MessageCacheSize    = 250,
                TotalShards         = 1
            };
            _client  = new DiscordShardedClient(_config);
            _command = new CommandHandler();

            ConfigHandler.DirectoryCheck();
            ConfigHandler.Config = await ConfigHandler.LoadConfigAsync();

            GuildHandler.GuildConfigs = await GuildHandler.LoadServerConfigsAsync <GuildModel>();

            BlacklistHandler.BlacklistConfigs = await BlacklistHandler.LoadBlacklistAsync <BlacklistModel>();

            string token;

            switch (ConfigHandler.Config.ReleaseEnv)
            {
            case "dev":
            case "development":
                token = ConfigHandler.Config.DevToken;
                break;

            case "prod":
            case "production":
                token = ConfigHandler.Config.Token;
                break;

            default:
                Console.WriteLine("ReleaseEnv can only be production or development");
                Environment.Exit(0);
                return;
            }

            await _client.LoginAsync(TokenType.Bot, token);

            await _client.StartAsync();

            foreach (var shard in _client.Shards)
            {
                shard.Ready += async() => await shard.SetGameAsync(PlayingStatus.Replace("{username}", shard.CurrentUser.Username));
            }

            _client.UserJoined     += Events.UserJoinedAsync;
            _client.UserLeft       += Events.UserLeftAsync;
            _client.GuildAvailable += Events.HandleGuildConfigAsync;
            _client.LeftGuild      += async guild => await Events.LeftGuildAsync(guild);

            _client.JoinedGuild += async guild => await Events.JoinedGuildAsync(guild);

            _client.UserBanned += Events.BannedUserAsync;

            await _command.Install(_client);

            _client.Log += Log;

            if (ConsolePresent)
            {
                var cki = Console.ReadKey(true);
                if (cki.Key == ConsoleKey.Escape || cki.Key == ConsoleKey.X)
                {
                    await _client.SetGameAsync("");

                    await _client.SetStatusAsync(UserStatus.Invisible);

                    await _client.LogoutAsync();

                    await _client.StopAsync();

                    Environment.Exit(0);
                }
            }

            await Task.Delay(-1);
        }