Skip to content

Commit d7c97c9

Browse files
authored
deal with Discord's component changes - immediate component API (#2085)
1 parent 0b95785 commit d7c97c9

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

DSharpPlus.Interactivity/InteractivityExtension.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
201201
throw new ArgumentException("Provided message does not contain any components.");
202202
}
203203

204-
if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is DiscordComponentType.Button))
204+
if (message.FilterComponents<DiscordButtonComponent>().Count == 0)
205205
{
206206
throw new ArgumentException("Provided message does not contain any button components.");
207207
}
@@ -246,12 +246,12 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
246246
throw new ArgumentException("Provided message does not contain any components.");
247247
}
248248

249-
if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is DiscordComponentType.Button))
249+
if (message.FilterComponents<DiscordButtonComponent>().Count == 0)
250250
{
251251
throw new ArgumentException("Provided message does not contain any button components.");
252252
}
253253

254-
IEnumerable<string> ids = message.Components.SelectMany(m => m.Components).Select(c => c.CustomId);
254+
IEnumerable<string> ids = message.FilterComponents<DiscordComponent>().Select(c => c.CustomId);
255255

256256
ComponentInteractionCreatedEventArgs? result =
257257
await
@@ -295,7 +295,7 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
295295
throw new ArgumentException("Provided message does not contain any components.");
296296
}
297297

298-
if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is DiscordComponentType.Button))
298+
if (message.FilterComponents<DiscordButtonComponent>().Count == 0)
299299
{
300300
throw new ArgumentException("Provided message does not contain any button components.");
301301
}
@@ -342,12 +342,12 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
342342
throw new ArgumentException("Provided message does not contain any components.");
343343
}
344344

345-
if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is DiscordComponentType.Button))
345+
if (message.FilterComponents<DiscordButtonComponent>().Count == 0)
346346
{
347347
throw new ArgumentException("Provided message does not contain any button components.");
348348
}
349349

350-
if (!message.Components.SelectMany(c => c.Components).OfType<DiscordButtonComponent>().Any(c => c.CustomId == id))
350+
if (!message.FilterComponents<DiscordButtonComponent>().Any(c => c.CustomId == id))
351351
{
352352
throw new ArgumentException($"Provided message does not contain button with Id of '{id}'.");
353353
}
@@ -387,7 +387,7 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
387387
throw new ArgumentException("Provided message does not contain any components.");
388388
}
389389

390-
if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is DiscordComponentType.Button))
390+
if (message.FilterComponents<DiscordButtonComponent>().Count == 0)
391391
{
392392
throw new ArgumentException("Provided message does not contain any button components.");
393393
}
@@ -429,7 +429,7 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
429429
throw new ArgumentException("Provided message does not contain any components.");
430430
}
431431

432-
if (!message.Components.SelectMany(c => c.Components).Any(IsSelect))
432+
if (!message.FilterComponents<DiscordComponent>().Any(IsSelect))
433433
{
434434
throw new ArgumentException("Provided message does not contain any select components.");
435435
}
@@ -472,12 +472,12 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
472472
throw new ArgumentException("Provided message does not contain any components.");
473473
}
474474

475-
if (!message.Components.SelectMany(c => c.Components).Any(IsSelect))
475+
if (!message.FilterComponents<DiscordComponent>().Any(IsSelect))
476476
{
477477
throw new ArgumentException("Provided message does not contain any select components.");
478478
}
479479

480-
if (message.Components.SelectMany(c => c.Components).Where(IsSelect).All(c => c.CustomId != id))
480+
if (message.FilterComponents<DiscordComponent>().Where(IsSelect).All(c => c.CustomId != id))
481481
{
482482
throw new ArgumentException($"Provided message does not contain select component with Id of '{id}'.");
483483
}
@@ -532,12 +532,12 @@ public async Task<InteractivityResult<ComponentInteractionCreatedEventArgs>> Wai
532532
throw new ArgumentException("Provided message does not contain any components.");
533533
}
534534

535-
if (!message.Components.SelectMany(c => c.Components).Any(IsSelect))
535+
if (!message.FilterComponents<DiscordComponent>().Any(IsSelect))
536536
{
537537
throw new ArgumentException("Provided message does not contain any select components.");
538538
}
539539

540-
if (message.Components.SelectMany(c => c.Components).Where(IsSelect).All(c => c.CustomId != id))
540+
if (message.FilterComponents<DiscordComponent>().Where(IsSelect).All(c => c.CustomId != id))
541541
{
542542
throw new ArgumentException($"Provided message does not contain button with Id of '{id}'.");
543543
}

