Example #1
0
        private async Task MuteUserForSpam(Contexts contexts, TimeSpan length)
        {
            var timeRange = new TimeRange(DateTime.UtcNow, DateTime.UtcNow.Add(length));
            var muteEvent = new MuteEvent(contexts.User.Id, timeRange, "Spam detected (by bot)", contexts.Server.Id, contexts.Channel.Id);

            await this._mutingService.MuteUserOrOverwrite(contexts, muteEvent, contexts.User);

            this._unmutingService.UnmuteInFuture(contexts, muteEvent, contexts.User);
        }
 public void UnmuteInFuture(Contexts contexts, MuteEvent muteEvent, UserContext userToUnmute)
 {
     if (this.ShouldBeConsideredAsShortMute(muteEvent))
     {
         this.UnmuteInShortTime(contexts, muteEvent, userToUnmute);
         return;
     }
     Log.Information("Mute {muteEventId} of user {userName} is considered as longer mute", muteEvent.Id, userToUnmute.Name);
 }
Example #3
0
        private async Task UnmuteUser(UserContext mutedUser, MuteEvent muteEvent, DiscordServerContext serverContext)
        {
            var muteRole = GetMuteRole(serverContext);

            await RemoveMuteRoleAsync(muteRole, mutedUser, serverContext);
            await MarkAsUnmuted(muteEvent);

            Log.Information("User {user} has been unmuted on server {server}", mutedUser.ToString(), serverContext.Name);
        }
        private async void RemoveMuteRole(MuteEvent muteEvent, DiscordServerContext server, IEnumerable <UserContext> users, UserRole muteRole)
        {
            var user = users.FirstOrDefault(userContext => userContext.Id == muteEvent.UserId);

            if (user == null)
            {
                return;
            }

            await _usersService.RemoveRole(muteRole, user, server);

            var command = new MarkMuteEventAsUnmutedCommand(muteEvent.Id);
            await _commandBus.ExecuteAsync(command);
        }
        public async Task MuteUser(MuteCommand command, Contexts contexts)
        {
            var userToMute = await this._usersService.GetUserByIdAsync(contexts.Server, command.User);

            if (userToMute == null || userToMute.Id == this._usersService.GetBot().Id)
            {
                throw new UserNotFoundException(command.User.GetUserMention());
            }
            var timeRange = TimeRange.FromNow(DateTime.UtcNow + command.Time); //todo: change DateTime.UtcNow to Contexts.SentAt
            var muteEvent = new MuteEvent(userToMute.Id, timeRange, command.Reason, contexts.Server.Id, contexts.Channel.Id);

            await this._mutingService.MuteUserOrOverwrite(contexts, muteEvent, userToMute);

            this._unmutingService.UnmuteInFuture(contexts, muteEvent, userToMute);
        }
Example #6
0
        public async void UnmuteInFuture(Contexts contexts, MuteEvent muteEvent, UserContext userToUnmute)
        {
            var timeRange = muteEvent.TimeRange;
            await Task.Delay(timeRange.End - timeRange.Start);

            var wasNeededToUnmute = await UnmuteIfNeeded(contexts.Server, userToUnmute);

            if (wasNeededToUnmute)
            {
                var messagesService = _messagesServiceFactory.Create(contexts);
                await messagesService.SendResponse(x => x.UnmutedUser(userToUnmute), contexts);

                await _directMessagesService.TrySendMessage(userToUnmute.Id, x => x.UnmutedUserForUser(userToUnmute, contexts.Server), contexts);
            }
        }
Example #7
0
        public async Task MuteUserOrOverwrite(Contexts contexts, MuteEvent muteEvent, UserContext userToMute)
        {
            var possiblePreviousUserMuteEvent       = this._mutingHelper.GetNotUnmutedUserMuteEvent(contexts.Server.Id, userToMute.Id);
            var shouldJustMuteAgainTheSameMuteEvent = possiblePreviousUserMuteEvent?.Id == muteEvent.Id;

            if (possiblePreviousUserMuteEvent != null && !shouldJustMuteAgainTheSameMuteEvent)
            {
                await this._mutingHelper.MarkAsUnmuted(possiblePreviousUserMuteEvent);
            }
            await this.MuteUser(userToMute, contexts.Server);

            await this._commandBus.ExecuteAsync(new AddMuteEventCommand(muteEvent));

            await this.NotifyUserAboutMute(contexts, userToMute, muteEvent);
        }
