<commit_before>﻿using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NoAdsHere.Common;
using NoAdsHere.Database.Entities.Guild;
using NoAdsHere.Database.UnitOfWork;
using NoAdsHere.Services.FAQ;
using NoAdsHere.Services.Github;
using NoAdsHere.Services.LogService;
using NoAdsHere.Services.Penalties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NoAdsHere.Services.AntiAds;
using NoAdsHere.Services.Violations;

namespace NoAdsHere.Services.Events
{
    public static class EventHandlers
    {
        private static DiscordShardedClient _client;
        private static IUnitOfWork _unit;
        private static IConfigurationRoot _config;
        private static LogChannelService _channellogger;
        private static CommandHandler _handler;
        private static ILoggerFactory _factory;
        private static ILogger _logger;

        public static async Task StartServiceHandlers(IServiceProvider provider)
        {
            _client = provider.GetService<DiscordShardedClient>();
            _channellogger = provider.GetService<LogChannelService>();
            _config = provider.GetService<IConfigurationRoot>();
            _unit = provider.GetService<IUnitOfWork>();
            _factory = provider.GetService<ILoggerFactory>();
            _logger = _factory.CreateLogger(typeof(EventHandlers));
            var adsService = provider.GetService<AntiAdsService>();
            var cmdService = provider.GetService<CommandService>();
            var faqService = provider.GetService<FaqService>();
            var githubService = provider.GetService<GithubService>();

            await ReadyLogger();

            _handler = new CommandHandler(provider);
            await _handler.LoadModulesAndStartAsync();
            _logger.LogInformation(new EventId(200), "Created & Started CommandHandler");

            await faqService.LoadFaqsAsync();
            _logger.LogInformation(new EventId(200), "Installed and loaded FAQ Service");

            adsService.StartService();
            _logger.LogInformation(new EventId(200), "Started AntiAds Service");

            await JobQueue.Install(provider);
            _logger.LogInformation(new EventId(200), "Loaded JobQueue");

            await githubService.StartAsync();
            _logger.LogInformation(new EventId(200), "Started Github service");

            _client.Log += ClientLogger;
            cmdService.Log += CommandLogger;

            _client.JoinedGuild += JoinedGuild;
            _client.LeftGuild += LeftGuild;
        }

        public static async Task StopCommandHandlerAsync()
        {
            await _handler.StopHandler();
        }

        private static Task ReadyLogger()
        {
            foreach (var shard in _client.Shards.OrderBy(socketClient => socketClient.ShardId))
            {
                async Task Handler()
                {
                    _logger.LogInformation(new EventId(100), $"Shard {shard.ShardId} Ready");
                    await _channellogger.LogMessageAsync(_client, shard, Emote.Parse("<:Science:330479610812956672>"),
                        $"Ready `G:{shard.Guilds.Count}, U:{shard.Guilds.Sum(g => g.Users.Count)}`");
                    shard.Ready -= Handler;
                }
                shard.Ready += Handler;
            }

            return Task.CompletedTask;
        }

        private static Task ClientLogger(LogMessage message)
        {
            LogDiscord(message);
            return Task.CompletedTask;
        }<commit_msg>Fixed Ready Event to get full MemberCount<commit_after>﻿using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NoAdsHere.Common;
using NoAdsHere.Database.Entities.Guild;
using NoAdsHere.Database.UnitOfWork;
using NoAdsHere.Services.FAQ;
using NoAdsHere.Services.Github;
using NoAdsHere.Services.LogService;
using NoAdsHere.Services.Penalties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NoAdsHere.Services.AntiAds;
using NoAdsHere.Services.Violations;

namespace NoAdsHere.Services.Events
{
    public static class EventHandlers
    {
        private static DiscordShardedClient _client;
        private static IUnitOfWork _unit;
        private static IConfigurationRoot _config;
        private static LogChannelService _channellogger;
        private static CommandHandler _handler;
        private static ILoggerFactory _factory;
        private static ILogger _logger;

        public static async Task StartServiceHandlers(IServiceProvider provider)
        {
            _client = provider.GetService<DiscordShardedClient>();
            _channellogger = provider.GetService<LogChannelService>();
            _config = provider.GetService<IConfigurationRoot>();
            _unit = provider.GetService<IUnitOfWork>();
            _factory = provider.GetService<ILoggerFactory>();
            _logger = _factory.CreateLogger(typeof(EventHandlers));
            var adsService = provider.GetService<AntiAdsService>();
            var cmdService = provider.GetService<CommandService>();
            var faqService = provider.GetService<FaqService>();
            var githubService = provider.GetService<GithubService>();

            await ReadyLogger();

            _handler = new CommandHandler(provider);
            await _handler.LoadModulesAndStartAsync();
            _logger.LogInformation(new EventId(200), "Created & Started CommandHandler");

            await faqService.LoadFaqsAsync();
            _logger.LogInformation(new EventId(200), "Installed and loaded FAQ Service");

            adsService.StartService();
            _logger.LogInformation(new EventId(200), "Started AntiAds Service");

            await JobQueue.Install(provider);
            _logger.LogInformation(new EventId(200), "Loaded JobQueue");

            await githubService.StartAsync();
            _logger.LogInformation(new EventId(200), "Started Github service");

            _client.Log += ClientLogger;
            cmdService.Log += CommandLogger;

            _client.JoinedGuild += JoinedGuild;
            _client.LeftGuild += LeftGuild;
        }

        public static async Task StopCommandHandlerAsync()
        {
            await _handler.StopHandler();
        }

        private static Task ReadyLogger()
        {
            foreach (var shard in _client.Shards.OrderBy(socketClient => socketClient.ShardId))
            {
                async Task Handler()
                {
                    _logger.LogInformation(new EventId(100), $"Shard {shard.ShardId} Ready");
                    await _channellogger.LogMessageAsync(_client, shard, Emote.Parse("<:Science:330479610812956672>"),
                        $"Ready `G:{shard.Guilds.Count}, U:{shard.Guilds.Sum(g => g.MemberCount)}`");
                    shard.Ready -= Handler;
                }
                shard.Ready += Handler;
            }

            return Task.CompletedTask;
        }

        private static Task ClientLogger(LogMessage message)
        {
            LogDiscord(message);
            return Task.CompletedTask;
        }
{'fraction_non_alphanumeric': 0.08739563695125235, 'fraction_numerical': 0.009695663883652034, 'mean_word_length': 2.61587147030185, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1999633', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1907, 'n_words': 400}