Skip to content

Commit 4b35968

Browse files
committed
up
1 parent a919df1 commit 4b35968

10 files changed

Lines changed: 93 additions & 24 deletions

File tree

src/Disc.NET.Commands/Contexts/InteractionContext.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Disc.NET.Commands.Contexts.Models;
1+
using Disc.NET.Client.SDK.Messages.Components.Enums;
2+
using Disc.NET.Commands.Contexts.Models;
23
using Disc.NET.Commands.Responses;
34

45
namespace Disc.NET.Commands.Contexts
@@ -12,7 +13,19 @@ public class InteractionContext : ContextBase
1213
public int Type { get; set; }
1314
public int Context { get; set; }
1415

16+
public InteractionData? Data { get; set; }
1517
public InteractionResponse Response { get; set; }
1618
}
1719

20+
public class InteractionData
21+
{
22+
public List<string> Values { get; set; } = [];
23+
public int Id { get; set; }
24+
25+
public string? CustomId { get; set; }
26+
27+
public MessageComponentType ComponentType { get; set; }
28+
}
29+
30+
1831
}

src/Disc.NET.Commands/Message.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Disc.NET.Client.SDK.Enums;
22
using Disc.NET.Client.SDK.Messages;
33
using Disc.NET.Client.SDK.Messages.Components;
4+
using Disc.NET.Commands.Contexts;
45
using Disc.NET.Commands.MessageBuilders;
56
using Disc.NET.Shared.Constraints;
67
using Disc.NET.Shared.Exceptions;
@@ -29,10 +30,7 @@ private List<object> MountActionRows()
2930
var results = new List<object>();
3031

3132
NormalizeActionRows<ActionRowSelectMenuBuilder>(ActionRowConstraint.MAX_SELECT_MENUS_PER_ACTION_ROW, message =>
32-
new ActionRowSelectMenuBuilder().AddMenu(message.First()));
33-
34-
NormalizeActionRows<ActionRowButtonBuilder>(ActionRowConstraint.MAX_BUTTONS_PER_ACTION_ROW, message =>
35-
new ActionRowButtonBuilder().AddButtons(message));
33+
new ActionRowSelectMenuBuilder().AddComponent<ContextBase>(message.First()));
3634

3735
ActionRows.ForEach(x => results.Add(x.Build()));
3836
return results;

src/Disc.NET.Commands/MessageBuilders/ActionRowButtonBuilder.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Disc.NET.Client.SDK.Messages.Components;
22
using Disc.NET.Client.SDK.Messages.Components.Enums;
33
using Disc.NET.Commands.Contexts;
4-
using System.ComponentModel;
54

65
namespace Disc.NET.Commands.MessageBuilders
76
{
@@ -19,27 +18,20 @@ public ActionRowButtonBuilder(string? id)
1918
Id = id;
2019
}
2120

22-
public ActionRowButtonBuilder AddButton<T>(IMessageComponent component, T? context = null , Func<T, Task>? callback = null) where T : ContextBase
21+
public IActionRowBuilder AddComponent<T>(IMessageComponent component, T? context = null, Func<T, Task>? callback = null) where T : ContextBase
2322
{
2423
if (context != null && callback != null)
2524
{
2625
if (string.IsNullOrEmpty(component.CustomId))
2726
throw new ArgumentException("CustomId must be set for the component when a callback is provided.");
2827

29-
RegisterComponentCallback(component, context, callback);
28+
RegisterComponentCallback(component, context, callback);
3029
}
3130
Components.Add(component);
3231
return this;
3332
}
3433

3534

36-
public ActionRowButtonBuilder AddButtons(List<IMessageComponent> components)
37-
{
38-
Components.AddRange(components);
39-
return this;
40-
}
41-
42-
4335
public object Build()
4436
{
4537
return new

src/Disc.NET.Commands/MessageBuilders/ActionRowSelectMenuBuilder.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Disc.NET.Client.SDK.Messages.Components;
22
using Disc.NET.Client.SDK.Messages.Components.Enums;
3+
using Disc.NET.Commands.Contexts;
34

45
namespace Disc.NET.Commands.MessageBuilders
56
{
@@ -15,13 +16,19 @@ public ActionRowSelectMenuBuilder(string? id)
1516
Id = id;
1617
}
1718

18-
public ActionRowSelectMenuBuilder AddMenu(IMessageComponent component)
19+
public IActionRowBuilder AddComponent<T>(IMessageComponent component, T? context = null, Func<T, Task>? callback = null) where T : ContextBase
1920
{
21+
if (context != null && callback != null)
22+
{
23+
if (string.IsNullOrEmpty(component.CustomId))
24+
throw new ArgumentException("CustomId must be set for the component when a callback is provided.");
25+
26+
RegisterComponentCallback(component, context, callback);
27+
}
2028
Components.Add(component);
2129
return this;
2230
}
2331

24-
2532
public string? Id { get; set; }
2633
public List<object> Components { get; } = [];
2734

@@ -34,6 +41,7 @@ public object Build()
3441
components = Components
3542
};
3643
}
44+
3745
}
3846

3947
}

src/Disc.NET.Commands/MessageBuilders/IActionRowBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface IActionRowBuilder
88
public string? Id { get; }
99
public List<object> Components { get; }
1010
object Build();
11+
IActionRowBuilder AddComponent<T>(IMessageComponent component, T? context = null, Func<T, Task>? callback = null) where T : ContextBase;
1112

1213
}
1314
}

