Esempio n. 1
0
 private async Task LeftGuild(SocketGuild guild)
 {
     await SqliteDbHandler.DeleteGuildConfig(guild);
 }
Esempio n. 2
0
        private Task Client_Ready()
        {
            List <GuildConfigEntity> guilds = SqliteDbHandler.GetAllGuilds();

            //delete old guilds from database(guilds that bot is no longer in)
            guilds.Except(guilds.Where(a => Client.Guilds.Any(b => b.Id == a.GuildId))).Select(x => x.GuildId).ToList().ForEach(y => SqliteDbHandler.DeleteGuildConfig(y).GetAwaiter().GetResult());

            //create record in database for guilds that bot joined while being offline
            Client.Guilds.Except(Client.Guilds.Where(a => guilds.Any(b => b.GuildId == a.Id))).ToList().ForEach(c => JoinedGuild(c).GetAwaiter().GetResult());
            tasks = SqliteDbHandler.GetAllTasks();

            //check if there are some old underway tasks in database, process them
            //TODO: optimalization- pull every guild and user once from databse instead of doing it in every iteration
            List <TaskEntity> uncompletedTasks = tasks.Where(x => x.DeadlineDate.CompareTo(DateTime.Now) <= 0).Where(x => x.CompletionStatus == 0).ToList();//deadline is over, task uncompleted

            uncompletedTasks.Select(x => new { x.GuildId, x.AssignedUserId }).Distinct().ToList().ForEach(y => {
                IGuild guild = Client.GetGuild(y.GuildId);
                if (guild != null)
                {
                    SocketUser user = Client.GetUser(y.AssignedUserId);
                    if (user != null)
                    {
                        EmbedBuilder builder = new EmbedBuilder();
                        uncompletedTasks.Where(x => x.AssignedUserId == user.Id && x.GuildId == guild.Id).ToList().ForEach(z => {
                            SqliteDbHandler.SetCompletionStatus(z, 2).GetAwaiter().GetResult();
                            builder.AddField(new EmbedFieldBuilder()
                            {
                                Name  = z.Name,
                                Value = $"{z.Description}\nDue to {z.DeadlineDate.Date.ToString("d")}\n"
                            });
                        });
                        builder.Color = Color.DarkRed;
                        GetAlertChannel(guild).SendMessageAsync($"{user.Mention} your tasks weren't completed on time:", embed: builder.Build());
                    }
                }
            });

            //start main loop
            new Timer(x => ReminderLoop(x), null, 0, timerInterval);
            return(Task.CompletedTask);
        }