[Feature] Initial user apps support (#2883)

* omg it kinda works somehow

* more things added

* a bit of xmldocs

* added interaction framework support

* working? IF

* more builder stuff

* space

* rename attribute to prevent conflict with `ContextType` enum

* context type

* moar features

* remove integration types

* trigger workflow

* modelzzzz

* `InteractionContextType`

* allow setting custom status with `SetGameAsync`

* bugzzz

* app permissions

* message interaction context

* hm

* push for cd

* structs lets goooo

* whoops forgot to change types

* whoops x2

* tweak some things

* xmldocs + missing prop + fix enabled in dm

* moar validations

* deprecate a bunch of stuffz

* disable moar obsolete warnings

* add IF sample

* Apply suggestions from code review

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>

* Update src/Discord.Net.Rest/Entities/RestApplication.cs

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>

---------

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
This commit is contained in:
Mihail Gribkov
2024-03-19 00:24:05 +03:00
committed by GitHub
parent bfc8dc229e
commit 24a69785fe
63 changed files with 1257 additions and 300 deletions

View File

@@ -0,0 +1,17 @@
namespace Discord;
/// <summary>
/// Defines where an application can be installed.
/// </summary>
public enum ApplicationIntegrationType
{
/// <summary>
/// The application can be installed to a guild.
/// </summary>
GuildInstall = 0,
/// <summary>
/// The application can be installed to a user.
/// </summary>
UserInstall = 1,
}

View File

@@ -154,5 +154,10 @@ namespace Discord
/// Gets the application's verification state.
/// </summary>
ApplicationVerificationState VerificationState { get; }
/// <summary>
/// Gets application install params configured for integration install types.
/// </summary>
IReadOnlyDictionary<ApplicationIntegrationType, ApplicationInstallParams> IntegrationTypesConfig { get; }
}
}

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace Discord;
/// <summary>
@@ -54,4 +56,9 @@ public class ModifyApplicationProperties
/// </remarks>
public Optional<ApplicationFlags> Flags { get; set; }
/// <summary>
/// Gets or sets application install params configured for integration install types.
/// </summary>
public Optional<Dictionary<ApplicationIntegrationType, ApplicationInstallParams>> IntegrationTypesConfig { get; set; }
}

View File

@@ -93,6 +93,16 @@ namespace Discord
/// </summary>
public Optional<GuildPermission> DefaultMemberPermissions { get; set; }
/// <summary>
/// Gets or sets the install method for this command.
/// </summary>
public Optional<HashSet<ApplicationIntegrationType>> IntegrationTypes { get; set; }
/// <summary>
/// Gets or sets context types this command can be executed in.
/// </summary>
public Optional<HashSet<InteractionContextType>> ContextTypes { get; set; }
internal ApplicationCommandProperties() { }
}
}

View File

@@ -40,6 +40,7 @@ namespace Discord
/// <remarks>
/// Only for globally-scoped commands.
/// </remarks>
[Obsolete("This property will be deprecated soon. Use ContextTypes instead.")]
bool IsEnabledInDm { get; }
/// <summary>
@@ -83,6 +84,16 @@ namespace Discord
/// </remarks>
string DescriptionLocalized { get; }
/// <summary>
/// Gets context types the command can be used in; <see langword="null" /> if not specified.
/// </summary>
IReadOnlyCollection<InteractionContextType> ContextTypes { get; }
/// <summary>
/// Gets the install method for the command; <see langword="null" /> if not specified.
/// </summary>
IReadOnlyCollection<ApplicationIntegrationType> IntegrationTypes { get; }
/// <summary>
/// Modifies the current application command.
/// </summary>

View File

@@ -0,0 +1,22 @@
namespace Discord;
/// <summary>
/// Represents a context in Discord where an interaction can be used.
/// </summary>
public enum InteractionContextType
{
/// <summary>
/// The command can be used in guilds.
/// </summary>
Guild = 0,
/// <summary>
/// The command can be used in DM channel with the bot.
/// </summary>
BotDm = 1,
/// <summary>
/// The command can be used in private channels.
/// </summary>
PrivateChannel = 2
}

View File

