Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix interactivity
  • Loading branch information
akiraveliara committed Sep 1, 2024
commit 9c83cbc5c017308955bd694a1cb296de2135d4b1
26 changes: 14 additions & 12 deletions DSharpPlus.Interactivity/InteractivityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is ComponentType.Button))
if (!message.FilterComponents<DiscordButtonComponent>().Any())
throw new ArgumentException("Provided message does not contain any button components.");


Expand Down Expand Up @@ -246,10 +246,12 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is ComponentType.Button))
IReadOnlyList<DiscordButtonComponent> buttons = message.FilterComponents<DiscordButtonComponent>();

if (!buttons.Any())
throw new ArgumentException("Provided message does not contain any button components.");

var ids = message.Components.SelectMany(m => m.Components).Select(c => c.CustomId);
var ids = buttons.Select(c => c.CustomId);

var result =
await this
Expand Down Expand Up @@ -289,7 +291,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is ComponentType.Button))
if (!message.FilterComponents<DiscordButtonComponent>().Any())
throw new ArgumentException("Provided message does not contain any button components.");

var result = await this
Expand Down Expand Up @@ -330,10 +332,10 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is ComponentType.Button))
if (!message.FilterComponents<DiscordButtonComponent>().Any())
throw new ArgumentException("Provided message does not contain any button components.");

if (!message.Components.SelectMany(c => c.Components).OfType<DiscordButtonComponent>().Any(c => c.CustomId == id))
if (!message.FilterComponents<DiscordButtonComponent>().Any(c => c.CustomId == id))
throw new ArgumentException($"Provided message does not contain button with Id of '{id}'.");

var result = await this
Expand Down Expand Up @@ -367,7 +369,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(c => c.Type is ComponentType.Button))
if (!message.FilterComponents<DiscordButtonComponent>().Any())
throw new ArgumentException("Provided message does not contain any button components.");

var result = await this
Expand Down Expand Up @@ -403,7 +405,7 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(this.IsSelect))
if (!message.FilterComponents<DiscordComponent>().Any(this.IsSelect))
throw new ArgumentException("Provided message does not contain any select components.");


Expand Down Expand Up @@ -442,10 +444,10 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(this.IsSelect))
if (!message.FilterComponents<DiscordComponent>().Any(this.IsSelect))
throw new ArgumentException("Provided message does not contain any select components.");

if (message.Components.SelectMany(c => c.Components).Where(this.IsSelect).All(c => c.CustomId != id))
if (message.FilterComponents<DiscordComponent>().Where(this.IsSelect).All(c => c.CustomId != id))
throw new ArgumentException($"Provided message does not contain select component with Id of '{id}'.");

var result = await this
Expand Down Expand Up @@ -495,10 +497,10 @@ public async Task<InteractivityResult<ComponentInteractionCreateEventArgs>> Wait
if (!message.Components.Any())
throw new ArgumentException("Provided message does not contain any components.");

if (!message.Components.SelectMany(c => c.Components).Any(this.IsSelect))
if (!message.FilterComponents<DiscordComponent>().Any(this.IsSelect))
throw new ArgumentException("Provided message does not contain any select components.");

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

var result = await this
Expand Down
30 changes: 30 additions & 0 deletions DSharpPlus/Entities/Channel/Message/DiscordMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Newtonsoft.Json;

Expand Down Expand Up @@ -440,6 +441,35 @@ internal void PopulateMentions()
this._mentionedUsers = mentionedUsers.ToList();
}

/// <summary>
/// Searches the components on this message for an aggregate of all components of a certain type.
/// </summary>
public IReadOnlyList<T> FilterComponents<T>()
where T : DiscordComponent
{
List<T> components = new();

foreach (DiscordComponent component in this.Components)
{
if (component is DiscordActionRowComponent actionRowComponent)
{
foreach (DiscordComponent subComponent in actionRowComponent.Components)
{
if (subComponent is T filteredComponent)
{
components.Add(filteredComponent);
}
}
}
else if (component is T filteredComponent)
{
components.Add(filteredComponent);
}
}

return components;
}

/// <summary>
/// Edits the message.
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion DSharpPlus/Entities/Channel/Message/DiscordMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public DiscordMessageBuilder(DiscordMessage baseMessage)
{
this.IsTTS = baseMessage.IsTTS;
this.ReplyId = baseMessage.ReferencedMessage?.Id;
this._components = baseMessage.Components.ToList(); // Calling ToList copies the list instead of referencing it

// calling tolist copies the list, which is good since we inaccurately reflect the message in case of
// unknown components
this._components = baseMessage.Components.OfType<DiscordActionRowComponent>().ToList();
this._content = baseMessage.Content;
this._embeds = baseMessage.Embeds.ToList();
this._stickers = baseMessage.Stickers.ToList();
Expand Down
2 changes: 1 addition & 1 deletion DSharpPlus/EventArgs/Interaction/ModalSubmitEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal ModalSubmitEventArgs(DiscordInteraction interaction)
var dict = new Dictionary<string, string>();

foreach (var component in interaction.Data._components)
if (component.Components.First() is TextInputComponent input)
if ((component as DiscordActionRowComponent)?.Components.First() is TextInputComponent input)
dict.Add(input.CustomId, input.Value);

this.Values = dict;
Expand Down