mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 17:28:27 -04:00
Applied codestyle to all .cs files
This commit is contained in:
@@ -13,7 +13,6 @@ using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Color = Discord.Color;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
// todo imagesharp extensions
|
||||
namespace NadekoBot.Extensions;
|
||||
@@ -27,17 +26,15 @@ public static class Extensions
|
||||
=> text switch
|
||||
{
|
||||
SmartEmbedText set => msg.ModifyAsync(x =>
|
||||
{
|
||||
x.Embed = set.GetEmbed().Build();
|
||||
x.Content = set.PlainText?.SanitizeMentions() ?? "";
|
||||
}
|
||||
),
|
||||
{
|
||||
x.Embed = set.GetEmbed().Build();
|
||||
x.Content = set.PlainText?.SanitizeMentions() ?? "";
|
||||
}),
|
||||
SmartPlainText spt => msg.ModifyAsync(x =>
|
||||
{
|
||||
x.Content = spt.Text.SanitizeMentions();
|
||||
x.Embed = null;
|
||||
}
|
||||
),
|
||||
{
|
||||
x.Content = spt.Text.SanitizeMentions();
|
||||
x.Embed = null;
|
||||
}),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(text))
|
||||
};
|
||||
|
||||
@@ -45,8 +42,8 @@ public static class Extensions
|
||||
=> client.Guilds.Select(x => x.Id).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Generates a string in the format HHH:mm if timespan is >= 2m.
|
||||
/// Generates a string in the format 00:mm:ss if timespan is less than 2m.
|
||||
/// Generates a string in the format HHH:mm if timespan is >= 2m.
|
||||
/// Generates a string in the format 00:mm:ss if timespan is less than 2m.
|
||||
/// </summary>
|
||||
/// <param name="span">Timespan to convert to string</param>
|
||||
/// <returns>Formatted duration string</returns>
|
||||
@@ -75,18 +72,14 @@ public static class Extensions
|
||||
var size = ctx.GetCurrentSize();
|
||||
var corners = BuildCorners(size.Width, size.Height, cornerRadius);
|
||||
|
||||
ctx.SetGraphicsOptions(new GraphicsOptions()
|
||||
{
|
||||
Antialias = true,
|
||||
// enforces that any part of this shape that has color is punched out of the background
|
||||
AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
|
||||
}
|
||||
);
|
||||
|
||||
foreach (var c in corners)
|
||||
ctx.SetGraphicsOptions(new GraphicsOptions
|
||||
{
|
||||
ctx = ctx.Fill(SixLabors.ImageSharp.Color.Red, c);
|
||||
}
|
||||
Antialias = true,
|
||||
// enforces that any part of this shape that has color is punched out of the background
|
||||
AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
|
||||
});
|
||||
|
||||
foreach (var c in corners) ctx = ctx.Fill(SixLabors.ImageSharp.Color.Red, c);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
@@ -94,11 +87,7 @@ public static class Extensions
|
||||
private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
|
||||
{
|
||||
// first create a square
|
||||
var rect = new RectangularPolygon(-0.5f,
|
||||
-0.5f,
|
||||
cornerRadius,
|
||||
cornerRadius
|
||||
);
|
||||
var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);
|
||||
|
||||
// then cut out of the square a circle so we are left with a corner
|
||||
var cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));
|
||||
@@ -114,15 +103,11 @@ public static class Extensions
|
||||
var cornerBottomLeft = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
|
||||
var cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);
|
||||
|
||||
return new PathCollection(cornerTopLeft,
|
||||
cornerBottomLeft,
|
||||
cornerTopRight,
|
||||
cornerBottomRight
|
||||
);
|
||||
return new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// First 10 characters of teh bot token.
|
||||
/// First 10 characters of teh bot token.
|
||||
/// </summary>
|
||||
public static string RedisKey(this IBotCredentials bc)
|
||||
=> bc.Token[..10];
|
||||
@@ -143,12 +128,11 @@ public static class Extensions
|
||||
ulong? guildId,
|
||||
string prefix)
|
||||
=> Array.ConvertAll(strings.GetCommandStrings(cmd.MethodName(), guildId).Args,
|
||||
arg => GetFullUsage(cmd.Name, arg, prefix)
|
||||
);
|
||||
arg => GetFullUsage(cmd.Name, arg, prefix));
|
||||
|
||||
private static string MethodName(this CommandInfo cmd)
|
||||
=> ((NadekoCommandAttribute?)cmd.Attributes.FirstOrDefault(x => x is NadekoCommandAttribute))?.MethodName ??
|
||||
cmd.Name;
|
||||
=> ((NadekoCommandAttribute?)cmd.Attributes.FirstOrDefault(x => x is NadekoCommandAttribute))?.MethodName
|
||||
?? cmd.Name;
|
||||
|
||||
private static string GetFullUsage(string commandName, string args, string prefix)
|
||||
=> $"{prefix}{commandName} {string.Format(args, prefix)}";
|
||||
@@ -157,8 +141,7 @@ public static class Extensions
|
||||
{
|
||||
if (lastPage != null)
|
||||
return embed.WithFooter($"{curPage + 1} / {lastPage + 1}");
|
||||
else
|
||||
return embed.WithFooter(curPage.ToString());
|
||||
return embed.WithFooter(curPage.ToString());
|
||||
}
|
||||
|
||||
public static Color ToDiscordColor(this Rgba32 color)
|
||||
@@ -205,33 +188,25 @@ public static class Extensions
|
||||
dict.Clear();
|
||||
dict.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
|
||||
dict.Add("User-Agent",
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1"
|
||||
);
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1");
|
||||
}
|
||||
|
||||
public static IMessage DeleteAfter(this IUserMessage msg, int seconds, ILogCommandService? logService = null)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(seconds * 1000);
|
||||
if (logService != null)
|
||||
{
|
||||
logService.AddDeleteIgnore(msg.Id);
|
||||
}
|
||||
{
|
||||
await Task.Delay(seconds * 1000);
|
||||
if (logService != null) logService.AddDeleteIgnore(msg.Id);
|
||||
|
||||
try { await msg.DeleteAsync(); }
|
||||
catch { }
|
||||
}
|
||||
);
|
||||
try { await msg.DeleteAsync(); }
|
||||
catch { }
|
||||
});
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static ModuleInfo GetTopLevelModule(this ModuleInfo module)
|
||||
{
|
||||
while (module.Parent != null)
|
||||
{
|
||||
module = module.Parent;
|
||||
}
|
||||
while (module.Parent != null) module = module.Parent;
|
||||
|
||||
return module;
|
||||
}
|
||||
@@ -246,27 +221,24 @@ public static class Extensions
|
||||
=> JsonSerializer.Serialize(any, options);
|
||||
|
||||
/// <summary>
|
||||
/// Adds fallback fonts to <see cref="TextOptions"/>
|
||||
/// Adds fallback fonts to <see cref="TextOptions" />
|
||||
/// </summary>
|
||||
/// <param name="opts"><see cref="TextOptions"/> to which fallback fonts will be added to</param>
|
||||
/// <param name="opts"><see cref="TextOptions" /> to which fallback fonts will be added to</param>
|
||||
/// <param name="fallback">List of fallback Font Families to add</param>
|
||||
/// <returns>The same <see cref="TextOptions"/> to allow chaining</returns>
|
||||
/// <returns>The same <see cref="TextOptions" /> to allow chaining</returns>
|
||||
public static TextOptions WithFallbackFonts(this TextOptions opts, List<FontFamily> fallback)
|
||||
{
|
||||
foreach (var ff in fallback)
|
||||
{
|
||||
opts.FallbackFonts.Add(ff);
|
||||
}
|
||||
foreach (var ff in fallback) opts.FallbackFonts.Add(ff);
|
||||
|
||||
return opts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds fallback fonts to <see cref="TextGraphicsOptions"/>
|
||||
/// Adds fallback fonts to <see cref="TextGraphicsOptions" />
|
||||
/// </summary>
|
||||
/// <param name="opts"><see cref="TextGraphicsOptions"/> to which fallback fonts will be added to</param>
|
||||
/// <param name="opts"><see cref="TextGraphicsOptions" /> to which fallback fonts will be added to</param>
|
||||
/// <param name="fallback">List of fallback Font Families to add</param>
|
||||
/// <returns>The same <see cref="TextGraphicsOptions"/> to allow chaining</returns>
|
||||
/// <returns>The same <see cref="TextGraphicsOptions" /> to allow chaining</returns>
|
||||
public static TextGraphicsOptions WithFallbackFonts(this TextGraphicsOptions opts, List<FontFamily> fallback)
|
||||
{
|
||||
opts.TextOptions.WithFallbackFonts(fallback);
|
||||
@@ -277,15 +249,13 @@ public static class Extensions
|
||||
{
|
||||
var imageStream = new MemoryStream();
|
||||
if (format?.Name == "GIF")
|
||||
{
|
||||
img.SaveAsGif(imageStream);
|
||||
}
|
||||
else
|
||||
{
|
||||
img.SaveAsPng(imageStream,
|
||||
new() { ColorType = PngColorType.RgbWithAlpha, CompressionLevel = PngCompressionLevel.BestCompression }
|
||||
);
|
||||
}
|
||||
new()
|
||||
{
|
||||
ColorType = PngColorType.RgbWithAlpha, CompressionLevel = PngCompressionLevel.BestCompression
|
||||
});
|
||||
|
||||
imageStream.Position = 0;
|
||||
return imageStream;
|
||||
@@ -307,20 +277,14 @@ public static class Extensions
|
||||
public static bool IsImage(this HttpResponseMessage msg, out string? mimeType)
|
||||
{
|
||||
mimeType = msg.Content.Headers.ContentType?.MediaType;
|
||||
if (mimeType is "image/png" or "image/jpeg" or "image/gif")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (mimeType is "image/png" or "image/jpeg" or "image/gif") return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static long? GetImageSize(this HttpResponseMessage msg)
|
||||
{
|
||||
if (msg.Content.Headers.ContentLength is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (msg.Content.Headers.ContentLength is null) return null;
|
||||
|
||||
return msg.Content.Headers.ContentLength.Value / 1.Mb();
|
||||
}
|
||||
@@ -330,4 +294,4 @@ public static class Extensions
|
||||
|
||||
public static string GetText(this IBotStrings strings, in LocStr str, CultureInfo culture)
|
||||
=> strings.GetText(str.Key, culture, str.Params);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user