using System; using System.IO; using System.Threading; using System.Threading.Tasks; using Discord; namespace DiscordBotCore.Others; /// /// A special class with functions /// public static class Functions { /// /// The location for the Resources folder /// String: ./Data/Resources/ /// public static readonly string dataFolder = @"./Data/Resources/"; public static Color RandomColor { get { var random = new Random(); return new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)); } } /// /// Copy one Stream to another /// /// The base stream /// The destination stream /// The buffer to read /// The progress /// The cancellation token /// Triggered if any is empty /// Triggered if is less then or equal to 0 /// Triggered if is not readable /// Triggered in is not writable public static async Task CopyToOtherStreamAsync( this Stream stream, Stream destination, int bufferSize, IProgress? progress = null, CancellationToken cancellationToken = default) { if (stream == null) throw new ArgumentNullException(nameof(stream)); if (destination == null) throw new ArgumentNullException(nameof(destination)); if (bufferSize <= 0) throw new ArgumentOutOfRangeException(nameof(bufferSize)); if (!stream.CanRead) throw new InvalidOperationException("The stream is not readable."); if (!destination.CanWrite) throw new ArgumentException("Destination stream is not writable", nameof(destination)); var buffer = new byte[bufferSize]; long totalBytesRead = 0; int bytesRead; while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken) .ConfigureAwait(false)) != 0) { await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false); totalBytesRead += bytesRead; progress?.Report(totalBytesRead); } } public static T SelectRandomValueOf() { var enums = Enum.GetValues(typeof(T)); var random = new Random(); return (T)enums.GetValue(random.Next(enums.Length)); } public static T RandomValue(this T[] values) { Random random = new(); return values[random.Next(values.Length)]; } public static string ToResourcesPath(this string path) { return Path.Combine(dataFolder, path); } }