예제 #1
0
        public MultiTextMarkovChainHelper(DiscordSocketClient client, DiscordContextFactory dbFactory, int depth)
        {
            _semaphore = new SemaphoreSlim(0, 1);
            _dbFactory = dbFactory;
            _db        = _dbFactory.Create(new DbContextFactoryOptions());
            _client    = client;
            Depth      = depth;

            if (depth > 0)
            {
                _markovChain = new StringMarkov(depth);
            }

            _timer = new Timer(async e => await SaveAsync().ConfigureAwait(false), null, 600000, 600000);
        }
예제 #2
0
        private static async Task OnReady()
        {
            try
            {
                if (_isFirstTime)
                {
                    using (var db = _dbFactory.Create())
                    {
                        var serviceProvider = db.GetInfrastructure();
                        var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();
                        loggerFactory.AddProvider(new MyLoggerProvider());
                    }

                    var services = new ServiceCollection();
                    _handler = new CommandHandler();
                    services.AddSingleton(Client);
                    services.AddSingleton(_settings);
                    services.AddSingleton(new RankService(_rankConfigs, configs => File.WriteAllText(RankConfigPath, JsonConvert.SerializeObject(_rankConfigs, Formatting.Indented)), _dbFactory, Client));
                    services.AddSingleton(_kiteChat);
                    services.AddSingleton(_handler);
                    services.AddEntityFrameworkNpgsql()
                    .AddDbContext <KiteBotDbContext>(options => options.UseNpgsql(_settings.DatabaseConnectionString));
                    services.AddSingleton(new VideoService(_settings.GiantBombApiKey));
                    services.AddSingleton(new SearchHelper(_settings.AnilistId, _settings.AnilistSecret));
                    services.AddSingleton(new ReminderService(Client));
                    services.AddSingleton(new FollowUpService());
                    services.AddSingleton(new Random());
                    services.AddSingleton(new CryptoRandom());

                    await _handler.InstallAsync(_commandService, services.BuildServiceProvider()).ConfigureAwait(false);

                    var _ = TryRun(async() =>
                    {
                        var sw = new Stopwatch();
                        sw.Start();
                        await _kiteChat.InitializeMarkovChainAsync().ConfigureAwait(false);
                        sw.Stop();
                        Log.Information("Initialize Markov Chain: Done,({sw} ms)", sw.ElapsedMilliseconds);
                    });
                    _isFirstTime = false;
                }
                Log.Information("Ready: Done");
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Exception in Ready Event");
            }
        }
        internal static async Task SyncGuild(this DiscordContextFactory dbFactory, SocketGuild socketGuild)
        {
            using (var dbContext = dbFactory.Create(new DbContextFactoryOptions()))
            {
                Guild guild = await dbContext.Guilds
                              .Include(g => g.Channels)
                              .Include(g => g.Users)
                              .SingleOrDefaultAsync(x => x.Id == socketGuild.Id).ConfigureAwait(false);

                //If guild does not exist, we create a new one and populate it with Users and Channels
                if (guild == null)
                {
                    guild = new Guild
                    {
                        Id       = socketGuild.Id,
                        Channels = new List <Channel>(),
                        Name     = socketGuild.Name,
                        Users    = new List <User>()
                    };

                    foreach (var textChannel in socketGuild.TextChannels)
                    {
                        Channel channel = new Channel
                        {
                            Id       = textChannel.Id,
                            Guild    = guild,
                            Name     = textChannel.Name,
                            Messages = new List <Message>()
                        };
                        guild.Channels.Add(channel);
                    }

                    foreach (var socketUser in socketGuild.Users)
                    //For now, Users are unique for each guild, this will cause me problems later I'm sure
                    {
                        User user = new User
                        {
                            Id             = socketUser.Id,
                            Guild          = guild,
                            LastActivityAt = DateTimeOffset.UtcNow,
                            JoinedAt       = socketUser.JoinedAt,
                            Messages       = new List <Message>(),
                            Name           = socketUser.Username
                        };
                        guild.Users.Add(user);
                    }
                    dbContext.Add(guild);
                }
                else
                {
                    //This should also probably track when channels no longer exist, but its probably not a big deal right now

                    var channelsNotTracked = socketGuild.TextChannels.Where(x => guild.Channels.All(y => y.Id != x.Id));
                    var socketTextChannels = channelsNotTracked as IList <SocketTextChannel> ??
                                             channelsNotTracked.ToList();
                    if (socketTextChannels.Any())
                    {
                        foreach (var channelToTrack in socketTextChannels)
                        {
                            Channel channel = new Channel
                            {
                                Guild    = guild,
                                Id       = channelToTrack.Id,
                                Messages = new List <Message>(),
                                Name     = channelToTrack.Name
                            };
                            guild.Channels.Add(channel);
                        }
                        dbContext.Update(guild);
                    }

                    var usersNotTracked  = socketGuild.Users.Where(x => guild.Users.All(y => y.Id != x.Id));
                    var socketGuildUsers = usersNotTracked as IList <SocketGuildUser> ?? usersNotTracked.ToList();
                    if (socketGuildUsers.Any())
                    {
                        foreach (var userToTrack in socketGuildUsers)
                        {
                            User user = new User
                            {
                                Id             = userToTrack.Id,
                                Guild          = guild,
                                LastActivityAt = DateTimeOffset.UtcNow,
                                JoinedAt       = userToTrack.JoinedAt,
                                Messages       = new List <Message>(),
                                Name           = userToTrack.Username
                            };
                            guild.Users.Add(user);
                        }
                        dbContext.Update(guild);
                    }
                }
                await dbContext.SaveChangesAsync().ConfigureAwait(false); //I could move this inside the branches, but its relatively cheap to call this if nothing has changed, and avoids multiple calls to it
            }
        }