@@ -44,6 +44,7 @@ namespace Discord
/// <summary>
/// Gets or sets whether or not this command can be used in DMs.
/// </summary>
[Obsolete("This property will be deprecated soon. Configure with ContextTypes instead.")]
public bool IsDMEnabled { get; set; } = true;
/// <summary>
@@ -56,6 +57,16 @@ namespace Discord
/// </summary>
public GuildPermission? DefaultMemberPermissions { get; set; }
/// <summary>
/// Gets the install method for this command; <see langword="null" /> if not specified.
/// </summary>
public HashSet<ApplicationIntegrationType> IntegrationTypes { get; set; } = null;
/// <summary>
/// Gets the context types this command can be executed in; <see langword="null" /> if not specified.
/// </summary>
public HashSet<InteractionContextType> ContextTypes { get; set; } = null;
private string _name;
private Dictionary<string, string> _nameLocalizations;
@@ -71,10 +82,14 @@ namespace Discord
{
Name = Name,
IsDefaultPermission = IsDefaultPermission,
#pragma warning disable CS0618 // Type or member is obsolete
IsDMEnabled = IsDMEnabled,
#pragma warning restore CS0618 // Type or member is obsolete
DefaultMemberPermissions = DefaultMemberPermissions ?? Optional<GuildPermission>.Unspecified,
NameLocalizations = NameLocalizations,
IsNsfw = IsNsfw,
IntegrationTypes = IntegrationTypes,
ContextTypes = ContextTypes
};
return props;
@@ -133,6 +148,7 @@ namespace Discord
/// </summary>
/// <param name="permission"><see langword="true"/> if the command is available in dms, otherwise <see langword="false"/>.</param>
/// <returns>The current builder.</returns>
[Obsolete("This method will be deprecated soon. Configure with WithContextTypes instead.")]
public MessageCommandBuilder WithDMPermission(bool permission)
{
IsDMEnabled = permission;
@@ -187,5 +203,31 @@ namespace Discord
DefaultMemberPermissions = permissions;
return this;
}
/// <summary>
/// Sets the install method for this command.
/// </summary>
/// <param name="integrationTypes">Install types for this command.</param>
/// <returns>The builder instance.</returns>
public MessageCommandBuilder WithIntegrationTypes(params ApplicationIntegrationType[] integrationTypes)
{
IntegrationTypes = integrationTypes is not null
? new HashSet<ApplicationIntegrationType>(integrationTypes)
: null;
return this;
}
/// <summary>
/// Sets context types this command can be executed in.
/// </summary>
/// <param name="contextTypes">Context types the command can be executed in.</param>
/// <returns>The builder instance.</returns>
public MessageCommandBuilder WithContextTypes(params InteractionContextType[] contextTypes)
{
ContextTypes = contextTypes is not null
? new HashSet<InteractionContextType>(contextTypes)
: null;
return this;
}
}
}

View File

@@ -44,6 +44,7 @@ namespace Discord
/// <summary>
/// Gets or sets whether or not this command can be used in DMs.
/// </summary>
[Obsolete("This property will be deprecated soon. Configure with ContextTypes instead.")]
public bool IsDMEnabled { get; set; } = true;
/// <summary>
@@ -56,6 +57,16 @@ namespace Discord
/// </summary>
public GuildPermission? DefaultMemberPermissions { get; set; }
/// <summary>
/// Gets the installation method for this command. <see langword="null"/> if not set.
/// </summary>
public HashSet<ApplicationIntegrationType> IntegrationTypes { get; set; }
/// <summary>
/// Gets the context types this command can be executed in. <see langword="null"/> if not set.
/// </summary>
public HashSet<InteractionContextType> ContextTypes { get; set; }
private string _name;
private Dictionary<string, string> _nameLocalizations;
@@ -69,10 +80,14 @@ namespace Discord
{
Name = Name,
IsDefaultPermission = IsDefaultPermission,
#pragma warning disable CS0618 // Type or member is obsolete
IsDMEnabled = IsDMEnabled,
#pragma warning restore CS0618 // Type or member is obsolete
DefaultMemberPermissions = DefaultMemberPermissions ?? Optional<GuildPermission>.Unspecified,
NameLocalizations = NameLocalizations,
IsNsfw = IsNsfw,
ContextTypes = ContextTypes,
IntegrationTypes = IntegrationTypes
};
return props;
@@ -131,6 +146,7 @@ namespace Discord
/// </summary>
/// <param name="permission"><see langword="true"/> if the command is available in dms, otherwise <see langword="false"/>.</param>
/// <returns>The current builder.</returns>
[Obsolete("This method will be deprecated soon. Configure with WithContextTypes instead.")]
public UserCommandBuilder WithDMPermission(bool permission)
{
IsDMEnabled = permission;
@@ -185,5 +201,31 @@ namespace Discord
DefaultMemberPermissions = permissions;
return this;
}
/// <summary>
/// Sets the installation method for this command.
/// </summary>
/// <param name="integrationTypes">Installation types for this command.</param>
/// <returns>The builder instance.</returns>
public UserCommandBuilder WithIntegrationTypes(params ApplicationIntegrationType[] integrationTypes)
{
IntegrationTypes = integrationTypes is not null
? new HashSet<ApplicationIntegrationType>(integrationTypes)
: null;
return this;
}
/// <summary>
/// Sets context types this command can be executed in.
/// </summary>
/// <param name="contextTypes">Context types the command can be executed in.</param>
/// <returns>The builder instance.</returns>
public UserCommandBuilder WithContextTypes(params InteractionContextType[] contextTypes)
{
ContextTypes = contextTypes is not null
? new HashSet<InteractionContextType>(contextTypes)
: null;
return this;
}
}
}