DSharpPlus/Entities/Channel/Message/DiscordMessage.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ public DiscordChannel? Channel
8989
/// Gets the components this message was sent with.
9090
/// </summary>
9191
[JsonProperty("components", NullValueHandling = NullValueHandling.Ignore)]
92-
public IReadOnlyList<DiscordActionRowComponent>? Components { get; internal set; }
92+
public IReadOnlyList<DiscordComponent>? Components { get; internal set; }
93+
94+
/// <summary>
95+
/// Gets the action rows this message was sent with - components holding buttons, selects and the likes.
96+
/// </summary>
97+
public IReadOnlyList<DiscordActionRowComponent>? ComponentActionRows
98+
=> this.Components?.Where(x => x is DiscordActionRowComponent).Cast<DiscordActionRowComponent>().ToList();
9399

94100
/// <summary>
95101
/// Gets the user or member that sent the message.
@@ -438,6 +444,35 @@ internal void PopulateMentions()
438444
}
439445
}
440446

447+
/// <summary>
448+
/// Searches the components on this message for an aggregate of all components of a certain type.
449+
/// </summary>
450+
public IReadOnlyList<T> FilterComponents<T>()
451+
where T : DiscordComponent
452+
{
453+
List<T> components = [];
454+
455+
foreach (DiscordComponent component in this.Components)
456+
{
457+
if (component is DiscordActionRowComponent actionRowComponent)
458+
{
459+
foreach (DiscordComponent subComponent in actionRowComponent.Components)
460+
{
461+
if (subComponent is T filteredComponent)
462+
{
463+
components.Add(filteredComponent);
464+
}
465+
}
466+
}
467+
else if (component is T filteredComponent)
468+
{
469+
components.Add(filteredComponent);
470+
}
471+
}
472+
473+
return components;
474+
}
475+
441476
/// <summary>
442477
/// Edits the message.
443478
/// </summary>

DSharpPlus/Entities/Channel/Message/DiscordMessageBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public DiscordMessageBuilder(DiscordMessage baseMessage)
9999
this.IsTTS = baseMessage.IsTTS;
100100
this.Poll = baseMessage.Poll == null ? null : new DiscordPollBuilder(baseMessage.Poll);
101101
this.ReplyId = baseMessage.ReferencedMessage?.Id;
102-
this.components = [.. baseMessage.Components];
102+
this.components = [.. baseMessage.Components?.OfType<DiscordActionRowComponent>()];
103103
this.content = baseMessage.Content;
104104
this.embeds = [.. baseMessage.Embeds];
105105
this.stickers = [.. baseMessage.Stickers];

DSharpPlus/Entities/Interaction/DiscordInteractionData.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Newtonsoft.Json;
34

45
namespace DSharpPlus.Entities;
@@ -41,11 +42,47 @@ public sealed class DiscordInteractionData : SnowflakeObject
4142
/// <summary>
4243
/// Components on this interaction. Only applies to modal interactions.
4344
/// </summary>
44-
public IReadOnlyList<DiscordActionRowComponent> Components => this.components;
45+
public IReadOnlyList<DiscordComponent>? Components => this.components;
4546

4647
[JsonProperty("components", NullValueHandling = NullValueHandling.Ignore)]
47-
internal List<DiscordActionRowComponent> components;
48+
internal List<DiscordComponent>? components;
4849

50+
/// <summary>
51+
/// Gets all text input components on this interaction.
52+
/// </summary>
53+
public IReadOnlyList<DiscordTextInputComponent>? TextInputComponents
54+
{
55+
get
56+
{
57+
if (this.Components is null)
58+
{
59+
return null;
60+
}
61+
62+
List<DiscordTextInputComponent> components = [];
63+
64+
foreach (DiscordComponent component in this.Components)
65+
{
66+
if (component is DiscordActionRowComponent actionRowComponent)
67+
{
68+
foreach (DiscordComponent subComponent in actionRowComponent.Components)
69+
{
70+
if (subComponent is DiscordTextInputComponent filteredComponent)
71+
{
72+
components.Add(filteredComponent);
73+
}
74+
}
75+
}
76+
else if (component is DiscordTextInputComponent filteredComponent)
77+
{
78+
components.Add(filteredComponent);
79+
}
80+
}
81+
82+
return components;
83+
}
84+
}
85+
4986
/// <summary>
5087
/// The Id of the target. Applicable for context menus.
5188
/// </summary>

0 commit comments

Comments
 (0)