Made .showembed always output lowercase field names and no null values

This commit is contained in:
Kwoth
2021-12-14 19:41:07 +01:00
parent 9be8140d4d
commit 83daf3c30f
3 changed files with 27 additions and 3 deletions

View File

@@ -0,0 +1,12 @@
using System.Text.Json;
namespace SystemTextJsonSamples
{
public class LowerCaseNamingPolicy : JsonNamingPolicy
{
public static LowerCaseNamingPolicy Default = new LowerCaseNamingPolicy();
public override string ConvertName(string name) =>
name.ToLower();
}
}

View File

@@ -11,11 +11,14 @@ using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using NadekoBot.Common.Replacements;
using NadekoBot.Services;
using Serilog;
using SystemTextJsonSamples;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
namespace NadekoBot.Modules.Utility
{
@@ -368,6 +371,13 @@ namespace NadekoBot.Modules.Utility
public Task ShowEmbed(ulong messageId)
=> ShowEmbed((ITextChannel)ctx.Channel, messageId);
private static readonly JsonSerializerOptions _showEmbedSerializerOptions = new JsonSerializerOptions()
{
WriteIndented = true,
IgnoreNullValues = true,
PropertyNamingPolicy = LowerCaseNamingPolicy.Default
};
[NadekoCommand, Aliases]
[RequireContext(ContextType.Guild)]
public async Task ShowEmbed(ITextChannel ch, ulong messageId)
@@ -394,7 +404,7 @@ namespace NadekoBot.Modules.Utility
return;
}
var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson();
var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(_showEmbedSerializerOptions);
await SendConfirmAsync(Format.Sanitize(json).Replace("](", "]\\("));
}

View File

@@ -22,10 +22,12 @@ using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NadekoBot.Common.Attributes;
using Color = Discord.Color;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace NadekoBot.Extensions
{
@@ -286,8 +288,8 @@ namespace NadekoBot.Extensions
public static async Task<IEnumerable<IGuildUser>> GetMembersAsync(this IRole role) =>
(await role.Guild.GetUsersAsync(CacheMode.CacheOnly).ConfigureAwait(false)).Where(u => u.RoleIds.Contains(role.Id)) ?? Enumerable.Empty<IGuildUser>();
public static string ToJson<T>(this T any, Formatting formatting = Formatting.Indented) =>
JsonConvert.SerializeObject(any, formatting);
public static string ToJson<T>(this T any, JsonSerializerOptions options = null) =>
JsonSerializer.Serialize(any, options);
/// <summary>
/// Adds fallback fonts to <see cref="TextOptions"/>