src/Disc.NET/Handlers/EventHandlers/InteractionCallbackHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public async Task HandleAsync(EventHandlerPayload payload, AppConfiguration conf
3535
var callbackCustomId = CallbackCustomIdHelper.GetCallbackCustomId(guildId, customIdWithSuffix);
3636
var context = BuildContextByCustomIdCallbackType(payload, callbackCustomId.CallbackType, configuration);
3737
await ComponentCallbackRepository.InvokeCallbackAsync(callbackCustomId.Id, context);
38+
//ComponentCallbackRepository.UnregisterCallback(callbackCustomId.Id);
3839
}
3940
}
4041
}

src/Disc.NET/Handlers/HandlerCommandBase.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ protected override InteractionContext BuildInteractionContext(JsonDocument conte
107107
{
108108
ChannelId = context.Channel?.Id ?? string.Empty,
109109
};
110+
111+
var data = contextJson.GetJsonStringProperty("data");
112+
if (!string.IsNullOrEmpty(data))
113+
{
114+
try
115+
{
116+
context.Data = Serializer.Deserialize<InteractionData>(data);
117+
}
118+
catch (JsonException)
119+
{
120+
context.Data = null;
121+
}
122+
}
110123
return context;
111124
}
112125

tests/GenericBot/PrefixPrefixCommandTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ await context.Response.ReplyAsync(new Message()
2525
],
2626
ActionRows = new List<IActionRowBuilder>
2727
{
28-
new ActionRowButtonBuilder().AddButton<CommandContext>
28+
new ActionRowButtonBuilder().AddComponent
2929
(
3030
new ButtonComponent(ButtonStyle.Primary)
3131
{

tests/GenericBot/TestCommand.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Disc.NET.Client.SDK.Messages.Components.Buttons;
2+
using Disc.NET.Client.SDK.Messages.Components.Selects;
23
using Disc.NET.Commands;
34
using Disc.NET.Commands.Attributes;
45
using Disc.NET.Commands.Contexts;
@@ -20,15 +21,37 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
2021
Content = "Teste",
2122
ActionRows = new List<IActionRowBuilder>
2223
{
24+
new ActionRowSelectMenuBuilder()
25+
.AddComponent(new StringSelectComponent()
26+
{
27+
CustomId = "test_select",
28+
Options = new List<StringSelectOption>()
29+
{
30+
new StringSelectOption()
31+
{
32+
Label = "Opção 1",
33+
Value = "option_1"
34+
},
35+
36+
}
37+
}, context, TestCallback3Async),
2338
new ActionRowButtonBuilder()
24-
.AddButton(
39+
.AddComponent(
2540
new ButtonComponent(ButtonStyle.Primary)
2641
{
2742
CustomId = "test_button",
2843
Label = "Testar"
2944
},
3045
context,
3146
TestCallbackAsync
47+
).AddComponent(
48+
new ButtonComponent(ButtonStyle.Secondary)
49+
{
50+
CustomId = "test_button_2",
51+
Label = "Testar 2"
52+
},
53+
context,
54+
TestCallback2Async
3255
)
3356
}
3457
};
@@ -43,5 +66,25 @@ await context.Response.SendMessageAsync(new Message()
4366
Content = "Teste",
4467
});
4568
}
69+
70+
private async Task TestCallback2Async(InteractionContext context)
71+
{
72+
await context.Response.SendMessageAsync(new Message()
73+
{
74+
Content = "Teste 2",
75+
});
76+
}
77+
78+
79+
// Think about how use SlashCommandParamsResult in callbacks
80+
private async Task TestCallback3Async(InteractionContext context)
81+
{
82+
83+
var option = context.Data?.Values.FirstOrDefault();
84+
await context.Response.SendMessageAsync(new Message()
85+
{
86+
Content = $"Você escolheu a opção: {option}",
87+
});
88+
}
4689
}
4790
}

tests/GenericBot/WeatherCommand.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
4444

4545

4646
var actionRowWithSelectMenu = new ActionRowSelectMenuBuilder()
47-
.AddMenu(new StringSelectComponent
47+
.AddComponent<InteractionContext>(new StringSelectComponent
4848
{
4949
CustomId = "weather_options1",
5050
Placeholder = "📊 Ver detalhes",
@@ -72,7 +72,7 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
7272
}
7373
]
7474
})
75-
.AddMenu(new StringSelectComponent
75+
.AddComponent<InteractionContext>(new StringSelectComponent
7676
{
7777
CustomId = "weather_options2",
7878
Placeholder = "📊 Ver detalhes",
@@ -100,7 +100,7 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
100100
}
101101
]
102102
})
103-
.AddMenu(new StringSelectComponent
103+
.AddComponent<InteractionContext>(new StringSelectComponent
104104
{
105105
CustomId = "weather_options3",
106106
Placeholder = "📊 Ver detalhes",
@@ -128,7 +128,7 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
128128
}
129129
]
130130
})
131-
.AddMenu(new StringSelectComponent
131+
.AddComponent<InteractionContext>(new StringSelectComponent
132132
{
133133
CustomId = "weather_options4",
134134
Placeholder = "📊 Ver detalhes",
@@ -156,7 +156,7 @@ public async Task RunAsync(InteractionContext context, SlashCommandParamsResult
156156
}
157157
]
158158
})
159-
.AddMenu(new StringSelectComponent
159+
.AddComponent<InteractionContext>(new StringSelectComponent
160160
{
161161
CustomId = "weather_options5",
162162
Placeholder = "📊 Ver detalhes",

0 commit comments

Comments
 (0)