public UserContext GetUser() { var mention = _request.GetMention(); var userToMute = _usersService.GetUserByMention(_contexts.Server, mention); if (userToMute == null) { throw new UserNotFoundException(mention); } return(userToMute); }
public async Task ReadUserMessages(DiscordRequest request, Contexts contexts) { var mention = request.GetMention(); var selectedUser = await this._usersService.GetUserByMentionAsync(contexts.Server, mention); if (selectedUser == null) { throw new UserNotFoundException(mention); } var timeRange = request.GetPastTimeRange(defaultTime: TimeSpan.FromHours(1)); var query = new GetMessagesQuery(contexts.Server.Id, selectedUser.Id) { SentDate = timeRange }; var messages = this._queryBus.Execute(query).Messages .OrderBy(x => x.SentAt) .ToList(); var messagesService = this._messagesServiceFactory.Create(contexts); var hasForceArgument = request.HasArgument("force") || request.HasArgument("f"); if (messages.Count > 200 && !hasForceArgument) { await messagesService.SendResponse(x => x.NumberOfMessagesIsHuge(messages.Count)); return; } if (!messages.Any()) { await messagesService.SendResponse(x => x.UserDidntWriteAnyMessageInThisTime(selectedUser)); return; } var header = $"Messages from user {selectedUser} starting at {timeRange.Start}"; var lines = messages.Select(x => $"{x.SentAt:yyyy-MM-dd HH:mm:ss} {x.Author.Name}: {x.Content.Replace("```", "")}"); var linesBuilder = new StringBuilder().PrintManyLines(lines.ToArray(), contentStyleBox: true); await this._directMessagesService.TrySendMessage(contexts.User.Id, header); await this._directMessagesService.TrySendMessage(contexts.User.Id, linesBuilder.ToString(), MessageType.BlockFormatted); await messagesService.SendResponse(x => x.SentByDmMessagesOfAskedUser(messages.Count, selectedUser)); }