Skip to content

Commit c90297e

Browse files
committed
Reapply "[Feature] Components V2 (#3065)" (#3100)
This reverts commit d27bb49.
1 parent d27bb49 commit c90297e

File tree

102 files changed

+2851
-691
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+2851
-691
lines changed

src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs

+33-20
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public interface IDiscordInteraction : ISnowflakeEntity
132132
/// A task that represents an asynchronous send operation for delivering the message.
133133
/// </returns>
134134
Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false, AllowedMentions allowedMentions = null,
135-
MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
135+
MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None);
136136

137137
/// <summary>
138138
/// Responds to this interaction with a file attachment.
@@ -154,7 +154,8 @@ Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false,
154154
/// </returns>
155155
#if NETCOREAPP3_0_OR_GREATER
156156
async Task RespondWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
157-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
157+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
158+
MessageFlags flags = MessageFlags.None)
158159
{
159160
using (var file = new FileAttachment(fileStream, fileName))
160161
{
@@ -163,7 +164,8 @@ async Task RespondWithFileAsync(Stream fileStream, string fileName, string text
163164
}
164165
#else
165166
Task RespondWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
166-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
167+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null
168+
, MessageFlags flags = MessageFlags.None);
167169
#endif
168170
/// <summary>
169171
/// Responds to this interaction with a file attachment.
@@ -185,7 +187,8 @@ Task RespondWithFileAsync(Stream fileStream, string fileName, string text = null
185187
/// </returns>
186188
#if NETCOREAPP3_0_OR_GREATER
187189
async Task RespondWithFileAsync(string filePath, string fileName = null, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
188-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
190+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
191+
MessageFlags flags = MessageFlags.None)
189192
{
190193
using (var file = new FileAttachment(filePath, fileName))
191194
{
@@ -194,7 +197,8 @@ async Task RespondWithFileAsync(string filePath, string fileName = null, string
194197
}
195198
#else
196199
Task RespondWithFileAsync(string filePath, string fileName = null, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
197-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
200+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
201+
MessageFlags flags = MessageFlags.None);
198202
#endif
199203
/// <summary>
200204
/// Responds to this interaction with a file attachment.
@@ -215,11 +219,13 @@ Task RespondWithFileAsync(string filePath, string fileName = null, string text =
215219
/// </returns>
216220
#if NETCOREAPP3_0_OR_GREATER
217221
Task RespondWithFileAsync(FileAttachment attachment, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
218-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
219-
=> RespondWithFilesAsync(new FileAttachment[] { attachment }, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll);
222+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
223+
MessageFlags flags = MessageFlags.None)
224+
=> RespondWithFilesAsync([attachment], text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll, flags);
220225
#else
221226
Task RespondWithFileAsync(FileAttachment attachment, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
222-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
227+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
228+
MessageFlags flags = MessageFlags.None);
223229
#endif
224230
/// <summary>
225231
/// Responds to this interaction with a collection of file attachments.
@@ -239,7 +245,9 @@ Task RespondWithFileAsync(FileAttachment attachment, string text = null, Embed[]
239245
/// contains the sent message.
240246
/// </returns>
241247
Task RespondWithFilesAsync(IEnumerable<FileAttachment> attachments, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
242-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
248+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
249+
MessageFlags flags = MessageFlags.None);
250+
243251
/// <summary>
244252
/// Sends a followup message for this interaction.
245253
/// </summary>
@@ -257,7 +265,9 @@ Task RespondWithFilesAsync(IEnumerable<FileAttachment> attachments, string text
257265
/// contains the sent message.
258266
/// </returns>
259267
Task<IUserMessage> FollowupAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
260-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
268+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
269+
MessageFlags flags = MessageFlags.None);
270+
261271
/// <summary>
262272
/// Sends a followup message for this interaction.
263273
/// </summary>
@@ -278,17 +288,19 @@ Task<IUserMessage> FollowupAsync(string text = null, Embed[] embeds = null, bool
278288
/// </returns>
279289
#if NETCOREAPP3_0_OR_GREATER
280290
async Task<IUserMessage> FollowupWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
281-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
291+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None)
282292
{
283293
using (var file = new FileAttachment(fileStream, fileName))
284294
{
285-
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll).ConfigureAwait(false);
295+
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll, flags).ConfigureAwait(false);
286296
}
287297
}
288298
#else
289299
Task<IUserMessage> FollowupWithFileAsync(Stream fileStream, string fileName, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
290-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
300+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
301+
MessageFlags flags = MessageFlags.None);
291302
#endif
303+
292304
/// <summary>
293305
/// Sends a followup message for this interaction.
294306
/// </summary>
@@ -309,16 +321,17 @@ Task<IUserMessage> FollowupWithFileAsync(Stream fileStream, string fileName, str
309321
/// </returns>
310322
#if NETCOREAPP3_0_OR_GREATER
311323
async Task<IUserMessage> FollowupWithFileAsync(string filePath, string fileName = null, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
312-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
324+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null,
325+
MessageFlags flags = MessageFlags.None)
313326
{
314327
using (var file = new FileAttachment(filePath, fileName))
315328
{
316-
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll).ConfigureAwait(false);
329+
return await FollowupWithFileAsync(file, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll, flags).ConfigureAwait(false);
317330
}
318331
}
319332
#else
320333
Task<IUserMessage> FollowupWithFileAsync(string filePath, string fileName = null, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
321-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
334+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None);
322335
#endif
323336
/// <summary>
324337
/// Sends a followup message for this interaction.
@@ -339,11 +352,11 @@ Task<IUserMessage> FollowupWithFileAsync(string filePath, string fileName = null
339352
/// </returns>
340353
#if NETCOREAPP3_0_OR_GREATER
341354
Task<IUserMessage> FollowupWithFileAsync(FileAttachment attachment, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
342-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null)
343-
=> FollowupWithFilesAsync(new FileAttachment[] { attachment }, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll);
355+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None)
356+
=> FollowupWithFilesAsync(new FileAttachment[] { attachment }, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options, poll, flags);
344357
#else
345358
Task<IUserMessage> FollowupWithFileAsync(FileAttachment attachment, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
346-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
359+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None);
347360
#endif
348361
/// <summary>
349362
/// Sends a followup message for this interaction.
@@ -363,7 +376,7 @@ Task<IUserMessage> FollowupWithFileAsync(FileAttachment attachment, string text
363376
/// contains the sent message.
364377
/// </returns>
365378
Task<IUserMessage> FollowupWithFilesAsync(IEnumerable<FileAttachment> attachments, string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false,
366-
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null);
379+
AllowedMentions allowedMentions = null, MessageComponent components = null, Embed embed = null, RequestOptions options = null, PollProperties poll = null, MessageFlags flags = MessageFlags.None);
367380
/// <summary>
368381
/// Gets the original response for this interaction.
369382
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
using System.Collections.Generic;
22

3-
namespace Discord
3+
namespace Discord;
4+
5+
/// <summary>
6+
/// Represents a <see cref="IMessageComponent"/> Row for child components to live in.
7+
/// </summary>
8+
public class ActionRowComponent : IMessageComponent
49
{
5-
/// <summary>
6-
/// Represents a <see cref="IMessageComponent"/> Row for child components to live in.
7-
/// </summary>
8-
public class ActionRowComponent : IMessageComponent
9-
{
10-
/// <inheritdoc/>
11-
public ComponentType Type => ComponentType.ActionRow;
10+
/// <inheritdoc/>
11+
public ComponentType Type => ComponentType.ActionRow;
1212

13-
/// <summary>
14-
/// Gets the child components in this row.
15-
/// </summary>
16-
public IReadOnlyCollection<IMessageComponent> Components { get; internal set; }
13+
/// <inheritdoc/>
14+
public int? Id { get; internal set; }
1715

18-
internal ActionRowComponent() { }
16+
/// <summary>
17+
/// Gets the child components in this row.
18+
/// </summary>
19+
public IReadOnlyCollection<IMessageComponent> Components { get; internal set; }
1920

20-
internal ActionRowComponent(List<IMessageComponent> components)
21-
{
22-
Components = components;
23-
}
21+
internal ActionRowComponent() { }
2422

25-
string IMessageComponent.CustomId => null;
23+
internal ActionRowComponent(IReadOnlyCollection<IMessageComponent> components)
24+
{
25+
Components = components;
2626
}
2727
}

src/Discord.Net.Core/Entities/Interactions/MessageComponents/Builders/ActionRowBuilder.cs

+36-14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ namespace Discord;
88
/// <summary>
99
/// Represents a class used to build Action rows.
1010
/// </summary>
11-
public class ActionRowBuilder
11+
public class ActionRowBuilder : IMessageComponentBuilder, IInteractableComponentContainer
1212
{
13+
public ComponentType Type => ComponentType.ActionRow;
14+
15+
public int? Id { get; set; }
16+
1317
/// <summary>
1418
/// The max amount of child components this row can hold.
1519
/// </summary>
@@ -20,7 +24,7 @@ public class ActionRowBuilder
2024
/// </summary>
2125
/// <exception cref="ArgumentNullException" accessor="set"><see cref="Components"/> cannot be null.</exception>
2226
/// <exception cref="ArgumentException" accessor="set"><see cref="Components"/> count exceeds <see cref="MaxChildCount"/>.</exception>
23-
public List<IMessageComponent> Components
27+
public List<IMessageComponentBuilder> Components
2428
{
2529
get => _components;
2630
set
@@ -37,15 +41,29 @@ public List<IMessageComponent> Components
3741
}
3842
}
3943

40-
private List<IMessageComponent> _components = new List<IMessageComponent>();
44+
45+
public ActionRowBuilder AddComponents(params IMessageComponentBuilder[] components)
46+
{
47+
foreach (var component in components)
48+
AddComponent(component);
49+
return this;
50+
}
51+
52+
public ActionRowBuilder WithComponents(IEnumerable<IMessageComponentBuilder> components)
53+
{
54+
Components = components.ToList();
55+
return this;
56+
}
57+
58+
private List<IMessageComponentBuilder> _components = new ();
4159

4260
/// <summary>
4361
/// Adds a list of components to the current row.
4462
/// </summary>
4563
/// <param name="components">The list of components to add.</param>
4664
/// <inheritdoc cref="Components"/>
4765
/// <returns>The current builder.</returns>
48-
public ActionRowBuilder WithComponents(List<IMessageComponent> components)
66+
public ActionRowBuilder WithComponents(List<IMessageComponentBuilder> components)
4967
{
5068
Components = components;
5169
return this;
@@ -57,7 +75,7 @@ public ActionRowBuilder WithComponents(List<IMessageComponent> components)
5775
/// <param name="component">The component to add.</param>
5876
/// <exception cref="InvalidOperationException">Components count reached <see cref="MaxChildCount"/></exception>
5977
/// <returns>The current builder.</returns>
60-
public ActionRowBuilder AddComponent(IMessageComponent component)
78+
public ActionRowBuilder AddComponent(IMessageComponentBuilder component)
6179
{
6280
if (Components.Count >= MaxChildCount)
6381
throw new InvalidOperationException($"Components count reached {MaxChildCount}");
@@ -103,13 +121,11 @@ public ActionRowBuilder WithSelectMenu(SelectMenuBuilder menu)
103121
{
104122
if (menu.Options is not null && menu.Options.Distinct().Count() != menu.Options.Count)
105123
throw new InvalidOperationException("Please make sure that there is no duplicates values.");
106-
107-
var builtMenu = menu.Build();
108-
124+
109125
if (Components.Count != 0)
110126
throw new InvalidOperationException($"A Select Menu cannot exist in a pre-occupied ActionRow.");
111127

112-
AddComponent(builtMenu);
128+
AddComponent(menu);
113129

114130
return this;
115131
}
@@ -152,15 +168,13 @@ public ActionRowBuilder WithButton(
152168
/// <returns>The current builder.</returns>
153169
public ActionRowBuilder WithButton(ButtonBuilder button)
154170
{
155-
var builtButton = button.Build();
156-
157171
if (Components.Count >= 5)
158172
throw new InvalidOperationException($"Components count reached {MaxChildCount}");
159173

160174
if (Components.Any(x => x.Type.IsSelectType()))
161175
throw new InvalidOperationException($"A button cannot be added to a row with a SelectMenu");
162176

163-
AddComponent(builtButton);
177+
AddComponent(button);
164178

165179
return this;
166180
}
@@ -171,10 +185,11 @@ public ActionRowBuilder WithButton(ButtonBuilder button)
171185
/// <returns>A <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/></returns>
172186
public ActionRowComponent Build()
173187
{
174-
return new ActionRowComponent(_components);
188+
return new ActionRowComponent(_components.Select(x => x.Build()).ToList());
175189
}
190+
IMessageComponent IMessageComponentBuilder.Build() => Build();
176191

177-
internal bool CanTakeComponent(IMessageComponent component)
192+
internal bool CanTakeComponent(IMessageComponentBuilder component)
178193
{
179194
switch (component.Type)
180195
{
@@ -195,4 +210,11 @@ internal bool CanTakeComponent(IMessageComponent component)
195210
return false;
196211
}
197212
}
213+
214+
215+
IComponentContainer IComponentContainer.AddComponent(IMessageComponentBuilder component) => AddComponent(component);
216+
217+
IComponentContainer IComponentContainer.AddComponents(params IMessageComponentBuilder[] components) => AddComponents(components);
218+
219+
IComponentContainer IComponentContainer.WithComponents(IEnumerable<IMessageComponentBuilder> components) => WithComponents(components);
198220
}

0 commit comments

Comments
 (0)