コード例 #1
0
        private void CheckTemporaryInfractions()
        {
            DateTime    now   = DateTime.UtcNow;
            SocketGuild guild = _client.GetGuild(_data.Configuration.GuildID);

            int resolvedCounter = 0;

            foreach (UserData user in _data.UserData.GetUsersWithTemporaryInfractions())
            {
                if (user.HasTemporaryInfraction(TemporaryInfractionType.TempBan))
                {
                    TemporaryInfraction infraction = user.TemporaryInfractions.First(t => t.Type == TemporaryInfractionType.TempBan);
                    if (infraction.Expire <= now)
                    {
                        guild.RemoveBanAsync(user.ID);

                        _ = _modLog.CreateEntry(ModerationLogEntry.New
                                                .WithActionType(ModerationActionType.Unban)
                                                .WithTarget(user.ID)
                                                .WithReason("Temporary ban timed out.")
                                                .WithTime(DateTimeOffset.Now)
                                                .WithModerator(_client.CurrentUser));

                        user.TemporaryInfractions.RemoveAll(i => i.Type == TemporaryInfractionType.TempBan);
                        resolvedCounter++;
                    }
                }
                if (user.HasTemporaryInfraction(TemporaryInfractionType.TempMute))
                {
                    TemporaryInfraction infraction = user.TemporaryInfractions.First(t => t.Type == TemporaryInfractionType.TempMute);
                    if (infraction.Expire <= now)
                    {
                        IRole mutedRole = guild.GetRole(_data.Configuration.MutedRoleID);

                        // If the user is no longer in the server, just remove the entry, but don't attempt to remove his role.
                        IGuildUser guildUser = guild.GetUser(user.ID);
                        guildUser?.RemoveRoleAsync(mutedRole);

                        user.Muted = false;
                        _data.SaveUserData();

                        ModerationLogEntry entry = ModerationLogEntry.New
                                                   .WithActionType(ModerationActionType.Unmute)
                                                   .WithReason("Temporary mute timed out.")
                                                   .WithTime(DateTimeOffset.Now)
                                                   .WithModerator(_client.CurrentUser);

                        if (guildUser != null)
                        {
                            entry = entry.WithTarget(guildUser);
                        }
                        else
                        {
                            entry = entry.WithTarget(user.ID);
                        }

                        _ = _modLog.CreateEntry(entry);

                        user.TemporaryInfractions.RemoveAll(i => i.Type == TemporaryInfractionType.TempMute);
                        resolvedCounter++;
                    }
                }
            }

            if (resolvedCounter > 0)
            {
                _log.LogMessageAsync(new LogMessage(LogSeverity.Info, "TemporaryInfractions", $"Resolved {resolvedCounter} temporary infraction(s)."));
                _data.SaveUserData();
            }
        }