예제 #1
0
        public async Task AddModerationEntry(UserModerationEntry entry)
        {
            moderationEntries.Add(entry);
            await Save();

            await Parent.InvokeUserModerationLogModifiedEvents(this, entry);
        }
예제 #2
0
        public async Task AddBan(UserModerationEntry entry, DateTimeOffset banUntil)
        {
            moderationEntries.Add(entry);
            BannedUntil = banUntil;
            MutedUntil  = null;
            if (banUntil < DateTimeOffset.MaxValue)
            {
                GuildModerationLog.AddTimeLimitedInfractionReference(this);
            }
            await Save();

            await Parent.InvokeUserModerationLogModifiedEvents(this, entry);
        }
예제 #3
0
        public async Task UnBan(SocketGuild guild, UserModerationEntry?entry = null)
        {
            BannedUntil = null;
            if (!entry.HasValue)
            {
                SocketGuildUser self = guild.GetUser(BotCore.Client.CurrentUser.Id);
                entry = new UserModerationEntry(Parent.GuildId, ModerationType.UnBanned, null, self, "Automatic Unban");
            }
            moderationEntries.Add(entry.Value);
            await Parent.InvokeUserModerationLogModifiedEvents(this, entry.Value);

            await Save();

            await guild.RemoveBanAsync(UserId);
        }
예제 #4
0
        public async Task RemoveMute(SocketGuildUser guildUser, UserModerationEntry?entry = null)
        {
            MutedUntil = null;
            if (!entry.HasValue)
            {
                SocketGuildUser self = guildUser.Guild.GetUser(BotCore.Client.CurrentUser.Id);
                entry = new UserModerationEntry(Parent.GuildId, ModerationType.UnMuted, null, self, "Automatic Unmute");
            }
            moderationEntries.Add(entry.Value);
            await Parent.InvokeUserModerationLogModifiedEvents(this, entry.Value);

            await Save();

            SocketRole muteRole = guildUser.Guild.GetRole(SettingsModel.MuteRole);

            if ((muteRole != null) && guildUser.Roles.Contains(role => { return(role.Id == muteRole.Id); }))
            {
                await guildUser.RemoveRoleAsync(muteRole);
            }
            if ((rolesPreMute != null) && rolesPreMute.Count > 0)
            {
                List <SocketRole> roles = new List <SocketRole>();
                foreach (ulong roleId in rolesPreMute)
                {
                    SocketRole role = guildUser.Guild.GetRole(roleId);
                    if (role != null && !guildUser.Roles.Any(existingRole => { return(existingRole.Id == roleId); }))
                    {
                        roles.Add(role);
                    }
                }
                if (roles.Count > 0)
                {
                    await guildUser.AddRolesAsync(roles);
                }
                rolesPreMute.Clear();
            }
        }
예제 #5
0
        public bool FromJSON(JSONContainer json)
        {
            if (json.TryGetField(JSON_USERID, out ulong id))
            {
                UserId = id;
            }
            else
            {
                return(false);
            }

            string timestamp_str;

            if (json.TryGetField(JSON_BANNEDUNTIL, out timestamp_str))
            {
                if (DateTimeOffset.TryParseExact(timestamp_str, "u", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset bannedUntil))
                {
                    BannedUntil = bannedUntil;
                }
                else
                {
                    BannedUntil = DateTimeOffset.MaxValue;
                }
            }
            else
            {
                BannedUntil = null;
            }
            if (json.TryGetField(JSON_MUTEDUNTIL, out timestamp_str))
            {
                if (DateTimeOffset.TryParseExact(timestamp_str, "u", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTimeOffset mutedUntil))
                {
                    MutedUntil = mutedUntil;
                }
                else
                {
                    MutedUntil = DateTimeOffset.MaxValue;
                }
                if (json.TryGetField(JSON_ROLEIDS, out JSONContainer roleArray))
                {
                    rolesPreMute = new List <ulong>();
                    if (roleArray.IsArray && roleArray.Array != null)
                    {
                        foreach (JSONField arrayField in roleArray.Array)
                        {
                            if (arrayField.IsNumber && !arrayField.IsFloat)
                            {
                                rolesPreMute.Add(arrayField.Unsigned_Int64);
                            }
                        }
                    }
                }
            }
            else
            {
                MutedUntil = null;
            }

            if (json.TryGetArrayField(JSON_MODENTRIES, out JSONContainer jsonModEntries))
            {
                if (jsonModEntries.Array != null)
                {
                    foreach (JSONField jsonModEntry in jsonModEntries.Array)
                    {
                        if (jsonModEntry.IsObject)
                        {
                            UserModerationEntry moderationEntry = new UserModerationEntry(Parent.GuildId);
                            if (moderationEntry.FromJSON(jsonModEntry.Container))
                            {
                                moderationEntries.Add(moderationEntry);
                            }
                        }
                    }
                }
            }

            return(BannedUntil != null || MutedUntil != null || moderationEntries.Count > 0);
        }
예제 #6
0
        public async Task AddMute(SocketGuildUser target, DateTimeOffset muteUntil, UserModerationEntry?entry = null)
        {
            // Generate UserModerationEntry

            if (!entry.HasValue)
            {
                SocketGuildUser self = target.Guild.GetUser(BotCore.Client.CurrentUser.Id);

                if (muteUntil == DateTimeOffset.MaxValue)
                {
                    entry = new UserModerationEntry(target.Guild.Id, ModerationType.Muted, DateTimeOffset.UtcNow, self, "Automatic Mute", "Duration: perma");
                }
                else
                {
                    entry = new UserModerationEntry(target.Guild.Id, ModerationType.Muted, DateTimeOffset.UtcNow, self, "Automatic Mute", "Duration: " + (muteUntil - DateTimeOffset.UtcNow).ToHumanTimeString());
                }
            }

            moderationEntries.Add(entry.Value);

            // Edit user roles

            List <SocketRole> roles = new List <SocketRole>(target.Roles.Count - 1);

            foreach (SocketRole role in target.Roles)
            {
                if (!role.IsEveryone)
                {
                    roles.Add(role);
                }
            }

            SocketRole muteRole = target.Guild.GetRole(SettingsModel.MuteRole);

            try
            {
                await target.RemoveRolesAsync(roles);

                if (muteRole != null)
                {
                    await target.AddRoleAsync(muteRole);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                throw;
#endif
            }

            // Edit UserModerationLog

            MutedUntil   = muteUntil;
            rolesPreMute = new List <ulong>(roles.Select(role => role.Id));
            if (muteUntil < DateTimeOffset.MaxValue)
            {
                GuildModerationLog.AddTimeLimitedInfractionReference(this);
            }

            // Save

            await Save();

            await Parent.InvokeUserModerationLogModifiedEvents(this, entry.Value);
        }
예제 #7
0
 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));
                 }
             }
         }
     }
 }
예제 #8
0
        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;
            }
        }