diff --git a/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs b/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs index 9017d310f..a2dbe0e5f 100644 --- a/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs +++ b/src/Discord.Net.Core/Entities/Interactions/IDiscordInteraction.cs @@ -52,6 +52,9 @@ namespace Discord /// /// Gets the preferred locale of the invoking User. /// + /// + /// This property returns if the interaction is a REST ping interaction. + /// string UserLocale { get; } /// @@ -67,6 +70,27 @@ namespace Discord /// bool IsDMInteraction { get; } + /// + /// Gets the ID of the channel this interaction was executed in. + /// + /// + /// This property returns if the interaction is a REST ping interaction. + /// + ulong? ChannelId { get; } + + /// + /// Gets the ID of the guild this interaction was executed in. + /// + /// + /// This property returns if the interaction was not executed in a guild. + /// + ulong? GuildId { get; } + + /// + /// Gets the ID of the application this interaction is for. + /// + ulong ApplicationId { get; } + /// /// Responds to an Interaction with type . /// diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs index 59adc0347..43d13f521 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text; using System.Threading.Tasks; using Model = Discord.API.Interaction; @@ -17,8 +16,8 @@ namespace Discord.Rest public abstract class RestInteraction : RestEntity, IDiscordInteraction { // Added so channel & guild methods don't need a client reference - private Func> _getChannel = null; - private Func> _getGuild = null; + private Func> _getChannel; + private Func> _getGuild; /// public InteractionType Type { get; private set; } @@ -56,30 +55,17 @@ namespace Discord.Rest public bool IsValidToken => InteractionHelper.CanRespondOrFollowup(this); - /// - /// Gets the ID of the channel this interaction was executed in. - /// - /// - /// if the interaction was not executed in a guild. - /// - public ulong? ChannelId { get; private set; } = null; - /// /// Gets the channel that this interaction was executed in. /// /// - /// if is set to false. + /// This property will be if is set to false. /// Call to set this property and get the interaction channel. /// public IRestMessageChannel Channel { get; private set; } - /// - /// Gets the ID of the guild this interaction was executed in if applicable. - /// - /// - /// if the interaction was not executed in a guild. - /// - public ulong? GuildId { get; private set; } = null; + /// + public ulong? ChannelId { get; private set; } /// /// Gets the guild this interaction was executed in if applicable. @@ -90,12 +76,18 @@ namespace Discord.Rest /// public RestGuild Guild { get; private set; } + /// + public ulong? GuildId { get; private set; } + /// public bool HasResponded { get; protected set; } /// public bool IsDMInteraction { get; private set; } + /// + public ulong ApplicationId { get; private set; } + internal RestInteraction(BaseDiscordClient discord, ulong id) : base(discord, id) { @@ -143,57 +135,67 @@ namespace Discord.Rest internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model, bool doApiCall) { - IsDMInteraction = !model.GuildId.IsSpecified; + ChannelId = model.ChannelId.IsSpecified + ? model.ChannelId.Value + : null; + + GuildId = model.GuildId.IsSpecified + ? model.GuildId.Value + : null; + + IsDMInteraction = GuildId is null; Data = model.Data.IsSpecified ? model.Data.Value : null; + Token = model.Token; Version = model.Version; Type = model.Type; + ApplicationId = model.ApplicationId; - if (Guild == null && model.GuildId.IsSpecified) + if (Guild is null && GuildId is not null) { - GuildId = model.GuildId.Value; if (doApiCall) - Guild = await discord.GetGuildAsync(model.GuildId.Value); + Guild = await discord.GetGuildAsync(GuildId.Value); else { Guild = null; - _getGuild = new(async (opt, ul) => await discord.GetGuildAsync(ul, opt)); + _getGuild = async (opt, ul) => await discord.GetGuildAsync(ul, opt); } } - if (User == null) + if (User is null) { - if (model.Member.IsSpecified && model.GuildId.IsSpecified) + if (model.Member.IsSpecified && GuildId is not null) { - User = RestGuildUser.Create(Discord, Guild, model.Member.Value, (Guild is null) ? model.GuildId.Value : null); + User = RestGuildUser.Create(Discord, Guild, model.Member.Value, GuildId); } else { User = RestUser.Create(Discord, model.User.Value); } } + - if (Channel == null && model.ChannelId.IsSpecified) + if (Channel is null && ChannelId is not null) { try { - ChannelId = model.ChannelId.Value; if (doApiCall) - Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); + Channel = (IRestMessageChannel)await discord.GetChannelAsync(ChannelId.Value); else { - _getChannel = new(async (opt, ul) => + Channel = null; + + _getChannel = async (opt, ul) => { if (Guild is null) return (IRestMessageChannel)await discord.GetChannelAsync(ul, opt); - else // get a guild channel if the guild is set. - return (IRestMessageChannel)await Guild.GetChannelAsync(ul, opt); - }); - Channel = null; + // get a guild channel if the guild is set. + return (IRestMessageChannel)await Guild.GetChannelAsync(ul, opt); + }; } } catch (HttpException x) when (x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore @@ -222,7 +224,7 @@ namespace Discord.Rest /// Gets the channel this interaction was executed in. Will be a DM channel if the interaction was executed in DM. /// /// - /// Calling this method succesfully will populate the property. + /// Calling this method successfully will populate the property. /// After this, further calls to this method will no longer call the API, and depend on the value set in . /// /// The request options for this request. @@ -230,20 +232,16 @@ namespace Discord.Rest /// Thrown if no channel can be received. public async Task GetChannelAsync(RequestOptions options = null) { - if (IsDMInteraction && Channel is null) + if (Channel is not null) + return Channel; + + if (IsDMInteraction) { - var channel = await User.CreateDMChannelAsync(options); - Channel = channel; + Channel = await User.CreateDMChannelAsync(options); } - - else if (Channel is null) + else if (ChannelId is not null) { - var channel = await _getChannel(options, ChannelId.Value); - - if (channel is null) - throw new InvalidOperationException("The interaction channel was not able to be retrieved."); - Channel = channel; - + Channel = await _getChannel(options, ChannelId.Value) ?? throw new InvalidOperationException("The interaction channel was not able to be retrieved."); _getChannel = null; // get rid of it, we don't need it anymore. } @@ -254,20 +252,19 @@ namespace Discord.Rest /// Gets the guild this interaction was executed in if applicable. /// /// - /// Calling this method succesfully will populate the property. + /// Calling this method successfully will populate the property. /// After this, further calls to this method will no longer call the API, and depend on the value set in . /// /// The request options for this request. /// The guild this interaction was executed in. if the interaction was executed inside DM. public async Task GetGuildAsync(RequestOptions options) { - if (IsDMInteraction) + if (GuildId is null) return null; - if (Guild is null) - Guild = await _getGuild(options, GuildId.Value); - + Guild ??= await _getGuild(options, GuildId.Value); _getGuild = null; // get rid of it, we don't need it anymore. + return Guild; } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index 5b2da04f5..f8eb6b12e 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -24,20 +24,11 @@ namespace Discord.WebSocket /// public ISocketMessageChannel Channel { get; private set; } - /// - /// Gets the ID of the channel this interaction was used in. - /// - /// - /// This property is exposed in cases where the bot scope is not provided, so the channel entity cannot be retrieved. - ///
- /// To get the channel, you can call - /// as this method makes a request for a if nothing was found in cache. - ///
+ /// public ulong? ChannelId { get; private set; } /// /// Gets the who triggered this interaction. - /// This property will be if the bot scope isn't used. /// public SocketUser User { get; private set; } @@ -74,6 +65,12 @@ namespace Discord.WebSocket /// public bool IsDMInteraction { get; private set; } + /// + public ulong? GuildId { get; private set; } + + /// + public ulong ApplicationId { get; private set; } + internal SocketInteraction(DiscordSocketClient client, ulong id, ISocketMessageChannel channel, SocketUser user) : base(client, id) { @@ -119,13 +116,21 @@ namespace Discord.WebSocket internal virtual void Update(Model model) { - IsDMInteraction = !model.GuildId.IsSpecified; + ChannelId = model.ChannelId.IsSpecified + ? model.ChannelId.Value + : null; - ChannelId = model.ChannelId.ToNullable(); + GuildId = model.GuildId.IsSpecified + ? model.GuildId.Value + : null; + + IsDMInteraction = GuildId is null; + ApplicationId = model.ApplicationId; Data = model.Data.IsSpecified ? model.Data.Value : null; + Token = model.Token; Version = model.Version; Type = model.Type; @@ -133,6 +138,7 @@ namespace Discord.WebSocket UserLocale = model.UserLocale.IsSpecified ? model.UserLocale.Value : null; + GuildLocale = model.GuildLocale.IsSpecified ? model.GuildLocale.Value : null; @@ -392,7 +398,7 @@ namespace Discord.WebSocket /// The request options for this request. /// A task that represents the asynchronous operation of responding to the interaction. public abstract Task RespondWithModalAsync(Modal modal, RequestOptions options = null); - #endregion +#endregion /// /// Attepts to get the channel this interaction was executed in. @@ -416,7 +422,7 @@ namespace Discord.WebSocket catch(HttpException ex) when (ex.DiscordCode == DiscordErrorCode.MissingPermissions) { return null; } // bot can't view that channel, return null instead of throwing. } - #region IDiscordInteraction +#region IDiscordInteraction /// IUser IDiscordInteraction.User => User; @@ -446,6 +452,6 @@ namespace Discord.WebSocket async Task IDiscordInteraction.FollowupWithFileAsync(FileAttachment attachment, string text, Embed[] embeds, bool isTTS, bool ephemeral, AllowedMentions allowedMentions, MessageComponent components, Embed embed, RequestOptions options) => await FollowupWithFileAsync(attachment, text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options).ConfigureAwait(false); #endif - #endregion +#endregion } }