public static void RemoveTimeLimitedInfractionReference(UserModerationLog usermodlog)
 {
     lock (removelistlock)
     {
         toBeRemoved.Add(usermodlog);
     }
 }
 public static void AddTimeLimitedInfractionReference(UserModerationLog usermodlog)
 {
     lock (addlistlock)
     {
         toBeAdded.Add(usermodlog);
     }
 }
 public UserModerationLog GetOrCreateUserModerationLog(ulong userId)
 {
     if (!userLogs.TryGetValue(userId, out UserModerationLog usermoderationLog))
     {
         usermoderationLog = new UserModerationLog(this, userId);
         userLogs.Add(userId, usermoderationLog);
     }
     return(usermoderationLog);
 }
        private static async Task LoadGuildModerationLog(ulong guildId)
        {
            GuildModerationLog guildModerationLog = new GuildModerationLog(guildId);

            if (Directory.Exists(guildModerationLog.UserDirectory))
            {
                foreach (string filepath in Directory.EnumerateFiles(guildModerationLog.UserDirectory, "*.json"))
                {
                    LoadFileOperation load = await ResourcesModel.LoadToJSONObject(filepath);

                    if (load.Success)
                    {
                        UserModerationLog userModerationLog = new UserModerationLog(guildModerationLog);
                        if (userModerationLog.FromJSON(load.Result))
                        {
                            guildModerationLog.userLogs.Add(userModerationLog.UserId, userModerationLog);
                            if (userModerationLog.IsBanned)
                            {
                                if (userModerationLog.BannedUntil.Value < DateTimeOffset.MaxValue)
                                {
                                    AddTimeLimitedInfractionReference(userModerationLog);
                                }
                            }
                            else if (userModerationLog.IsMuted)
                            {
                                if (userModerationLog.MutedUntil.Value < DateTimeOffset.MaxValue)
                                {
                                    AddTimeLimitedInfractionReference(userModerationLog);
                                }
                            }
                        }
                    }
                }
            }
            if (Directory.Exists(guildModerationLog.ChannelDirectory))
            {
                foreach (string filepath in Directory.EnumerateFiles(guildModerationLog.ChannelDirectory, "*.json"))
                {
                    LoadFileOperation load = await ResourcesModel.LoadToJSONObject(filepath);

                    if (load.Success)
                    {
                        ChannelModerationLog channelModerationLog = new ChannelModerationLog(guildModerationLog);
                        if (channelModerationLog.FromJSON(load.Result))
                        {
                            guildModerationLog.channelLogs.Add(channelModerationLog.ChannelId, channelModerationLog);
                        }
                    }
                }
            }
            GuildLogs.Add(guildModerationLog.GuildId, guildModerationLog);
        }
        private static async Task <string> UnbanUser(UserModerationLog userModLog)
        {
            GuildModerationLog guildLog = userModLog.Parent;
            SocketGuild        guild    = BotCore.Client.GetGuild(guildLog.GuildId);

            if (guild != null)
            {
                await userModLog.UnBan(guild);

                return(null);
            }
            else
            {
                return($"Failed to unban `{userModLog.UserId}` - Guild `{userModLog.Parent.GuildId}` not found!");
            }
        }
        internal async Task InvokeUserModerationLogModifiedEvents(UserModerationLog userLog, UserModerationEntry entry)
        {
            await TryInvokeUserModerationLogModifiedEvent(OnUserModerationLogEntryAdded, userLog, entry);

            switch (entry.Type)
            {
            case ModerationType.Note:
                await TryInvokeUserModerationLogModifiedEvent(OnUserNote, userLog, entry);

                break;

            case ModerationType.Warning:
                await TryInvokeUserModerationLogModifiedEvent(OnUserWarning, userLog, entry);

                break;

            case ModerationType.Muted:
                await TryInvokeUserModerationLogModifiedEvent(OnUserMuted, userLog, entry);

                break;

            case ModerationType.UnMuted:
                await TryInvokeUserModerationLogModifiedEvent(OnUserUnMuted, userLog, entry);

                break;

            case ModerationType.Kicked:
                await TryInvokeUserModerationLogModifiedEvent(OnUserKicked, userLog, entry);

                break;

            case ModerationType.Banned:
                await TryInvokeUserModerationLogModifiedEvent(OnUserBanned, userLog, entry);

                break;

            case ModerationType.UnBanned:
                await TryInvokeUserModerationLogModifiedEvent(OnUserUnBanned, userLog, entry);

                break;
            }
        }
        private static async Task <string> UnMuteUser(UserModerationLog userModLog)
        {
            GuildModerationLog guildLog = userModLog.Parent;
            SocketGuild        guild    = BotCore.Client.GetGuild(guildLog.GuildId);

            if (guild is null)
            {
                return($"Failed to unmute `{userModLog.UserId}` - Guild `{userModLog.Parent.GuildId}` not found!");
            }
            SocketGuildUser user = guild.GetUser(userModLog.UserId);

            if (user is null)
            {
                return($"Failed to unmute `{userModLog.UserId}` - User not found!");
            }

            await userModLog.RemoveMute(user);

            return(null);
        }
        public async Task <string> RelieveInfraction()
        {
            SocketGuild guild = BotCore.Client.GetGuild(GuildId);

            if (guild != null)
            {
                SocketGuildUser user = guild.GetUser(UserId);

                GuildModerationLog guildLog = GuildModerationLog.GetOrCreateGuildModerationLog(GuildId);
                UserModerationLog  userLog  = guildLog.GetOrCreateUserModerationLog(UserId);

                if (Type == ModerationType.Banned)
                {
                    if (userLog.BannedUntil.HasValue)
                    {
                        if (userLog.BannedUntil.Value == Ends)
                        {
                            await guild.RemoveBanAsync(UserId);

                            return(null);
                        }
                    }
                    return("Banstate Invalid!");
                }

                if (Type == ModerationType.Muted)
                {
                }

                return($"Unhandled timelimited moderation type `{Type}`");
            }
            else
            {
                return($"Guild `{GuildId}` not found!");
            }
        }
 private async Task TryInvokeUserModerationLogModifiedEvent(OnUserModerationLogModifiedDelegate eventDelegates, UserModerationLog userLog, UserModerationEntry entry)
 {
     if (eventDelegates != null)
     {
         foreach (Delegate del in eventDelegates.GetInvocationList())
         {
             OnUserModerationLogModifiedDelegate deleg = del as OnUserModerationLogModifiedDelegate;
             if (deleg != null)
             {
                 try
                 {
                     await deleg(this, userLog, entry);
                 }
                 catch (Exception e)
                 {
                     await YNBBotCore.Logger(new LogMessage(LogSeverity.Error, "GML", "A user moderation log handler threw an unhandled exception", e));
                 }
             }
         }
     }
 }