Skip to content

Commit 82f6630

Browse files
committed
Add support for Components V2 in paginated responses
Updated ComponentPaginator and InteractivityExtension to handle Components V2 by checking the UsesCV2 flag and building component layouts accordingly. This ensures correct rendering and interaction handling for both legacy and V2 component structures in paginated messages.
1 parent f42b46d commit 82f6630

File tree

2 files changed

+62
-26
lines changed

2 files changed

+62
-26
lines changed

DisCatSharp.Interactivity/EventHandling/Components/ComponentPaginator.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,41 @@ private async Task HandlePaginationAsync(IPaginationRequest request, ComponentIn
145145
if (request is InteractionPaginationRequest ipr)
146146
{
147147
var builder = new DiscordWebhookBuilder();
148-
if (page.Content is not null)
149-
builder.WithContent(page.Content);
150-
if (page.Embed is not null)
151-
builder.AddEmbed(page.Embed);
152-
builder.AddComponents(bts);
148+
if (!page.UsesCV2)
149+
{
150+
if (page.Content is not null)
151+
builder.WithContent(page.Content);
152+
if (page.Embed is not null)
153+
builder.AddEmbed(page.Embed);
154+
builder.AddComponents(bts);
155+
}
156+
else
157+
{
158+
builder.WithV2Components();
159+
builder.AddComponents(page.ComponentsInternal);
160+
builder.AddComponents(new DiscordActionRowComponent(bts));
161+
}
153162

154163
await (await ipr.GetLastInteractionAsync()).EditOriginalResponseAsync(builder).ConfigureAwait(false);
155164
return;
156165
}
157166

158167
this._builder.Clear();
159168

160-
if (page.Content is not null)
161-
this._builder.WithContent(page.Content);
162-
if (page.Embed is not null)
163-
this._builder.AddEmbed(page.Embed);
164-
this._builder.AddComponents(bts);
169+
if (!page.UsesCV2)
170+
{
171+
if (page.Content is not null)
172+
this._builder.WithContent(page.Content);
173+
if (page.Embed is not null)
174+
this._builder.AddEmbed(page.Embed);
175+
this._builder.AddComponents(bts);
176+
}
177+
else
178+
{
179+
this._builder.WithV2Components();
180+
this._builder.AddComponents(page.ComponentsInternal);
181+
this._builder.AddComponents(new DiscordActionRowComponent(bts));
182+
}
165183

166184
await this._builder.ModifyAsync(msg).ConfigureAwait(false);
167185
}

DisCatSharp.Interactivity/InteractivityExtension.cs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
235235
throw new ArgumentException("Provided message does not contain any components.");
236236

237237
string[]? ids = null;
238-
238+
239239
if (message.Flags.HasValue && message.Flags.Value.HasMessageFlag(MessageFlags.IsComponentsV2))
240240
{
241241
if (message.Components.OfType<DiscordContainerComponent>().SelectMany(c => c.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components)).All(c => c.Type != ComponentType.Button))
@@ -301,7 +301,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
301301
if (message.Components.Count == 0)
302302
throw new ArgumentException("Provided message does not contain any components.");
303303

304-
304+
305305
if (message.Flags.HasValue && message.Flags.Value.HasMessageFlag(MessageFlags.IsComponentsV2))
306306
{
307307
if (message.Components.OfType<DiscordContainerComponent>().SelectMany(c => c.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components)).All(c => c.Type != ComponentType.Button))
@@ -362,7 +362,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
362362
if (message.Components.Count is 0)
363363
throw new ArgumentException("Provided message does not contain any components.");
364364

365-
365+
366366
if (message.Flags.HasValue && message.Flags.Value.HasMessageFlag(MessageFlags.IsComponentsV2))
367367
{
368368
if (message.Components.OfType<DiscordContainerComponent>().SelectMany(c => c.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components)).All(c => c.Type != ComponentType.Button))
@@ -468,7 +468,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
468468
else
469469
{
470470
if (message.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components).All(c => c.Type != selectType))
471-
throw new ArgumentException("Message does not contain any select components.");
471+
throw new ArgumentException("Message does not contain any select components.");
472472
}
473473

474474
var result = await this
@@ -524,7 +524,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
524524
else
525525
{
526526
if (message.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components).All(c => c.Type != selectType))
527-
throw new ArgumentException("Message does not contain any select components.");
527+
throw new ArgumentException("Message does not contain any select components.");
528528

529529
if (message.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components).OfType<DiscordBaseSelectComponent>().All(c => c.CustomId != id))
530530
throw new ArgumentException($"Message does not contain select with Id of '{id}'.");
@@ -584,7 +584,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
584584
else
585585
{
586586
if (message.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components).All(c => c.Type != selectType))
587-
throw new ArgumentException("Message does not contain any select components.");
587+
throw new ArgumentException("Message does not contain any select components.");
588588

589589
if (message.Components.OfType<DiscordActionRowComponent>().SelectMany(c => c.Components).OfType<DiscordBaseSelectComponent>().All(c => c.CustomId != id))
590590
throw new ArgumentException($"Message does not contain select with Id of '{id}'.");
@@ -970,22 +970,40 @@ public async Task SendPaginatedResponseAsync(DiscordInteraction interaction, boo
970970
{
971971
var page = pages.First();
972972
var builder = new DiscordWebhookBuilder();
973-
if (page.Content is not null)
974-
builder.WithContent(page.Content);
975-
if (page.Embed is not null)
976-
builder.AddEmbed(page.Embed);
977-
builder.AddComponents(bts.ButtonArray.ToList());
973+
if (!page.UsesCV2)
974+
{
975+
if (page.Content is not null)
976+
builder.WithContent(page.Content);
977+
if (page.Embed is not null)
978+
builder.AddEmbed(page.Embed);
979+
builder.AddComponents(bts.ButtonArray.ToList());
980+
}
981+
else
982+
{
983+
builder.WithV2Components();
984+
builder.AddComponents(page.ComponentsInternal);
985+
builder.AddComponents(new DiscordActionRowComponent(bts.ButtonArray));
986+
}
978987
message = await interaction.EditOriginalResponseAsync(builder).ConfigureAwait(false);
979988
}
980989
else
981990
{
982991
var page = pages.First();
983992
var builder = new DiscordInteractionResponseBuilder();
984-
if (page.Content is not null)
985-
builder.WithContent(page.Content);
986-
if (page.Embed is not null)
987-
builder.AddEmbed(page.Embed);
988-
builder.AddComponents(bts.ButtonArray.ToList());
993+
if (!page.UsesCV2)
994+
{
995+
if (page.Content is not null)
996+
builder.WithContent(page.Content);
997+
if (page.Embed is not null)
998+
builder.AddEmbed(page.Embed);
999+
builder.AddComponents(bts.ButtonArray.ToList());
1000+
}
1001+
else
1002+
{
1003+
builder.WithV2Components();
1004+
builder.AddComponents(page.ComponentsInternal);
1005+
builder.AddComponents(new DiscordActionRowComponent(bts.ButtonArray));
1006+
}
9891007
if (ephemeral)
9901008
builder = builder.AsEphemeral();
9911009
message = (await interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, builder).ConfigureAwait(false)).Message!;

0 commit comments

Comments
 (0)