View File

@@ -96,6 +96,21 @@ namespace Discord
/// </summary>
IReadOnlyCollection<IEntitlement> Entitlements { get; }
/// <summary>
/// Gets which integrations authorized the interaction.
/// </summary>
IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; }
/// <summary>
/// Gets the context this interaction was created in. <see langword="null"/> if context type is unknown.
/// </summary>
InteractionContextType? ContextType { get; }
/// <summary>
/// Gets the permissions the app or bot has within the channel the interaction was sent from.
/// </summary>
GuildPermissions Permissions { get; }
/// <summary>
/// Responds to an Interaction with type <see cref="InteractionResponseType.ChannelMessageWithSource"/>.
/// </summary>

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace Discord;
/// <summary>
/// Represents the metadata of an application command interaction.
/// </summary>
public readonly struct ApplicationCommandInteractionMetadata : IMessageInteractionMetadata
{
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc />
public ulong Id { get; }
/// <inheritdoc />
public InteractionType Type { get; }
/// <inheritdoc />
public ulong UserId { get; }
/// <inheritdoc />
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; }
/// <inheritdoc />
public ulong? OriginalResponseMessageId { get; }
/// <summary>
/// Gets the name of the command.
/// </summary>
public string Name { get; }
internal ApplicationCommandInteractionMetadata(ulong id, InteractionType type, ulong userId, IReadOnlyDictionary<ApplicationIntegrationType, ulong> integrationOwners,
ulong? originalResponseMessageId, string name)
{
Id = id;
Type = type;
UserId = userId;
IntegrationOwners = integrationOwners;
OriginalResponseMessageId = originalResponseMessageId;
Name = name;
}
}

View File

