From 491939d93a4ea4702fc7602581f42082d2942a4d Mon Sep 17 00:00:00 2001 From: Hubert Date: Mon, 29 Jun 2026 14:30:24 +0200 Subject: [PATCH 1/7] feat: Add `RegisteredApplicationCommand` ToString override --- .gitignore | 5 ++++- .../RegisteredApplicationCommand.cs | 10 +++++++++- NetCord/Mention.cs | 18 ++++++++++++++++++ NetCord/Rest/ApplicationCommand.cs | 19 ++----------------- NetCord/Rest/ApplicationCommandOption.cs | 19 ++----------------- Tests/MentionTest/TryFormat.cs | 11 +++++++++++ 6 files changed, 46 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index 44c3b430e..006a4a894 100644 --- a/.gitignore +++ b/.gitignore @@ -364,4 +364,7 @@ FodyWeavers.xsd # Settings file appsettings.json -!Documentation/**/appsettings.json \ No newline at end of file +!Documentation/**/appsettings.json + +# Intellij configuration files (eg. Jetbrains Rider) +.idea/ diff --git a/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs b/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs index c16c56b0c..759982223 100644 --- a/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs +++ b/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs @@ -1,3 +1,11 @@ namespace NetCord.Services.ApplicationCommands; -public readonly record struct RegisteredApplicationCommand(ulong Id, ApplicationCommandInfo Info) where TContext : IApplicationCommandContext; +public readonly record struct RegisteredApplicationCommand(ulong Id, ApplicationCommandInfo Info) : ISpanFormattable where TContext : IApplicationCommandContext +{ + public override string ToString() => $""; + + public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); + + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => + Mention.TryFormatSlashCommand(destination, out charsWritten, Id, Info.Name); +} diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index 17e0e2a1e..b00784627 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -167,6 +167,24 @@ public static bool TryFormatRole(Span destination, out int charsWritten, u return TryFormat(destination, out charsWritten, id, "<@&", ">"); } + public static bool TryFormatSlashCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) + { + var requiredLength = 5 + fullName.Length; + if (destination.Length < requiredLength || !id.TryFormat(destination[(3 + fullName.Length)..^1], out int length)) + { + charsWritten = 0; + return false; + } + + "'; + + charsWritten = 4 + fullName.Length + length; + return true; + } + private static bool TryFormat(Span destination, out int charsWritten, ulong id, ReadOnlySpan prefix, ReadOnlySpan suffix) { if (destination.Length <= prefix.Length + suffix.Length || !id.TryFormat(destination[prefix.Length..^suffix.Length], out int length)) diff --git a/NetCord/Rest/ApplicationCommand.cs b/NetCord/Rest/ApplicationCommand.cs index 26940e66a..cd70f1151 100644 --- a/NetCord/Rest/ApplicationCommand.cs +++ b/NetCord/Rest/ApplicationCommand.cs @@ -69,21 +69,6 @@ public partial class ApplicationCommand(JsonModels.JsonApplicationCommand jsonMo public override string ToString() => $""; - public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) - { - var requiredLength = 5 + Name.Length; - if (destination.Length < requiredLength || !Id.TryFormat(destination[(3 + Name.Length)..^1], out int length)) - { - charsWritten = 0; - return false; - } - - "'; - - charsWritten = 4 + Name.Length + length; - return true; - } + public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => + Mention.TryFormatSlashCommand(destination, out charsWritten, Id, Name); } diff --git a/NetCord/Rest/ApplicationCommandOption.cs b/NetCord/Rest/ApplicationCommandOption.cs index 21a7515a3..06b6beecc 100644 --- a/NetCord/Rest/ApplicationCommandOption.cs +++ b/NetCord/Rest/ApplicationCommandOption.cs @@ -98,21 +98,6 @@ public ApplicationCommandOption(JsonModels.JsonApplicationCommandOption jsonMode public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); - public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) - { - var requiredLength = 5 + _fullName.Length; - if (destination.Length < requiredLength || !_parentId.TryFormat(destination[(3 + _fullName.Length)..^1], out int length)) - { - charsWritten = 0; - return false; - } - - "'; - - charsWritten = 4 + _fullName.Length + length; - return true; - } + public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => + Mention.TryFormatSlashCommand(destination, out charsWritten, _parentId, _fullName); } diff --git a/Tests/MentionTest/TryFormat.cs b/Tests/MentionTest/TryFormat.cs index 8bec51b45..8192d2768 100644 --- a/Tests/MentionTest/TryFormat.cs +++ b/Tests/MentionTest/TryFormat.cs @@ -13,6 +13,8 @@ public class TryFormat private static readonly IReadOnlyList _slashCommandMentions = _ids.SelectMany(id => [new(id, "name"), new(id, "name", "name2"), new(id, "name", "name2", "name3")]).ToArray(); + private static readonly IReadOnlyList<(ulong Id, string FullName)> _slashCommandMentionValues = _ids.SelectMany(id => [(id, "name"), (id, "name name2"), (id, "name name2 name3")]).ToArray(); + private static readonly IReadOnlyList _timestamps = _ids.SelectMany(id => { var timestamp = Snowflake.Timestamp(id); @@ -99,6 +101,15 @@ public void SlashCommand() static bool TryFormat(Span destination, out int charsWritten, SlashCommandMention value) => value.TryFormat(destination, out charsWritten); } + [TestMethod] + public void SlashCommandMention() + { + TestTryFormat(TryFormat, _slashCommandMentionValues, m => $""); + + static bool TryFormat(Span destination, out int charsWritten, (ulong Id, string FullName) value) => + Mention.TryFormatSlashCommand(destination, out charsWritten, value.Id, value.FullName); + } + [TestMethod] public void Timestamp() { From 31dd0e00bb74d5822dcff477481a0053914509f6 Mon Sep 17 00:00:00 2001 From: Hubert Date: Mon, 29 Jun 2026 16:25:07 +0200 Subject: [PATCH 2/7] refactor: Standardize mentions across lib --- .../RegisteredApplicationCommand.cs | 10 +- NetCord.Services/UserId.cs | 2 +- NetCord/Channels/Channel.cs | 2 +- NetCord/Mention.cs | 145 +++++++++++++++--- NetCord/Rest/ApplicationCommand.cs | 4 +- NetCord/Rest/ApplicationCommandOption.cs | 4 +- NetCord/Role.cs | 2 +- NetCord/SlashCommandMention.cs | 60 +------- NetCord/ThreadUser.cs | 2 +- NetCord/User.cs | 2 +- Tests/MentionTest/TryFormat.cs | 19 ++- 11 files changed, 158 insertions(+), 94 deletions(-) diff --git a/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs b/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs index 759982223..c16c56b0c 100644 --- a/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs +++ b/NetCord.Services/ApplicationCommands/RegisteredApplicationCommand.cs @@ -1,11 +1,3 @@ namespace NetCord.Services.ApplicationCommands; -public readonly record struct RegisteredApplicationCommand(ulong Id, ApplicationCommandInfo Info) : ISpanFormattable where TContext : IApplicationCommandContext -{ - public override string ToString() => $""; - - public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); - - public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => - Mention.TryFormatSlashCommand(destination, out charsWritten, Id, Info.Name); -} +public readonly record struct RegisteredApplicationCommand(ulong Id, ApplicationCommandInfo Info) where TContext : IApplicationCommandContext; diff --git a/NetCord.Services/UserId.cs b/NetCord.Services/UserId.cs index 9f342d7ad..b173488d5 100644 --- a/NetCord.Services/UserId.cs +++ b/NetCord.Services/UserId.cs @@ -26,7 +26,7 @@ public UserId(ulong id, User? user) : this(id) User = user; } - public override string ToString() => $"<@{Id}>"; + public override string ToString() => Mention.UserToString(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } diff --git a/NetCord/Channels/Channel.cs b/NetCord/Channels/Channel.cs index a2f0e00cf..f41a620ef 100644 --- a/NetCord/Channels/Channel.cs +++ b/NetCord/Channels/Channel.cs @@ -13,7 +13,7 @@ public abstract partial class Channel(JsonChannel jsonModel, RestClient client) Permissions IInteractionChannel.Permissions => _jsonModel.Permissions.GetValueOrDefault(); - public override string ToString() => $"<#{Id}>"; + public override string ToString() => Mention.ChannelToString(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatChannel(destination, out charsWritten, Id); diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index b00784627..fe9b0bfed 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -4,6 +4,11 @@ namespace NetCord; public static class Mention { + public static bool TryFormatUser(Span destination, out int charsWritten, ulong id) + { + return TryFormat(destination, out charsWritten, id, "<@", ">"); + } + public static bool TryParseUser(ReadOnlySpan mention, out ulong id) { if (mention is ['<', '@', _, .., '>']) @@ -27,6 +32,13 @@ public static ulong ParseUser(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } + public static string UserToString(ulong userId) => $"<@{userId}>"; + + public static bool TryFormatChannel(Span destination, out int charsWritten, ulong id) + { + return TryFormat(destination, out charsWritten, id, "<#", ">"); + } + public static bool TryParseChannel(ReadOnlySpan mention, out ulong id) { if (mention is ['<', '#', .., '>']) @@ -48,6 +60,13 @@ public static ulong ParseChannel(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } + public static string ChannelToString(ulong channelId) => $"<#{channelId}>"; + + public static bool TryFormatRole(Span destination, out int charsWritten, ulong id) + { + return TryFormat(destination, out charsWritten, id, "<@&", ">"); + } + public static bool TryParseRole(ReadOnlySpan mention, out ulong id) { if (mention is ['<', '@', '&', .., '>']) @@ -69,6 +88,23 @@ public static ulong ParseRole(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } + public static string RoleToString(ulong roleId) => $"<@&{roleId}>"; + + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) + { + return TryFormatApplicationCommandCore(destination, out charsWritten, id, fullName); + } + + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) + { + return TryFormatApplicationCommandCore(destination, out charsWritten, id, name, subCommandName); + } + + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) + { + return TryFormatApplicationCommandCore(destination, out charsWritten, id, name, subCommandGroupName, subCommandName); + } + public static bool TryParseSlashCommand(ReadOnlySpan mention, [MaybeNullWhen(false)] out SlashCommandMention result) { if (mention is ['<', '/', .., '>']) @@ -94,6 +130,7 @@ public static bool TryParseSlashCommand(ReadOnlySpan mention, [MaybeNullWh s[i] = names[..x].ToString(); names = names[(x + 1)..]; } + i++; } @@ -126,6 +163,33 @@ public static SlashCommandMention ParseSlashCommand(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } + public static string ApplicationCommandToString(string fullName, ulong id) + { + return string.Create( + length: 4 + fullName.Length + CountDigits(id), + state: (Id: id, FullName: fullName), + action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.FullName) + ); + } + + public static string ApplicationCommandToString(string name, string subCommandName, ulong id) + { + return string.Create( + length: 5 + name.Length + subCommandName.Length + CountDigits(id), + state: (Id: id, Name: name, SubCommandName: subCommandName), + action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.Name, state.SubCommandName) + ); + } + + public static string ApplicationCommandToString(string name, string subCommandGroupName, string subCommandName, ulong id) + { + return string.Create( + length: 6 + name.Length + subCommandGroupName.Length + subCommandName.Length + CountDigits(id), + state: (Id: id, Name: name, SubCommandGroupName: subCommandGroupName, SubCommandName: subCommandName), + action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.Name, state.SubCommandGroupName, state.SubCommandName) + ); + } + public static bool TryParseTimestamp(ReadOnlySpan mention, out Timestamp result) { return Timestamp.TryParse(mention, out result); @@ -152,50 +216,89 @@ public static GuildNavigation ParseGuildNavigation(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static bool TryFormatUser(Span destination, out int charsWritten, ulong id) + private static bool TryFormat(Span destination, out int charsWritten, ulong id, ReadOnlySpan prefix, ReadOnlySpan suffix) { - return TryFormat(destination, out charsWritten, id, "<@", ">"); - } + if (destination.Length <= prefix.Length + suffix.Length || !id.TryFormat(destination[prefix.Length..^suffix.Length], out int length)) + { + charsWritten = 0; + return false; + } - public static bool TryFormatChannel(Span destination, out int charsWritten, ulong id) - { - return TryFormat(destination, out charsWritten, id, "<#", ">"); + prefix.CopyTo(destination); + suffix.CopyTo(destination[(prefix.Length + length)..]); + charsWritten = prefix.Length + length + suffix.Length; + return true; } - public static bool TryFormatRole(Span destination, out int charsWritten, ulong id) + private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) { - return TryFormat(destination, out charsWritten, id, "<@&", ">"); + var pathLength = fullName.Length; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + { + charsWritten = 0; + return false; + } + + "'; + charsWritten = idOffset + length + 1; + return true; } - public static bool TryFormatSlashCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) + private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) { - var requiredLength = 5 + fullName.Length; - if (destination.Length < requiredLength || !id.TryFormat(destination[(3 + fullName.Length)..^1], out int length)) + var nameLength = name.Length; + var subCommandNameLength = subCommandName.Length; + var pathLength = nameLength + subCommandNameLength + 1; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) { charsWritten = 0; return false; } "'; - - charsWritten = 4 + fullName.Length + length; + name.CopyTo(destination[2..]); + destination[2 + nameLength] = ' '; + subCommandName.CopyTo(destination[(3 + nameLength)..]); + destination[3 + nameLength + subCommandNameLength] = ':'; + destination[idOffset + length] = '>'; + charsWritten = idOffset + length + 1; return true; } - private static bool TryFormat(Span destination, out int charsWritten, ulong id, ReadOnlySpan prefix, ReadOnlySpan suffix) + private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) { - if (destination.Length <= prefix.Length + suffix.Length || !id.TryFormat(destination[prefix.Length..^suffix.Length], out int length)) + var nameLength = name.Length; + var subCommandGroupNameLength = subCommandGroupName.Length; + var subCommandNameLength = subCommandName.Length; + var pathLength = nameLength + subCommandGroupNameLength + subCommandNameLength + 2; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) { charsWritten = 0; return false; } - prefix.CopyTo(destination); - suffix.CopyTo(destination[(prefix.Length + length)..]); - charsWritten = prefix.Length + length + suffix.Length; + "'; + charsWritten = idOffset + length + 1; return true; } + + private static int CountDigits(ulong value) + { + var result = 1; + while ((value /= 10) != 0) result++; + return result; + } } diff --git a/NetCord/Rest/ApplicationCommand.cs b/NetCord/Rest/ApplicationCommand.cs index cd70f1151..87709a481 100644 --- a/NetCord/Rest/ApplicationCommand.cs +++ b/NetCord/Rest/ApplicationCommand.cs @@ -67,8 +67,8 @@ public partial class ApplicationCommand(JsonModels.JsonApplicationCommand jsonMo /// public ulong Version => _jsonModel.Version; - public override string ToString() => $""; + public override string ToString() => Mention.ApplicationCommandToString(Name, Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => - Mention.TryFormatSlashCommand(destination, out charsWritten, Id, Name); + Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, Name); } diff --git a/NetCord/Rest/ApplicationCommandOption.cs b/NetCord/Rest/ApplicationCommandOption.cs index 06b6beecc..26fcb17b9 100644 --- a/NetCord/Rest/ApplicationCommandOption.cs +++ b/NetCord/Rest/ApplicationCommandOption.cs @@ -94,10 +94,10 @@ public ApplicationCommandOption(JsonModels.JsonApplicationCommandOption jsonMode Options = options.Select(o => new ApplicationCommandOption(o, _fullName, _parentId)).ToArray(); } - public override string ToString() => $""; + public override string ToString() => Mention.ApplicationCommandToString(_fullName, _parentId); public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); public bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => - Mention.TryFormatSlashCommand(destination, out charsWritten, _parentId, _fullName); + Mention.TryFormatApplicationCommand(destination, out charsWritten, _parentId, _fullName); } diff --git a/NetCord/Role.cs b/NetCord/Role.cs index 0107ac91b..6238b1fc8 100644 --- a/NetCord/Role.cs +++ b/NetCord/Role.cs @@ -103,7 +103,7 @@ public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(client) /// An pointing to the role's icon. If the role does not have one set, returns . public ImageUrl? GetIconUrl(ImageFormat format) => IconHash is string hash ? ImageUrl.RoleIcon(Id, hash, format) : null; - public override string ToString() => $"<@&{Id}>"; + public override string ToString() => Mention.RoleToString(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatRole(destination, out charsWritten, Id); } diff --git a/NetCord/SlashCommandMention.cs b/NetCord/SlashCommandMention.cs index 1185d7bb1..4653529ba 100644 --- a/NetCord/SlashCommandMention.cs +++ b/NetCord/SlashCommandMention.cs @@ -27,12 +27,12 @@ public override string ToString() if (subCommandGroupName is null) { if (subCommandName is null) - return $""; + return Mention.ApplicationCommandToString(Name, Id); else - return $""; + return Mention.ApplicationCommandToString(Name, subCommandName, Id); } else - return $""; + return Mention.ApplicationCommandToString(Name, subCommandGroupName, subCommandName!, Id); } public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) @@ -44,60 +44,12 @@ public override bool TryFormat(Span destination, out int charsWritten, Rea if (subCommandGroupName is null) { if (subCommandName is null) - { - if (destination.Length < 5 + name.Length || !Id.TryFormat(destination[(3 + name.Length)..^1], out var length)) - { - charsWritten = 0; - return false; - } - - "'; - - charsWritten = 4 + name.Length + length; - return true; - } + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name); else - { - if (destination.Length < 6 + name.Length + subCommandName.Length || !Id.TryFormat(destination[(4 + name.Length + subCommandName.Length)..^1], out var length)) - { - charsWritten = 0; - return false; - } - - "'; - - charsWritten = 5 + name.Length + subCommandName.Length + length; - return true; - } + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name, subCommandName); } else - { - if (destination.Length < 7 + name.Length + subCommandGroupName.Length + subCommandName!.Length || !Id.TryFormat(destination[(5 + name.Length + subCommandGroupName.Length + subCommandName.Length)..^1], out var length)) - { - charsWritten = 0; - return false; - } - - "'; - - charsWritten = 6 + name.Length + subCommandGroupName.Length + subCommandName.Length + length; - return true; - } + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name, subCommandGroupName, subCommandName!); } public static bool operator ==(SlashCommandMention? left, SlashCommandMention? right) => left is null ? right is null : Equals(left, right); diff --git a/NetCord/ThreadUser.cs b/NetCord/ThreadUser.cs index bc9867e1a..15a61f64e 100644 --- a/NetCord/ThreadUser.cs +++ b/NetCord/ThreadUser.cs @@ -11,7 +11,7 @@ public class ThreadUser(JsonModels.JsonThreadUser jsonModel, RestClient client) public DateTimeOffset JoinTimestamp => jsonModel.JoinTimestamp; public int Flags => jsonModel.Flags; - public override string ToString() => $"<@{Id}>"; + public override string ToString() => Mention.UserToString(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } diff --git a/NetCord/User.cs b/NetCord/User.cs index d9223c426..088fe7f88 100644 --- a/NetCord/User.cs +++ b/NetCord/User.cs @@ -256,7 +256,7 @@ public User(JsonModels.JsonUser jsonModel, RestClient client) : base(client) /// /// Converts the ID of this user into its string representation, using Discord's mention syntax (<@803169206115237908>). /// - public override string ToString() => $"<@{Id}>"; + public override string ToString() => Mention.UserToString(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } diff --git a/Tests/MentionTest/TryFormat.cs b/Tests/MentionTest/TryFormat.cs index 8192d2768..2c4a23c23 100644 --- a/Tests/MentionTest/TryFormat.cs +++ b/Tests/MentionTest/TryFormat.cs @@ -107,7 +107,24 @@ public void SlashCommandMention() TestTryFormat(TryFormat, _slashCommandMentionValues, m => $""); static bool TryFormat(Span destination, out int charsWritten, (ulong Id, string FullName) value) => - Mention.TryFormatSlashCommand(destination, out charsWritten, value.Id, value.FullName); + Mention.TryFormatApplicationCommand(destination, out charsWritten, value.Id, value.FullName); + } + + [TestMethod] + public void ApplicationCommandMentionSegments() + { + TestTryFormat(TryFormat, _slashCommandMentions, m => m.ToString()); + + static bool TryFormat(Span destination, out int charsWritten, SlashCommandMention value) + { + if (value.SubCommandGroupName is { } subCommandGroupName) + return Mention.TryFormatApplicationCommand(destination, out charsWritten, value.Id, value.Name, subCommandGroupName, value.SubCommandName!); + + if (value.SubCommandName is { } subCommandName) + return Mention.TryFormatApplicationCommand(destination, out charsWritten, value.Id, value.Name, subCommandName); + + return Mention.TryFormatApplicationCommand(destination, out charsWritten, value.Id, value.Name); + } } [TestMethod] From a9dc955c92cf2753de61d90a9447b68b1b3e24a1 Mon Sep 17 00:00:00 2001 From: Hubert Date: Mon, 29 Jun 2026 18:01:08 +0200 Subject: [PATCH 3/7] fix: Missed variable --- NetCord/SlashCommandMention.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/NetCord/SlashCommandMention.cs b/NetCord/SlashCommandMention.cs index 4653529ba..d497d22e3 100644 --- a/NetCord/SlashCommandMention.cs +++ b/NetCord/SlashCommandMention.cs @@ -39,17 +39,16 @@ public override bool TryFormat(Span destination, out int charsWritten, Rea { var subCommandGroupName = SubCommandGroupName; var subCommandName = SubCommandName; - var name = Name; if (subCommandGroupName is null) { if (subCommandName is null) - return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name); + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, Name); else - return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name, subCommandName); + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, Name, subCommandName); } else - return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, name, subCommandGroupName, subCommandName!); + return Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, Name, subCommandGroupName, subCommandName!); } public static bool operator ==(SlashCommandMention? left, SlashCommandMention? right) => left is null ? right is null : Equals(left, right); From 532860d491097b10ce1b860dc4bed6d90e68d818 Mon Sep 17 00:00:00 2001 From: Hubert Date: Mon, 29 Jun 2026 18:26:49 +0200 Subject: [PATCH 4/7] up --- NetCord.Services/UserId.cs | 2 +- NetCord/Channels/Channel.cs | 2 +- NetCord/Mention.cs | 12 ++++++------ NetCord/Rest/ApplicationCommand.cs | 2 +- NetCord/Rest/ApplicationCommandOption.cs | 2 +- NetCord/Role.cs | 2 +- NetCord/SlashCommandMention.cs | 6 +++--- NetCord/ThreadUser.cs | 2 +- NetCord/User.cs | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/NetCord.Services/UserId.cs b/NetCord.Services/UserId.cs index b173488d5..e855413e6 100644 --- a/NetCord.Services/UserId.cs +++ b/NetCord.Services/UserId.cs @@ -26,7 +26,7 @@ public UserId(ulong id, User? user) : this(id) User = user; } - public override string ToString() => Mention.UserToString(Id); + public override string ToString() => Mention.User(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } diff --git a/NetCord/Channels/Channel.cs b/NetCord/Channels/Channel.cs index f41a620ef..8a83172c4 100644 --- a/NetCord/Channels/Channel.cs +++ b/NetCord/Channels/Channel.cs @@ -13,7 +13,7 @@ public abstract partial class Channel(JsonChannel jsonModel, RestClient client) Permissions IInteractionChannel.Permissions => _jsonModel.Permissions.GetValueOrDefault(); - public override string ToString() => Mention.ChannelToString(Id); + public override string ToString() => Mention.Channel(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatChannel(destination, out charsWritten, Id); diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index fe9b0bfed..ddb176fcd 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -32,7 +32,7 @@ public static ulong ParseUser(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string UserToString(ulong userId) => $"<@{userId}>"; + public static string User(ulong userId) => $"<@{userId}>"; public static bool TryFormatChannel(Span destination, out int charsWritten, ulong id) { @@ -60,7 +60,7 @@ public static ulong ParseChannel(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string ChannelToString(ulong channelId) => $"<#{channelId}>"; + public static string Channel(ulong channelId) => $"<#{channelId}>"; public static bool TryFormatRole(Span destination, out int charsWritten, ulong id) { @@ -88,7 +88,7 @@ public static ulong ParseRole(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string RoleToString(ulong roleId) => $"<@&{roleId}>"; + public static string Role(ulong roleId) => $"<@&{roleId}>"; public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) { @@ -163,7 +163,7 @@ public static SlashCommandMention ParseSlashCommand(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string ApplicationCommandToString(string fullName, ulong id) + public static string ApplicationCommand(string fullName, ulong id) { return string.Create( length: 4 + fullName.Length + CountDigits(id), @@ -172,7 +172,7 @@ public static string ApplicationCommandToString(string fullName, ulong id) ); } - public static string ApplicationCommandToString(string name, string subCommandName, ulong id) + public static string ApplicationCommand(string name, string subCommandName, ulong id) { return string.Create( length: 5 + name.Length + subCommandName.Length + CountDigits(id), @@ -181,7 +181,7 @@ public static string ApplicationCommandToString(string name, string subCommandNa ); } - public static string ApplicationCommandToString(string name, string subCommandGroupName, string subCommandName, ulong id) + public static string ApplicationCommand(string name, string subCommandGroupName, string subCommandName, ulong id) { return string.Create( length: 6 + name.Length + subCommandGroupName.Length + subCommandName.Length + CountDigits(id), diff --git a/NetCord/Rest/ApplicationCommand.cs b/NetCord/Rest/ApplicationCommand.cs index 87709a481..56e02fd90 100644 --- a/NetCord/Rest/ApplicationCommand.cs +++ b/NetCord/Rest/ApplicationCommand.cs @@ -67,7 +67,7 @@ public partial class ApplicationCommand(JsonModels.JsonApplicationCommand jsonMo /// public ulong Version => _jsonModel.Version; - public override string ToString() => Mention.ApplicationCommandToString(Name, Id); + public override string ToString() => Mention.ApplicationCommand(Name, Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatApplicationCommand(destination, out charsWritten, Id, Name); diff --git a/NetCord/Rest/ApplicationCommandOption.cs b/NetCord/Rest/ApplicationCommandOption.cs index 26fcb17b9..60237aaf2 100644 --- a/NetCord/Rest/ApplicationCommandOption.cs +++ b/NetCord/Rest/ApplicationCommandOption.cs @@ -94,7 +94,7 @@ public ApplicationCommandOption(JsonModels.JsonApplicationCommandOption jsonMode Options = options.Select(o => new ApplicationCommandOption(o, _fullName, _parentId)).ToArray(); } - public override string ToString() => Mention.ApplicationCommandToString(_fullName, _parentId); + public override string ToString() => Mention.ApplicationCommand(_fullName, _parentId); public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); diff --git a/NetCord/Role.cs b/NetCord/Role.cs index 6238b1fc8..7a23687f9 100644 --- a/NetCord/Role.cs +++ b/NetCord/Role.cs @@ -103,7 +103,7 @@ public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(client) /// An pointing to the role's icon. If the role does not have one set, returns . public ImageUrl? GetIconUrl(ImageFormat format) => IconHash is string hash ? ImageUrl.RoleIcon(Id, hash, format) : null; - public override string ToString() => Mention.RoleToString(Id); + public override string ToString() => Mention.Role(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatRole(destination, out charsWritten, Id); } diff --git a/NetCord/SlashCommandMention.cs b/NetCord/SlashCommandMention.cs index d497d22e3..fb167cbbe 100644 --- a/NetCord/SlashCommandMention.cs +++ b/NetCord/SlashCommandMention.cs @@ -27,12 +27,12 @@ public override string ToString() if (subCommandGroupName is null) { if (subCommandName is null) - return Mention.ApplicationCommandToString(Name, Id); + return Mention.ApplicationCommand(Name, Id); else - return Mention.ApplicationCommandToString(Name, subCommandName, Id); + return Mention.ApplicationCommand(Name, subCommandName, Id); } else - return Mention.ApplicationCommandToString(Name, subCommandGroupName, subCommandName!, Id); + return Mention.ApplicationCommand(Name, subCommandGroupName, subCommandName!, Id); } public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) diff --git a/NetCord/ThreadUser.cs b/NetCord/ThreadUser.cs index 15a61f64e..a7d86bbac 100644 --- a/NetCord/ThreadUser.cs +++ b/NetCord/ThreadUser.cs @@ -11,7 +11,7 @@ public class ThreadUser(JsonModels.JsonThreadUser jsonModel, RestClient client) public DateTimeOffset JoinTimestamp => jsonModel.JoinTimestamp; public int Flags => jsonModel.Flags; - public override string ToString() => Mention.UserToString(Id); + public override string ToString() => Mention.User(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } diff --git a/NetCord/User.cs b/NetCord/User.cs index 088fe7f88..fd0342855 100644 --- a/NetCord/User.cs +++ b/NetCord/User.cs @@ -256,7 +256,7 @@ public User(JsonModels.JsonUser jsonModel, RestClient client) : base(client) /// /// Converts the ID of this user into its string representation, using Discord's mention syntax (<@803169206115237908>). /// - public override string ToString() => Mention.UserToString(Id); + public override string ToString() => Mention.User(Id); public override bool TryFormat(Span destination, out int charsWritten, ReadOnlySpan format = default, IFormatProvider? provider = null) => Mention.TryFormatUser(destination, out charsWritten, Id); } From f0b90ed8aee4a6a00a6653694b3a1264bcb6271f Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 30 Jun 2026 19:38:55 +0200 Subject: [PATCH 5/7] Address feedback --- NetCord/Mention.cs | 51 ++++++------------------ NetCord/Rest/ApplicationCommandOption.cs | 2 +- Tests/MentionTest/TryFormat.cs | 11 ----- 3 files changed, 14 insertions(+), 50 deletions(-) diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index ddb176fcd..07a7c40ed 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -4,6 +4,8 @@ namespace NetCord; public static class Mention { + public static string User(ulong userId) => $"<@{userId}>"; + public static bool TryFormatUser(Span destination, out int charsWritten, ulong id) { return TryFormat(destination, out charsWritten, id, "<@", ">"); @@ -32,7 +34,7 @@ public static ulong ParseUser(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string User(ulong userId) => $"<@{userId}>"; + public static string Channel(ulong channelId) => $"<#{channelId}>"; public static bool TryFormatChannel(Span destination, out int charsWritten, ulong id) { @@ -60,7 +62,7 @@ public static ulong ParseChannel(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string Channel(ulong channelId) => $"<#{channelId}>"; + public static string Role(ulong roleId) => $"<@&{roleId}>"; public static bool TryFormatRole(Span destination, out int charsWritten, ulong id) { @@ -88,11 +90,17 @@ public static ulong ParseRole(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string Role(ulong roleId) => $"<@&{roleId}>"; + public static string ApplicationCommand(string name, ulong id) => ApplicationCommandCore(name, id); + + internal static string ApplicationCommandCore(string fullName, ulong id) => $""; + + public static string ApplicationCommand(string name, string subCommandName, ulong id) => $""; - public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) + public static string ApplicationCommand(string name, string subCommandGroupName, string subCommandName, ulong id) => $""; + + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name) { - return TryFormatApplicationCommandCore(destination, out charsWritten, id, fullName); + return TryFormatApplicationCommandCore(destination, out charsWritten, id, name); } public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) @@ -163,33 +171,6 @@ public static SlashCommandMention ParseSlashCommand(ReadOnlySpan mention) throw new FormatException("Cannot parse the mention."); } - public static string ApplicationCommand(string fullName, ulong id) - { - return string.Create( - length: 4 + fullName.Length + CountDigits(id), - state: (Id: id, FullName: fullName), - action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.FullName) - ); - } - - public static string ApplicationCommand(string name, string subCommandName, ulong id) - { - return string.Create( - length: 5 + name.Length + subCommandName.Length + CountDigits(id), - state: (Id: id, Name: name, SubCommandName: subCommandName), - action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.Name, state.SubCommandName) - ); - } - - public static string ApplicationCommand(string name, string subCommandGroupName, string subCommandName, ulong id) - { - return string.Create( - length: 6 + name.Length + subCommandGroupName.Length + subCommandName.Length + CountDigits(id), - state: (Id: id, Name: name, SubCommandGroupName: subCommandGroupName, SubCommandName: subCommandName), - action: static (destination, state) => TryFormatApplicationCommand(destination, out _, state.Id, state.Name, state.SubCommandGroupName, state.SubCommandName) - ); - } - public static bool TryParseTimestamp(ReadOnlySpan mention, out Timestamp result) { return Timestamp.TryParse(mention, out result); @@ -295,10 +276,4 @@ private static bool TryFormatApplicationCommandCore(Span destination, out return true; } - private static int CountDigits(ulong value) - { - var result = 1; - while ((value /= 10) != 0) result++; - return result; - } } diff --git a/NetCord/Rest/ApplicationCommandOption.cs b/NetCord/Rest/ApplicationCommandOption.cs index 60237aaf2..139b07a08 100644 --- a/NetCord/Rest/ApplicationCommandOption.cs +++ b/NetCord/Rest/ApplicationCommandOption.cs @@ -94,7 +94,7 @@ public ApplicationCommandOption(JsonModels.JsonApplicationCommandOption jsonMode Options = options.Select(o => new ApplicationCommandOption(o, _fullName, _parentId)).ToArray(); } - public override string ToString() => Mention.ApplicationCommand(_fullName, _parentId); + public override string ToString() => Mention.ApplicationCommandCore(_fullName, _parentId); public string ToString(string? format, IFormatProvider? formatProvider) => ToString(); diff --git a/Tests/MentionTest/TryFormat.cs b/Tests/MentionTest/TryFormat.cs index 2c4a23c23..cd379f59b 100644 --- a/Tests/MentionTest/TryFormat.cs +++ b/Tests/MentionTest/TryFormat.cs @@ -13,8 +13,6 @@ public class TryFormat private static readonly IReadOnlyList _slashCommandMentions = _ids.SelectMany(id => [new(id, "name"), new(id, "name", "name2"), new(id, "name", "name2", "name3")]).ToArray(); - private static readonly IReadOnlyList<(ulong Id, string FullName)> _slashCommandMentionValues = _ids.SelectMany(id => [(id, "name"), (id, "name name2"), (id, "name name2 name3")]).ToArray(); - private static readonly IReadOnlyList _timestamps = _ids.SelectMany(id => { var timestamp = Snowflake.Timestamp(id); @@ -101,15 +99,6 @@ public void SlashCommand() static bool TryFormat(Span destination, out int charsWritten, SlashCommandMention value) => value.TryFormat(destination, out charsWritten); } - [TestMethod] - public void SlashCommandMention() - { - TestTryFormat(TryFormat, _slashCommandMentionValues, m => $""); - - static bool TryFormat(Span destination, out int charsWritten, (ulong Id, string FullName) value) => - Mention.TryFormatApplicationCommand(destination, out charsWritten, value.Id, value.FullName); - } - [TestMethod] public void ApplicationCommandMentionSegments() { From bf7081d02b765256e3205c0bf9aa1ca15bcb3d5d Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 30 Jun 2026 21:17:48 +0200 Subject: [PATCH 6/7] Inline privates --- NetCord/Mention.cs | 122 ++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 69 deletions(-) diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index 07a7c40ed..ea8265310 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -100,17 +100,67 @@ public static ulong ParseRole(ReadOnlySpan mention) public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name) { - return TryFormatApplicationCommandCore(destination, out charsWritten, id, name); + var pathLength = name.Length; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + { + charsWritten = 0; + return false; + } + + "'; + charsWritten = idOffset + length + 1; + return true; } public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) { - return TryFormatApplicationCommandCore(destination, out charsWritten, id, name, subCommandName); + var nameLength = name.Length; + var subCommandNameLength = subCommandName.Length; + var pathLength = nameLength + subCommandNameLength + 1; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + { + charsWritten = 0; + return false; + } + + "'; + charsWritten = idOffset + length + 1; + return true; } public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) { - return TryFormatApplicationCommandCore(destination, out charsWritten, id, name, subCommandGroupName, subCommandName); + var nameLength = name.Length; + var subCommandGroupNameLength = subCommandGroupName.Length; + var subCommandNameLength = subCommandName.Length; + var pathLength = nameLength + subCommandGroupNameLength + subCommandNameLength + 2; + var idOffset = 3 + pathLength; + if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + { + charsWritten = 0; + return false; + } + + "'; + charsWritten = idOffset + length + 1; + return true; } public static bool TryParseSlashCommand(ReadOnlySpan mention, [MaybeNullWhen(false)] out SlashCommandMention result) @@ -210,70 +260,4 @@ private static bool TryFormat(Span destination, out int charsWritten, ulon charsWritten = prefix.Length + length + suffix.Length; return true; } - - private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan fullName) - { - var pathLength = fullName.Length; - var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) - { - charsWritten = 0; - return false; - } - - "'; - charsWritten = idOffset + length + 1; - return true; - } - - private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) - { - var nameLength = name.Length; - var subCommandNameLength = subCommandName.Length; - var pathLength = nameLength + subCommandNameLength + 1; - var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) - { - charsWritten = 0; - return false; - } - - "'; - charsWritten = idOffset + length + 1; - return true; - } - - private static bool TryFormatApplicationCommandCore(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) - { - var nameLength = name.Length; - var subCommandGroupNameLength = subCommandGroupName.Length; - var subCommandNameLength = subCommandName.Length; - var pathLength = nameLength + subCommandGroupNameLength + subCommandNameLength + 2; - var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) - { - charsWritten = 0; - return false; - } - - "'; - charsWritten = idOffset + length + 1; - return true; - } - } From ae642e82a95b212cc7f756bd02801d72f31ad1b5 Mon Sep 17 00:00:00 2001 From: Hubert Date: Tue, 14 Jul 2026 17:48:09 +0200 Subject: [PATCH 7/7] Rename params --- NetCord/Mention.cs | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/NetCord/Mention.cs b/NetCord/Mention.cs index ea8265310..ff8e4ab57 100644 --- a/NetCord/Mention.cs +++ b/NetCord/Mention.cs @@ -6,29 +6,29 @@ public static class Mention { public static string User(ulong userId) => $"<@{userId}>"; - public static bool TryFormatUser(Span destination, out int charsWritten, ulong id) + public static bool TryFormatUser(Span destination, out int charsWritten, ulong userId) { - return TryFormat(destination, out charsWritten, id, "<@", ">"); + return TryFormat(destination, out charsWritten, userId, "<@", ">"); } - public static bool TryParseUser(ReadOnlySpan mention, out ulong id) + public static bool TryParseUser(ReadOnlySpan mention, out ulong userId) { if (mention is ['<', '@', _, .., '>']) { mention = mention[2] is '!' ? mention[3..^1] : mention[2..^1]; - if (Snowflake.TryParse(mention, out id)) + if (Snowflake.TryParse(mention, out userId)) return true; } else - id = default; + userId = default; return false; } - public static ulong ParseUser(ReadOnlySpan mention) + public static ulong ParseUser(ReadOnlySpan userMention) { - if (TryParseUser(mention, out ulong id)) + if (TryParseUser(userMention, out ulong id)) return id; else throw new FormatException("Cannot parse the mention."); @@ -36,27 +36,27 @@ public static ulong ParseUser(ReadOnlySpan mention) public static string Channel(ulong channelId) => $"<#{channelId}>"; - public static bool TryFormatChannel(Span destination, out int charsWritten, ulong id) + public static bool TryFormatChannel(Span destination, out int charsWritten, ulong channelId) { - return TryFormat(destination, out charsWritten, id, "<#", ">"); + return TryFormat(destination, out charsWritten, channelId, "<#", ">"); } - public static bool TryParseChannel(ReadOnlySpan mention, out ulong id) + public static bool TryParseChannel(ReadOnlySpan mention, out ulong channelId) { if (mention is ['<', '#', .., '>']) { - if (Snowflake.TryParse(mention[2..^1], out id)) + if (Snowflake.TryParse(mention[2..^1], out channelId)) return true; } else - id = default; + channelId = default; return false; } - public static ulong ParseChannel(ReadOnlySpan mention) + public static ulong ParseChannel(ReadOnlySpan channelMention) { - if (TryParseChannel(mention, out ulong id)) + if (TryParseChannel(channelMention, out ulong id)) return id; else throw new FormatException("Cannot parse the mention."); @@ -64,45 +64,45 @@ public static ulong ParseChannel(ReadOnlySpan mention) public static string Role(ulong roleId) => $"<@&{roleId}>"; - public static bool TryFormatRole(Span destination, out int charsWritten, ulong id) + public static bool TryFormatRole(Span destination, out int charsWritten, ulong roleId) { - return TryFormat(destination, out charsWritten, id, "<@&", ">"); + return TryFormat(destination, out charsWritten, roleId, "<@&", ">"); } - public static bool TryParseRole(ReadOnlySpan mention, out ulong id) + public static bool TryParseRole(ReadOnlySpan mention, out ulong roleId) { if (mention is ['<', '@', '&', .., '>']) { - if (Snowflake.TryParse(mention[3..^1], out id)) + if (Snowflake.TryParse(mention[3..^1], out roleId)) return true; } else - id = default; + roleId = default; return false; } - public static ulong ParseRole(ReadOnlySpan mention) + public static ulong ParseRole(ReadOnlySpan roleMention) { - if (TryParseRole(mention, out ulong id)) + if (TryParseRole(roleMention, out ulong id)) return id; else throw new FormatException("Cannot parse the mention."); } - public static string ApplicationCommand(string name, ulong id) => ApplicationCommandCore(name, id); + public static string ApplicationCommand(string name, ulong commandId) => ApplicationCommandCore(name, commandId); - internal static string ApplicationCommandCore(string fullName, ulong id) => $""; + internal static string ApplicationCommandCore(string fullName, ulong commandId) => $""; - public static string ApplicationCommand(string name, string subCommandName, ulong id) => $""; + public static string ApplicationCommand(string name, string subCommandName, ulong commandId) => $""; - public static string ApplicationCommand(string name, string subCommandGroupName, string subCommandName, ulong id) => $""; + public static string ApplicationCommand(string name, string subCommandGroupName, string subCommandName, ulong commandId) => $""; - public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name) + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong commandId, ReadOnlySpan name) { var pathLength = name.Length; var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + if (destination.Length < 5 + pathLength || !commandId.TryFormat(destination[idOffset..^1], out int length)) { charsWritten = 0; return false; @@ -116,13 +116,13 @@ public static bool TryFormatApplicationCommand(Span destination, out int c return true; } - public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandName) + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong commandId, ReadOnlySpan name, ReadOnlySpan subCommandName) { var nameLength = name.Length; var subCommandNameLength = subCommandName.Length; var pathLength = nameLength + subCommandNameLength + 1; var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + if (destination.Length < 5 + pathLength || !commandId.TryFormat(destination[idOffset..^1], out int length)) { charsWritten = 0; return false; @@ -138,14 +138,14 @@ public static bool TryFormatApplicationCommand(Span destination, out int c return true; } - public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong id, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) + public static bool TryFormatApplicationCommand(Span destination, out int charsWritten, ulong commandId, ReadOnlySpan name, ReadOnlySpan subCommandGroupName, ReadOnlySpan subCommandName) { var nameLength = name.Length; var subCommandGroupNameLength = subCommandGroupName.Length; var subCommandNameLength = subCommandName.Length; var pathLength = nameLength + subCommandGroupNameLength + subCommandNameLength + 2; var idOffset = 3 + pathLength; - if (destination.Length < 5 + pathLength || !id.TryFormat(destination[idOffset..^1], out int length)) + if (destination.Length < 5 + pathLength || !commandId.TryFormat(destination[idOffset..^1], out int length)) { charsWritten = 0; return false; @@ -213,9 +213,9 @@ public static bool TryParseSlashCommand(ReadOnlySpan mention, [MaybeNullWh return false; } - public static SlashCommandMention ParseSlashCommand(ReadOnlySpan mention) + public static SlashCommandMention ParseSlashCommand(ReadOnlySpan slashCommandMention) { - if (TryParseSlashCommand(mention, out var result)) + if (TryParseSlashCommand(slashCommandMention, out var result)) return result; else throw new FormatException("Cannot parse the mention.");