internal IMessageChannel GetAlertChannel(IGuild guild)
        {
            SocketTextChannel channel = guild.GetChannelAsync(SqliteDbHandler.GetAlertChannelId(guild)).GetAwaiter().GetResult() as SocketTextChannel;

            return(channel != null? channel : GetCommandChannel(guild));
            //return guild.GetDefaultChannelAsync().GetAwaiter().GetResult() as SocketChannel;
        }
Beispiel #2
0
        public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider map)
        {
            var service = map.GetService <RemindMeService>();

            if (service != null)
            {
                //if (CheckAllowCommands(service, context))
                //{
                try
                {
                    if (SqliteDbHandler.GetAdminRoles(context.Guild).Any(x => ((SocketGuildUser)context.User).Roles.Select(y => y.Id).Contains(x)))
                    {
                        return(Task.FromResult(PreconditionResult.FromSuccess()));
                    }
                    else if (new RequireOwnerAttribute().CheckPermissionsAsync(context, command, map).GetAwaiter().GetResult().IsSuccess || new RequireUserPermissionAttribute(GuildPermission.Administrator).CheckPermissionsAsync(context, command, map).GetAwaiter().GetResult().IsSuccess)
                    {
                        return(Task.FromResult(PreconditionResult.FromSuccess()));
                    }
                    return(Task.FromResult(PreconditionResult.FromError("This command can only be used by user with admin role, server administrator or bot owner.")));
                }
                catch (SqliteException e)
                {
                    context.Message.AddReactionAsync(new Emoji("❌"));
                    return(Task.FromResult(PreconditionResult.FromError(e.Message)));
                }
                //}
                //return Task.FromResult(PreconditionResult.FromError("Managing music via commands is disabled in this guild."));
            }

            return(Task.FromResult(PreconditionResult.FromError("No RemindMeService found.")));
        }
        internal List <SocketRole> GetAdminRoles(IGuild guild)
        {
            List <SocketRole> roles = new List <SocketRole>();

            foreach (ulong roleId in SqliteDbHandler.GetAdminRoles(guild))
            {
                roles.Add(guild.Roles.Where(x => x.Id == roleId).FirstOrDefault() as SocketRole);
            }
            return(roles);
        }
        public override Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider map)
        {
            var service = map.GetService <RemindMeService>();

            if (service != null)
            {
                //if (CheckAllowCommands(service, context))
                //{

                //TODO: Check if channel is set and do action(?)
                return(SqliteDbHandler.GetCommandChannelId(context.Guild) == context.Channel.Id
                    ? Task.FromResult(PreconditionResult.FromSuccess())
                    : Task.FromResult(PreconditionResult.FromError("This command can only be used in command channel.")));
                //}
                //return Task.FromResult(PreconditionResult.FromError("Managing music via commands is disabled in this guild."));
            }

            return(Task.FromResult(PreconditionResult.FromError("No RemindMeService found.")));
        }
        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);
        }
 private async Task LeftGuild(SocketGuild guild)
 {
     await SqliteDbHandler.DeleteGuildConfig(guild);
 }
 private async Task JoinedGuild(SocketGuild guild)
 {
     await SqliteDbHandler.CreateDefaultGuildConfig(guild);
 }
 internal async Task AddTask(IGuild guild, IUser assignedUser, string name, string description, DateTime deadline)
 {
     await SqliteDbHandler.CreateTask(guild.Id, assignedUser.Id, name, description, DateTime.Now, deadline);
 }
 internal async Task SetCommandChannel(IGuild guild, IMessageChannel channel)
 {
     await SqliteDbHandler.SetCommandChannelId(guild, channel);
 }
 internal async Task RemoveAdminRoles(IGuild guild, IRole[] roles)
 {
     await SqliteDbHandler.RemoveAdminRoles(guild, roles);
 }