From 18d93a2192ff7f00b9053d049f1e355025884730 Mon Sep 17 00:00:00 2001 From: quin lynch Date: Fri, 28 May 2021 05:50:26 -0300 Subject: [PATCH] Closes #5. Added message components to ModifyMessageArgs, abstracted SocketInteraction for #8 --- .../Entities/Messages/MessageProperties.cs | 5 ++ .../API/Rest/ModifyMessageParams.cs | 4 +- .../Entities/Messages/MessageHelper.cs | 3 +- .../API/Gateway/InteractionCreated.cs | 16 +++++-- .../DiscordSocketClient.cs | 31 +++++++----- .../SocketMessageComponent.cs | 21 ++++++++ .../Slash Commands/SocketSlashCommand.cs | 4 +- .../Slash Commands/SocketSlashCommandData.cs | 12 ++--- .../SocketSlashCommandDataOption.cs | 12 ++--- .../Entities/Interaction/SocketInteraction.cs | 48 ++++++++++++------- 10 files changed, 104 insertions(+), 52 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Messages/MessageProperties.cs b/src/Discord.Net.Core/Entities/Messages/MessageProperties.cs index b632d6a18..1e4846a94 100644 --- a/src/Discord.Net.Core/Entities/Messages/MessageProperties.cs +++ b/src/Discord.Net.Core/Entities/Messages/MessageProperties.cs @@ -21,5 +21,10 @@ namespace Discord /// Gets or sets the embed the message should display. /// public Optional Embed { get; set; } + + /// + /// Gets or sets the components for this message. + /// + public Optional Components { get; set; } } } diff --git a/src/Discord.Net.Rest/API/Rest/ModifyMessageParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyMessageParams.cs index fdff4de15..195525afc 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyMessageParams.cs +++ b/src/Discord.Net.Rest/API/Rest/ModifyMessageParams.cs @@ -1,4 +1,4 @@ -#pragma warning disable CS1591 +#pragma warning disable CS1591 using Newtonsoft.Json; namespace Discord.API.Rest @@ -10,5 +10,7 @@ namespace Discord.API.Rest public Optional Content { get; set; } [JsonProperty("embed")] public Optional Embed { get; set; } + [JsonProperty("components")] + public Optional Components { get; set; } } } diff --git a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs index b86a5dbf3..b66ac1a01 100644 --- a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs +++ b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs @@ -41,7 +41,8 @@ namespace Discord.Rest var apiArgs = new API.Rest.ModifyMessageParams { Content = args.Content, - Embed = args.Embed.IsSpecified ? args.Embed.Value.ToModel() : Optional.Create() + Embed = args.Embed.IsSpecified ? args.Embed.Value.ToModel() : Optional.Create(), + Components = args?.Components.GetValueOrDefault()?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional.Unspecified }; return await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, apiArgs, options).ConfigureAwait(false); } diff --git a/src/Discord.Net.WebSocket/API/Gateway/InteractionCreated.cs b/src/Discord.Net.WebSocket/API/Gateway/InteractionCreated.cs index 8c451a552..6e9ebb4fb 100644 --- a/src/Discord.Net.WebSocket/API/Gateway/InteractionCreated.cs +++ b/src/Discord.Net.WebSocket/API/Gateway/InteractionCreated.cs @@ -13,6 +13,9 @@ namespace Discord.API.Gateway [JsonProperty("id")] public ulong Id { get; set; } + [JsonProperty("application_id")] + public ulong ApplicationId { get; set; } + [JsonProperty("type")] public InteractionType Type { get; set; } @@ -20,18 +23,25 @@ namespace Discord.API.Gateway public Optional Data { get; set; } [JsonProperty("guild_id")] - public ulong GuildId { get; set; } + public Optional GuildId { get; set; } [JsonProperty("channel_id")] - public ulong ChannelId { get; set; } + public Optional ChannelId { get; set; } [JsonProperty("member")] - public GuildMember Member { get; set; } + public Optional Member { get; set; } + + [JsonProperty("user")] + public Optional User { get; set; } [JsonProperty("token")] public string Token { get; set; } [JsonProperty("version")] public int Version { get; set; } + + [JsonProperty("message")] + public Optional Message { get; set; } + } } diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 115367e6f..df8e79638 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1785,26 +1785,33 @@ namespace Discord.WebSocket await _gatewayLogger.DebugAsync("Received Dispatch (INTERACTION_CREATE)").ConfigureAwait(false); var data = (payload as JToken).ToObject(_serializer); - if (State.GetChannel(data.ChannelId) is SocketGuildChannel channel) + if (data.Member.IsSpecified && data.ChannelId.IsSpecified) { - var guild = channel.Guild; - if (!guild.IsSynced) + if (State.GetChannel(data.ChannelId.Value) is SocketGuildChannel channel) { - await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); - return; - } + var guild = channel.Guild; + if (!guild.IsSynced) + { + await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); + return; + } - var interaction = SocketInteraction.Create(this, data); + var interaction = SocketInteraction.Create(this, data); - if (this.AlwaysAcknowledgeInteractions) - await interaction.AcknowledgeAsync().ConfigureAwait(false); + if (this.AlwaysAcknowledgeInteractions) + await interaction.AcknowledgeAsync().ConfigureAwait(false); - await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false); + await TimedInvokeAsync(_interactionCreatedEvent, nameof(InteractionCreated), interaction).ConfigureAwait(false); + } + else + { + await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false); + return; + } } else { - await UnknownChannelAsync(type, data.ChannelId).ConfigureAwait(false); - return; + // DM TODO } } break; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs index 7d5ddfb17..6e97f9edd 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs @@ -14,6 +14,8 @@ namespace Discord.WebSocket { new public SocketMessageComponentData Data { get; } + public SocketMessage Message { get; private set; } + internal SocketMessageComponent(DiscordSocketClient client, Model model) : base(client, model.Id) { @@ -22,6 +24,8 @@ namespace Discord.WebSocket : null; this.Data = new SocketMessageComponentData(dataModel); + + } new internal static SocketMessageComponent Create(DiscordSocketClient client, Model model) @@ -31,6 +35,23 @@ namespace Discord.WebSocket return entity; } + internal override void Update(Model model) + { + base.Update(model); + + if (model.Message.IsSpecified) + { + if (this.Message == null) + { + this.Message = SocketMessage.Create(this.Discord, this.Discord.State, this.User, this.Channel, model.Message.Value); + } + else + { + this.Message.Update(this.Discord.State, model.Message.Value); + } + } + } + /// /// Responds to an Interaction. /// diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs index 9010d1eea..644b7c3fc 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs @@ -24,7 +24,7 @@ namespace Discord.WebSocket (model.Data.Value as JToken).ToObject() : null; - Data = SocketSlashCommandData.Create(client, dataModel, model.GuildId); + Data = SocketSlashCommandData.Create(client, dataModel, model.Id); } new internal static SocketInteraction Create(DiscordSocketClient client, Model model) @@ -40,7 +40,7 @@ namespace Discord.WebSocket (model.Data.Value as JToken).ToObject() : null; - this.Data.Update(data, this.Guild.Id); + this.Data.Update(data); base.Update(model); } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs index 098d81b86..79a2cfd53 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandData.cs @@ -18,27 +18,23 @@ namespace Discord.WebSocket /// public IReadOnlyCollection Options { get; private set; } - private ulong guildId; - internal SocketSlashCommandData(DiscordSocketClient client, ulong id) : base(client, id) { } - internal static SocketSlashCommandData Create(DiscordSocketClient client, Model model, ulong guildId) + internal static SocketSlashCommandData Create(DiscordSocketClient client, Model model, ulong id) { var entity = new SocketSlashCommandData(client, model.Id); - entity.Update(model, guildId); + entity.Update(model); return entity; } - internal void Update(Model model, ulong guildId) + internal void Update(Model model) { this.Name = model.Name; - this.guildId = guildId; - this.Options = model.Options.IsSpecified - ? model.Options.Value.Select(x => new SocketSlashCommandDataOption(x, this.Discord, guildId)).ToImmutableArray() + ? model.Options.Value.Select(x => new SocketSlashCommandDataOption(x, this.Discord)).ToImmutableArray() : null; } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandDataOption.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandDataOption.cs index 89f1443be..691d8951c 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandDataOption.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommandDataOption.cs @@ -25,18 +25,16 @@ namespace Discord.WebSocket public IReadOnlyCollection Options { get; private set; } private DiscordSocketClient discord; - private ulong guild; internal SocketSlashCommandDataOption() { } - internal SocketSlashCommandDataOption(Model model, DiscordSocketClient discord, ulong guild) + internal SocketSlashCommandDataOption(Model model, DiscordSocketClient discord) { this.Name = model.Name; this.Value = model.Value.IsSpecified ? model.Value.Value : null; this.discord = discord; - this.guild = guild; this.Options = model.Options.IsSpecified - ? model.Options.Value.Select(x => new SocketSlashCommandDataOption(x, discord, guild)).ToImmutableArray() + ? model.Options.Value.Select(x => new SocketSlashCommandDataOption(x, discord)).ToImmutableArray() : null; } @@ -52,7 +50,7 @@ namespace Discord.WebSocket { if (option.Value is ulong id) { - var guild = option.discord.GetGuild(option.guild); + var guild = option.discord.GetGuild(id); if (guild == null) return null; @@ -67,7 +65,7 @@ namespace Discord.WebSocket { if (option.Value is ulong id) { - var guild = option.discord.GetGuild(option.guild); + var guild = option.discord.GetGuild(id); if (guild == null) return null; @@ -82,7 +80,7 @@ namespace Discord.WebSocket { if(option.Value is ulong id) { - var guild = option.discord.GetGuild(option.guild); + var guild = option.discord.GetGuild(id); if (guild == null) return null; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index 96f0bca10..62f0debef 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -14,21 +14,14 @@ namespace Discord.WebSocket public abstract class SocketInteraction : SocketEntity, IDiscordInteraction { /// - /// The this interaction was used in. + /// The this interaction was used in. /// - public SocketGuild Guild - => Discord.GetGuild(GuildId); + public ISocketMessageChannel Channel { get; private set; } /// - /// The this interaction was used in. + /// The who triggered this interaction. /// - public SocketTextChannel Channel - => Guild.GetTextChannel(ChannelId); - - /// - /// The who triggered this interaction. - /// - public SocketGuildUser User { get; private set; } + public SocketUser User { get; private set; } /// /// The type of this interaction. @@ -58,9 +51,8 @@ namespace Discord.WebSocket public bool IsValidToken => CheckToken(); - private ulong GuildId { get; set; } - private ulong ChannelId { get; set; } - private ulong UserId { get; set; } + private ulong? GuildId { get; set; } + private ulong? ChannelId { get; set; } internal SocketInteraction(DiscordSocketClient client, ulong id) : base(client, id) @@ -83,15 +75,35 @@ namespace Discord.WebSocket ? model.Data.Value : null; - this.GuildId = model.GuildId; - this.ChannelId = model.ChannelId; + this.GuildId = model.GuildId.ToNullable(); + this.ChannelId = model.ChannelId.ToNullable(); this.Token = model.Token; this.Version = model.Version; - this.UserId = model.Member.User.Id; this.Type = model.Type; if (this.User == null) - this.User = SocketGuildUser.Create(this.Guild, Discord.State, model.Member); // Change from getter. + { + if (model.Member.IsSpecified && model.GuildId.IsSpecified) + { + this.User = SocketGuildUser.Create(Discord.State.GetGuild(this.GuildId.Value), Discord.State, model.Member.Value); + } + else + { + this.User = SocketGlobalUser.Create(this.Discord, this.Discord.State, model.User.Value); + } + } + + if (this.Channel == null) + { + if (model.ChannelId.IsSpecified) + { + this.Channel = Discord.State.GetChannel(model.ChannelId.Value) as ISocketMessageChannel; + } + else + { + this.Channel = Discord.State.GetDMChannel(this.User.Id); + } + } } ///