Example #8
0
        public async Task MuteUserOrOverwrite(Contexts contexts, MuteEvent muteEvent, UserContext userToMute)
        {
            var possiblePreviousUserMuteEvent       = GetNotUnmutedUserMuteEvent(contexts.Server, userToMute);
            var shouldJustMuteAgainTheSameMuteEvent = possiblePreviousUserMuteEvent?.Id == muteEvent.Id;

            if (possiblePreviousUserMuteEvent != null && !shouldJustMuteAgainTheSameMuteEvent)
            {
                var markAsUnmuted = new MarkMuteEventAsUnmutedCommand(possiblePreviousUserMuteEvent.Id);
                await _commandBus.ExecuteAsync(markAsUnmuted);
            }

            await MuteUser(userToMute, contexts.Server);

            await _commandBus.ExecuteAsync(new AddMuteEventCommand(muteEvent));

            var messagesService = _messagesServiceFactory.Create(contexts);
            await messagesService.SendResponse(x => x.MutedUser(userToMute, muteEvent.TimeRange.End), contexts);
        }
        private async void UnmuteInShortTime(Contexts contexts, MuteEvent muteEvent, UserContext userToUnmute)
        {
            if (this._muteEventsAlreadyBeingHandled.Contains(muteEvent.Id))
            {
                return;
            }
            if (muteEvent.UserId != userToUnmute.Id)
            {
                throw new ArgumentException($"value of {nameof(muteEvent.UserId)} is different than {nameof(userToUnmute.Id)}");
            }
            if (muteEvent.TimeRange.End > DateTime.UtcNow)
            {
                Log.Information("Waiting short time for unmute user {userName} for muteEventId {muteEventId}", userToUnmute.Name, muteEvent.Id);
                this._muteEventsAlreadyBeingHandled.Add(muteEvent.Id);
                await Task.Delay(muteEvent.TimeRange.End - DateTime.UtcNow);
            }
            await this.UnmuteSpecificEvent(contexts, userToUnmute, muteEvent);

            this._muteEventsAlreadyBeingHandled.Remove(muteEvent.Id);
        }
Example #10
0
 public ISettingCommandComposite Mute(MuteEvent mute)
 {
     this.parts[this.parts.Count - 1].Setting.Mute = mute;
     return(this);
 }
Example #11
0
 private async Task MarkAsUnmuted(MuteEvent muteEvent)
 {
     var command = new MarkMuteEventAsUnmutedCommand(muteEvent.Id);
     await _commandBus.ExecuteAsync(command);
 }
Example #12
0
        private (string title, string description, IEnumerable <KeyValuePair <string, string> > values) GetMuteEmbedMessage(UserContext user, DiscordServerContext server, MuteEvent muteEvent)
        {
            var title       = "Zostałeś wyciszony";
            var description = $"Cześć {user.Mention}! Zostałeś wyciszony przez moderatora na serwerze {server.Name}";
            var values      = new Dictionary <string, string>
            {
                { "Serwer:", server.Name },
                { "Powód:", $"{muteEvent.Reason}" },
                { "Czas wygaśnięcia:", muteEvent.TimeRange.End.ToLocalTimeString() }
            };

            return(title, description, values);
        }
Example #13
0
        private (string title, string description, IEnumerable <KeyValuePair <string, string> > values) GetMuteEmbedMessage(UserContext user, DiscordServerContext server, MuteEvent muteEvent)
        {
            var title       = "Zostałeś wyciszony";
            var description = $"Cześć {user.Mention}! Zostałeś wyciszony przez moderatora na serwerze {server.Name}";
            var values      = new Dictionary <string, string>
            {
                { "Serwer:", server.Name },
                { "Powód:", $"{muteEvent.Reason}" },
                { "Czas wygaśnięcia:", TimeZoneInfo.ConvertTimeFromUtc(muteEvent.TimeRange.End, TimeZoneInfo.Local).ToString("dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture) }
            };

            return(title, description, values);
        }
Example #14
0
        private async Task NotifyUserAboutMute(Contexts contexts, UserContext mutedUser, MuteEvent muteEvent)
        {
            var messagesService = this._messagesServiceFactory.Create(contexts);
            await messagesService.SendResponse(x => x.MutedUser(mutedUser, muteEvent.TimeRange.End));

            var(title, description, values) = this.GetMuteEmbedMessage(mutedUser, contexts.Server, muteEvent);
            await this._directMessagesService.TrySendEmbedMessage(mutedUser.Id, title, description, values);
        }
Example #15
0
 public AddMuteEventCommand(MuteEvent muteEvent)
 {
     MuteEvent = muteEvent;
 }
        private async void RemoveInFuture(MuteEvent muteEvent, DiscordServerContext server, IEnumerable <UserContext> users, UserRole muteRole)
        {
            await Task.Delay(muteEvent.TimeRange.End - DateTime.UtcNow);

            this.RemoveMuteRole(muteEvent, server, users, muteRole);
        }
Example #17
0
 private bool ShouldBeConsideredAsShortMute(MuteEvent muteEvent)
 {
     return(muteEvent.TimeRange.End < DateTime.UtcNow.AddMinutes(SHORT_MUTE_TIME_IN_MINUTES));
 }
Example #18
0
        private async Task UnmuteSpecificEvent(Contexts contexts, UserContext userToUnmute, MuteEvent muteEvent)
        {
            Log.Information("Unmuting {muteEventId} mute event of user {userName}", muteEvent.Id, userToUnmute.Name);
            var serverMuteEvents = this._mutingHelper.GetNotUnmutedMuteEvents(contexts.Server.Id);
            var eventToUnmute    = serverMuteEvents.FirstOrDefault(x => x.Id == muteEvent.Id);

            if (eventToUnmute == null)
            {
                Log.Information("Mute event {muteEventId} of user {userName} doesn't exists or was unmuted earlier", muteEvent.Id, userToUnmute.Name);
                return;
            }
            var isStillOnServer = await this._usersService.IsUserStillOnServerAsync(contexts.Server, userToUnmute.Id);

            if (!isStillOnServer)
            {
                Log.Information("User {userName} left server {serverName} so he/she's not going to be unmuted", userToUnmute.Name, contexts.Server.Name);
                return;
            }
            await this.UnmuteUser(userToUnmute, eventToUnmute, contexts.Server);

            await this.NotifyAboutUnmute(contexts, userToUnmute);
        }