Skip to content

Commit 51e3239

Browse files
committed
add emoji support to select menu options
1 parent 2e0b57f commit 51e3239

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/Discord.Net.Interactions/Attributes/Modals/ModalSelectMenuOptionAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public class ModalSelectMenuOptionAttribute : Attribute
2929
/// <summary>
3030
/// Gets or sets the emote of the option.
3131
/// </summary>
32+
/// <remarks>
33+
/// Can be either an <see cref="Emoji"/> or an <see cref="Discord.Emote"/>
34+
/// </remarks>
3235
public string Emote { get; set; }
3336

3437
/// <summary>
@@ -42,7 +45,7 @@ public class ModalSelectMenuOptionAttribute : Attribute
4245
/// <param name="label">Label of the option.</param>
4346
/// <param name="value">Value of the option.</param>
4447
/// <param name="description">Description of the option.</param>
45-
/// <param name="emote">Emote of the option.</param>
48+
/// <param name="emote">Emote of the option. Can be either an <see cref="Emoji"/> or an <see cref="Discord.Emote"/></param>
4649
/// <param name="isDefault">Whether the option is selected by default</param>
4750
public ModalSelectMenuOptionAttribute(string label, string value, string description = null, string emote = null, bool isDefault = false)
4851
{

src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,16 +716,18 @@ private static void BuildSelectMenuComponent(SelectMenuComponentBuilder builder,
716716
builder.Description = inputLabel.Description;
717717
break;
718718
case ModalSelectMenuOptionAttribute selectMenuOption:
719+
Emoji emoji = null;
720+
Emote emote = null;
719721

720-
if (!Emote.TryParse(selectMenuOption.Emote, out var emote) && !string.IsNullOrEmpty(selectMenuOption.Emote))
721-
throw new ArgumentException($"Invalid emote format on {propertyInfo.DeclaringType} modal, {propertyInfo.Name} property");
722+
if (!string.IsNullOrEmpty(selectMenuOption?.Emote) && !(Emote.TryParse(selectMenuOption.Emote, out emote) || Emoji.TryParse(selectMenuOption.Emote, out emoji)))
723+
throw new ArgumentException($"Unable to parse {selectMenuOption.Emote} of {propertyInfo.DeclaringType}.{propertyInfo.Name} into an {typeof(Emote).Name} or an {typeof(Emoji).Name}");
722724

723725
builder.AddOption(new SelectMenuOptionBuilder
724726
{
725727
Label = selectMenuOption.Label,
726728
Description = selectMenuOption.Description,
727729
Value = selectMenuOption.Value,
728-
Emote = emote,
730+
Emote = emote != null ? emote : emoji,
729731
IsDefault = selectMenuOption.IsDefault
730732
});
731733
break;

src/Discord.Net.Interactions/TypeConverters/ModalComponents/EnumModalComponentConverter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ public EnumModalComponentConverter()
2222
_options = members.Select(x =>
2323
{
2424
var selectMenuOptionAttr = x.GetCustomAttribute<SelectMenuOptionAttribute>();
25+
26+
Emoji emoji = null;
27+
Emote emote = null;
28+
29+
if (!string.IsNullOrEmpty(selectMenuOptionAttr?.Emote) && !(Emote.TryParse(selectMenuOptionAttr.Emote, out emote) || Emoji.TryParse(selectMenuOptionAttr.Emote, out emoji)))
30+
throw new ArgumentException($"Unable to parse {selectMenuOptionAttr.Emote} of {x.DeclaringType.Name}.{x.Name} into an {typeof(Emote).Name} or an {typeof(Emoji).Name}");
31+
32+
2533
var hideAttr = x.GetCustomAttribute<HideAttribute>();
2634
Predicate<IDiscordInteraction> predicate = hideAttr != null ? hideAttr.Predicate : null;
27-
return (new SelectMenuOptionBuilder(x.GetCustomAttribute<ChoiceDisplayAttribute>()?.Name ?? x.Name, x.Name, selectMenuOptionAttr?.Description, selectMenuOptionAttr?.Emote != null ? Emote.Parse(selectMenuOptionAttr?.Emote) : null, selectMenuOptionAttr?.IsDefault), predicate);
35+
return (new SelectMenuOptionBuilder(x.GetCustomAttribute<ChoiceDisplayAttribute>()?.Name ?? x.Name, x.Name, selectMenuOptionAttr?.Description, emote != null ? emote : emoji, selectMenuOptionAttr?.IsDefault), predicate);
2836
}).ToImmutableArray();
2937
}
3038

@@ -82,5 +90,8 @@ public class SelectMenuOptionAttribute : Attribute
8290
/// <summary>
8391
/// Gets or sets the emote of the option.
8492
/// </summary>
93+
/// <remarks>
94+
/// Can be either an <see cref="Emoji"/> or an <see cref="Discord.Emote"/>
95+
/// </remarks>
8596
public string Emote { get; set; }
8697
}

0 commit comments

Comments
 (0)