NadekoBot.Extensions should now be fully annotated with nullable reference types as well as many classes from NadekoBot.Common

This commit is contained in:
Kwoth
2021-12-28 10:44:00 +01:00
parent 0634470a8a
commit 59f5056035
543 changed files with 1895 additions and 1448 deletions

View File

@@ -1,4 +1,4 @@
namespace NadekoBot.Extensions;
namespace NadekoBot.Extensions;
// made for customreactions because they almost never get added
// and they get looped through constantly
@@ -34,4 +34,4 @@ public static class ArrayExtensions
/// <returns>New array with updated elements</returns>
public static TOut[] Map<TIn, TOut>(this TIn[] arr, Func<TIn, TOut> f)
=> Array.ConvertAll(arr, x => f(x));
}
}

View File

@@ -1,7 +1,7 @@
namespace NadekoBot.Extensions;
namespace NadekoBot.Extensions;
public static class BotCredentialsExtensions
{
public static bool IsOwner(this IBotCredentials creds, IUser user)
=> creds.OwnerIds.Contains(user.Id);
}
}

View File

@@ -1,4 +1,4 @@
using System.Security.Cryptography;
using System.Security.Cryptography;
using NadekoBot.Common.Collections;
using NadekoBot.Services.Database.Models;
@@ -14,7 +14,7 @@ public static class EnumerableExtensions
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
/// <typeparam name="T">The type of the members of values.</typeparam>
/// <returns>A string that consists of the members of values delimited by the separator character. -or- Empty if values has no elements.</returns>
public static string Join<T>(this IEnumerable<T> data, char separator, Func<T, string> func = null)
public static string Join<T>(this IEnumerable<T> data, char separator, Func<T, string>? func = null)
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
/// <summary>
@@ -25,7 +25,7 @@ public static class EnumerableExtensions
/// <param name="func">Optional transformation to apply to each element before concatenation.</param>
/// <typeparam name="T">The type of the members of values.</typeparam>
/// <returns>A string that consists of the members of values delimited by the separator character. -or- Empty if values has no elements.</returns>
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string> func = null)
public static string Join<T>(this IEnumerable<T> data, string separator, Func<T, string>? func = null)
=> string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty)));
/// <summary>
@@ -66,6 +66,7 @@ public static class EnumerableExtensions
/// <returns>A new instance of the <see cref="ConcurrentDictionary{TKey,TValue}"/> class</returns>
public static ConcurrentDictionary<TKey, TValue> ToConcurrent<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> dict)
where TKey : notnull
=> new(dict);
public static IndexedCollection<T> ToIndexed<T>(this IEnumerable<T> enumerable)
@@ -83,4 +84,4 @@ public static class EnumerableExtensions
/// <returns>A task that represents the completion of all of the supplied tasks.</returns>
public static Task<TResult[]> WhenAll<TResult>(this IEnumerable<Task<TResult>> tasks)
=> Task.WhenAll(tasks);
}
}

View File

@@ -1,4 +1,4 @@
using Humanizer.Localisation;
using Humanizer.Localisation;
using NadekoBot.Modules.Administration.Services;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
@@ -147,7 +147,7 @@ public static class Extensions
);
private static string MethodName(this CommandInfo cmd)
=> ((NadekoCommandAttribute)cmd.Attributes.FirstOrDefault(x => x is NadekoCommandAttribute))?.MethodName ??
=> ((NadekoCommandAttribute?)cmd.Attributes.FirstOrDefault(x => x is NadekoCommandAttribute))?.MethodName ??
cmd.Name;
private static string GetFullUsage(string commandName, string args, string prefix)
@@ -177,7 +177,7 @@ public static class Extensions
this IUserMessage msg,
DiscordSocketClient client,
Func<SocketReaction, Task> reactionAdded,
Func<SocketReaction, Task> reactionRemoved = null)
Func<SocketReaction, Task>? reactionRemoved = null)
{
if (reactionRemoved is null)
reactionRemoved = _ => Task.CompletedTask;
@@ -209,11 +209,8 @@ public static class Extensions
);
}
public static IMessage DeleteAfter(this IUserMessage msg, int seconds, ILogCommandService logService = null)
public static IMessage DeleteAfter(this IUserMessage msg, int seconds, ILogCommandService? logService = null)
{
if (msg is null)
return null;
Task.Run(async () =>
{
await Task.Delay(seconds * 1000).ConfigureAwait(false);
@@ -245,7 +242,7 @@ public static class Extensions
return users.Where(u => u.RoleIds.Contains(role.Id));
}
public static string ToJson<T>(this T any, JsonSerializerOptions options = null)
public static string ToJson<T>(this T any, JsonSerializerOptions? options = null)
=> JsonSerializer.Serialize(any, options);
/// <summary>
@@ -276,7 +273,7 @@ public static class Extensions
return opts;
}
public static MemoryStream ToStream(this Image<Rgba32> img, IImageFormat format = null)
public static MemoryStream ToStream(this Image<Rgba32> img, IImageFormat? format = null)
{
var imageStream = new MemoryStream();
if (format?.Name == "GIF")
@@ -307,7 +304,7 @@ public static class Extensions
public static bool IsImage(this HttpResponseMessage msg)
=> IsImage(msg, out _);
public static bool IsImage(this HttpResponseMessage msg, out string mimeType)
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")
@@ -333,4 +330,4 @@ public static class Extensions
public static string GetText(this IBotStrings strings, in LocStr str, CultureInfo culture)
=> strings.GetText(str.Key, culture, str.Params);
}
}