@@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace Discord;
/// <summary>
/// Represents the metadata of an interaction.
/// </summary>
public interface IMessageInteractionMetadata : ISnowflakeEntity
{
/// <summary>
/// Gets the type of the interaction.
/// </summary>
InteractionType Type { get; }
/// <summary>
/// Gets the ID of the user who triggered the interaction.
/// </summary>
ulong UserId { get; }
/// <summary>
/// Gets the Ids for installation contexts related to the interaction.
/// </summary>
IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; }
/// <summary>
/// Gets the ID of the original response message if the message is a followup.
/// <see langword="null"/> on original response messages.
/// </summary>
ulong? OriginalResponseMessageId { get; }
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace Discord;
/// <summary>
/// Represents the metadata of a component interaction.
/// </summary>
public readonly struct MessageComponentInteractionMetadata : IMessageInteractionMetadata
{
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc />
public ulong Id { get; }
/// <inheritdoc />
public InteractionType Type { get; }
/// <inheritdoc />
public ulong UserId { get; }
/// <inheritdoc />
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; }
/// <inheritdoc />
public ulong? OriginalResponseMessageId { get; }
/// <summary>
/// Gets the ID of the message that was interacted with to trigger the interaction.
/// </summary>
public ulong InteractedMessageId { get; }
internal MessageComponentInteractionMetadata(ulong id, InteractionType type, ulong userId, IReadOnlyDictionary<ApplicationIntegrationType, ulong> integrationOwners,
ulong? originalResponseMessageId, ulong interactedMessageId)
{
Id = id;
Type = type;
UserId = userId;
IntegrationOwners = integrationOwners;
OriginalResponseMessageId = originalResponseMessageId;
InteractedMessageId = interactedMessageId;
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace Discord;
/// <summary>
/// Represents the metadata of a modal interaction.
/// </summary>
public readonly struct ModalSubmitInteractionMetadata :IMessageInteractionMetadata
{
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <inheritdoc />
public ulong Id { get; }
/// <inheritdoc />
public InteractionType Type { get; }
/// <inheritdoc />
public ulong UserId { get; }
/// <inheritdoc />
public IReadOnlyDictionary<ApplicationIntegrationType, ulong> IntegrationOwners { get; }
/// <inheritdoc />
public ulong? OriginalResponseMessageId { get; }
/// <summary>
/// Gets the interaction metadata of the interaction that responded with the modal.
/// </summary>
public IMessageInteractionMetadata TriggeringInteractionMetadata { get; }
internal ModalSubmitInteractionMetadata(ulong id, InteractionType type, ulong userId, IReadOnlyDictionary<ApplicationIntegrationType, ulong> integrationOwners,
ulong? originalResponseMessageId, IMessageInteractionMetadata triggeringInteractionMetadata)
{
Id = id;
Type = type;
UserId = userId;
IntegrationOwners = integrationOwners;
OriginalResponseMessageId = originalResponseMessageId;
TriggeringInteractionMetadata = triggeringInteractionMetadata;
}
}

View File

@@ -75,6 +75,16 @@ namespace Discord
/// </summary>
public IReadOnlyDictionary<string, string> DescriptionLocalizations => _descriptionLocalizations;
/// <summary>
/// Gets the context types this command can be executed in. <see langword="null"/> if not set.
/// </summary>
public HashSet<InteractionContextType> ContextTypes { get; set; }
/// <summary>
/// Gets the installation method for this command. <see langword="null"/> if not set.
/// </summary>
public HashSet<ApplicationIntegrationType> IntegrationTypes { get; set; }
/// <summary>
/// Gets or sets whether the command is enabled by default when the app is added to a guild
/// </summary>
@@ -83,6 +93,7 @@ namespace Discord
/// <summary>
/// Gets or sets whether or not this command can be used in DMs.
/// </summary>
[Obsolete("This property will be deprecated soon. Configure with ContextTypes instead.")]
public bool IsDMEnabled { get; set; } = true;
/// <summary>
@@ -107,6 +118,10 @@ namespace Discord
/// <returns>A <see cref="SlashCommandProperties"/> that can be used to create slash commands.</returns>
public SlashCommandProperties Build()
{
// doing ?? 1 for now until we know default values (if these become non-optional)
Preconditions.AtLeast(ContextTypes?.Count ?? 1, 1, nameof(ContextTypes), "At least 1 context type must be specified");
Preconditions.AtLeast(IntegrationTypes?.Count ?? 1, 1, nameof(IntegrationTypes), "At least 1 integration type must be specified");
var props = new SlashCommandProperties
{
Name = Name,
@@ -114,9 +129,13 @@ namespace Discord
IsDefaultPermission = IsDefaultPermission,
NameLocalizations = _nameLocalizations,
DescriptionLocalizations = _descriptionLocalizations,
#pragma warning disable CS0618 // Type or member is obsolete
IsDMEnabled = IsDMEnabled,
#pragma warning restore CS0618 // Type or member is obsolete
DefaultMemberPermissions = DefaultMemberPermissions ?? Optional<GuildPermission>.Unspecified,
IsNsfw = IsNsfw,
ContextTypes = ContextTypes ?? Optional<HashSet<InteractionContextType>>.Unspecified,
IntegrationTypes = IntegrationTypes ?? Optional<HashSet<ApplicationIntegrationType>>.Unspecified
};
if (Options != null && Options.Any())
@@ -171,6 +190,7 @@ namespace Discord
/// </summary>
/// <param name="permission"><see langword="true"/> if the command is available in dms, otherwise <see langword="false"/>.</param>
/// <returns>The current builder.</returns>
[Obsolete("This method will be deprecated soon. Configure using WithContextTypes instead.")]
public SlashCommandBuilder WithDMPermission(bool permission)
{
IsDMEnabled = permission;
@@ -199,6 +219,32 @@ namespace Discord
return this;
}
/// <summary>
/// Sets the installation method for this command.
/// </summary>
/// <param name="integrationTypes">Installation types for this command.</param>
/// <returns>The builder instance.</returns>
public SlashCommandBuilder WithIntegrationTypes(params ApplicationIntegrationType[] integrationTypes)
{
IntegrationTypes = integrationTypes is not null
? new HashSet<ApplicationIntegrationType>(integrationTypes)
: null;
return this;
}
/// <summary>
/// Sets context types this command can be executed in.
/// </summary>
/// <param name="contextTypes">Context types the command can be executed in.</param>
/// <returns>The builder instance.</returns>
public SlashCommandBuilder WithContextTypes(params InteractionContextType[] contextTypes)
{
ContextTypes = contextTypes is not null
? new HashSet<InteractionContextType>(contextTypes)
: null;
return this;
}
/// <summary>
/// Adds an option to the current slash command.
/// </summary>

View File

@@ -215,6 +215,7 @@ namespace Discord
/// <returns>
/// A <see cref="IMessageInteraction"/> if the message is a response to an interaction; otherwise <see langword="null"/>.
/// </returns>
[Obsolete("This property will be deprecated soon. Use IUserMessage.InteractionMetadata instead.")]
IMessageInteraction Interaction { get; }
/// <summary>

View File

@@ -21,6 +21,14 @@ namespace Discord
/// </returns>
IUserMessage ReferencedMessage { get; }
/// <summary>
/// Gets the interaction metadata for the interaction this message is a response to.
/// </summary>
/// <remarks>
/// Will be <see langword="null"/> if the message is not a response to an interaction.
/// </remarks>
IMessageInteractionMetadata InteractionMetadata { get; }
/// <summary>
/// Modifies this message.
/// </summary>