View File

@@ -1,4 +1,4 @@
namespace NadekoBot.Extensions;
namespace NadekoBot.Extensions;
public static class MessageChannelExtensions
{
@@ -7,8 +7,8 @@ public static class MessageChannelExtensions
public static Task<IUserMessage> SendAsync(
this IMessageChannel channel,
string plainText,
Embed embed,
string? plainText,
Embed? embed,
bool sanitizeAll = false)
{
plainText = sanitizeAll ? plainText?.SanitizeAllMentions() ?? "" : plainText?.SanitizeMentions() ?? "";
@@ -32,8 +32,8 @@ public static class MessageChannelExtensions
IEmbedBuilderService eb,
string title,
string error,
string url = null,
string footer = null)
string? url = null,
string? footer = null)
{
var embed = eb.Create().WithErrorColor().WithDescription(error).WithTitle(title);
@@ -58,8 +58,8 @@ public static class MessageChannelExtensions
IEmbedBuilderService eb,
string title,
string text,
string url = null,
string footer = null)
string? url = null,
string? footer = null)
{
var embed = eb.Create().WithOkColor().WithDescription(text).WithTitle(title);
@@ -223,4 +223,4 @@ public static class MessageChannelExtensions
public static Task WarningAsync(this ICommandContext ctx)
=> ctx.Message.AddReactionAsync(new Emoji("⚠️"));
}
}

View File

@@ -1,5 +1,4 @@
#nullable enable
namespace NadekoBot.Extensions;
namespace NadekoBot.Extensions;
public static class LinkedListExtensions
{
@@ -16,4 +15,4 @@ public static class LinkedListExtensions
return null;
}
}
}

View File

@@ -1,4 +1,4 @@
namespace NadekoBot.Extensions;
namespace NadekoBot.Extensions;
public static class NumberExtensions
{
@@ -50,4 +50,4 @@ public static class NumberExtensions
0,
TimeSpan.Zero
).AddSeconds(number);
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// https://github.com/aspnet/Common/blob/dev/shared/Microsoft.Extensions.Process.Sources/ProcessHelper.cs
@@ -79,7 +79,7 @@ public static class ProcessExtensions
string fileName,
string arguments,
TimeSpan timeout,
out string stdout)
out string? stdout)
{
stdout = null;
@@ -104,4 +104,4 @@ public static class ProcessExtensions
return process.ExitCode;
}
}
}

View File

@@ -1,4 +1,4 @@
using SixLabors.ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Gif;
using SixLabors.ImageSharp.Formats.Png;
@@ -54,4 +54,4 @@ public static class Rgba32Extensions
canvas.Frames.RemoveFrame(0);
return canvas;
}
}
}

View File

@@ -1,4 +1,4 @@
using System.Reflection;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Modules.Music;
using NadekoBot.Modules.Music.Resolvers;
@@ -70,4 +70,4 @@ public static class ServiceCollectionExtensions
services.AddSingleton(ConnectionMultiplexer.Connect(conf));
return services;
}
}
}

View File

@@ -14,7 +14,7 @@ public static class StringExtensions
return str.PadLeft(padLeft).PadRight(length);
}
public static T MapJson<T>(this string str)
public static T? MapJson<T>(this string str)
=> JsonConvert.DeserializeObject<T>(str);
private static readonly HashSet<char> _lettersAndDigits = new(Enumerable.Range(48, 10)
@@ -26,7 +26,7 @@ public static class StringExtensions
public static string StripHtml(this string input)
=> Regex.Replace(input, "<.*?>", String.Empty);
public static string TrimTo(this string str, int maxLength, bool hideDots = false)
public static string? TrimTo(this string? str, int maxLength, bool hideDots = false)
=> hideDots ? str?.Truncate(maxLength, string.Empty) : str?.Truncate(maxLength);
public static string ToTitleCase(this string str)
@@ -148,4 +148,4 @@ public static class StringExtensions
return newString;
}
);
}
}

View File

@@ -1,4 +1,4 @@
using NadekoBot.Db.Models;
using NadekoBot.Db.Models;
namespace NadekoBot.Extensions;
@@ -30,11 +30,11 @@ public static class UserExtensions
=> usr.AvatarId is null ? new(usr.GetDefaultAvatarUrl()) : new Uri(usr.GetAvatarUrl(ImageFormat.Auto, size));
// This method is only used for the xp card
public static Uri RealAvatarUrl(this DiscordUser usr)
public static Uri? RealAvatarUrl(this DiscordUser usr)
=> usr.AvatarId is null
? null
: new Uri(usr.AvatarId.StartsWith("a_", StringComparison.InvariantCulture)
? $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.gif"
: $"{DiscordConfig.CDNUrl}avatars/{usr.UserId}/{usr.AvatarId}.png"
);
}
}