| @@ -32,9 +32,15 @@ namespace Discord.Rest | |||||
| /// Initializes a new <see cref="DiscordRestClient"/> with the provided configuration. | /// Initializes a new <see cref="DiscordRestClient"/> with the provided configuration. | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="config">The configuration to be used with the client.</param> | /// <param name="config">The configuration to be used with the client.</param> | ||||
| public DiscordRestClient(DiscordRestConfig config) : base(config, CreateApiClient(config)) { } | |||||
| public DiscordRestClient(DiscordRestConfig config) : base(config, CreateApiClient(config)) | |||||
| { | |||||
| APIOnInteractionCreation = config.APIOnRestInteractionCreation; | |||||
| } | |||||
| // used for socket client rest access | // used for socket client rest access | ||||
| internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) { } | |||||
| internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) | |||||
| { | |||||
| APIOnInteractionCreation = config.APIOnRestInteractionCreation; | |||||
| } | |||||
| private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) | private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) | ||||
| => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, serializer: Serializer, useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback); | => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent, serializer: Serializer, useSystemClock: config.UseSystemClock, defaultRatelimitCallback: config.DefaultRatelimitCallback); | ||||
| @@ -82,6 +88,8 @@ namespace Discord.Rest | |||||
| #region Rest interactions | #region Rest interactions | ||||
| internal readonly bool APIOnInteractionCreation; | |||||
| public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, string body) | public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, string body) | ||||
| => IsValidHttpInteraction(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body)); | => IsValidHttpInteraction(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body)); | ||||
| public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, byte[] body) | public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, byte[] body) | ||||
| @@ -9,5 +9,7 @@ namespace Discord.Rest | |||||
| { | { | ||||
| /// <summary> Gets or sets the provider used to generate new REST connections. </summary> | /// <summary> Gets or sets the provider used to generate new REST connections. </summary> | ||||
| public RestClientProvider RestClientProvider { get; set; } = DefaultRestClientProvider.Instance; | public RestClientProvider RestClientProvider { get; set; } = DefaultRestClientProvider.Instance; | ||||
| public bool APIOnRestInteractionCreation { get; set; } = true; | |||||
| } | } | ||||
| } | } | ||||
| @@ -38,15 +38,26 @@ namespace Discord.Rest | |||||
| if (resolved.Channels.IsSpecified) | if (resolved.Channels.IsSpecified) | ||||
| { | { | ||||
| var channels = await guild.GetChannelsAsync().ConfigureAwait(false); | |||||
| var channels = discord.APIOnInteractionCreation ? await guild.GetChannelsAsync().ConfigureAwait(false) : null; | |||||
| foreach (var channelModel in resolved.Channels.Value) | foreach (var channelModel in resolved.Channels.Value) | ||||
| { | { | ||||
| var restChannel = channels.FirstOrDefault(x => x.Id == channelModel.Value.Id); | |||||
| if (channels != null) | |||||
| { | |||||
| var guildChannel = channels.FirstOrDefault(x => x.Id == channelModel.Value.Id); | |||||
| restChannel.Update(channelModel.Value); | |||||
| guildChannel.Update(channelModel.Value); | |||||
| Channels.Add(ulong.Parse(channelModel.Key), restChannel); | |||||
| Channels.Add(ulong.Parse(channelModel.Key), guildChannel); | |||||
| } | |||||
| else | |||||
| { | |||||
| var restChannel = RestChannel.Create(discord, channelModel.Value); | |||||
| restChannel.Update(channelModel.Value); | |||||
| Channels.Add(ulong.Parse(channelModel.Key), restChannel); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -76,7 +87,10 @@ namespace Discord.Rest | |||||
| { | { | ||||
| foreach (var msg in resolved.Messages.Value) | foreach (var msg in resolved.Messages.Value) | ||||
| { | { | ||||
| channel ??= (IRestMessageChannel)(Channels.FirstOrDefault(x => x.Key == msg.Value.ChannelId).Value ?? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false)); | |||||
| channel ??= (IRestMessageChannel)(Channels.FirstOrDefault(x => x.Key == msg.Value.ChannelId).Value | |||||
| ?? (discord.APIOnInteractionCreation | |||||
| ? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false) | |||||
| : null)); | |||||
| RestUser author; | RestUser author; | ||||
| @@ -31,6 +31,10 @@ namespace Discord.Rest | |||||
| /// <summary> | /// <summary> | ||||
| /// Gets the user who invoked the interaction. | /// Gets the user who invoked the interaction. | ||||
| /// </summary> | /// </summary> | ||||
| /// <remarks> | |||||
| /// If this user is an <see cref="RestGuildUser"/> and <see cref="DiscordRestConfig.APIOnRestInteractionCreation"/> is set to false, | |||||
| /// <see cref="RestGuildUser.Guild"/> will return <see langword="null"/> | |||||
| /// </remarks> | |||||
| public RestUser User { get; private set; } | public RestUser User { get; private set; } | ||||
| /// <inheritdoc/> | /// <inheritdoc/> | ||||
| @@ -51,11 +55,17 @@ namespace Discord.Rest | |||||
| /// <summary> | /// <summary> | ||||
| /// Gets the channel that this interaction was executed in. | /// Gets the channel that this interaction was executed in. | ||||
| /// </summary> | /// </summary> | ||||
| /// <remarks> | |||||
| /// <see langword="null"/> if <see cref="DiscordRestConfig.APIOnRestInteractionCreation"/> is set to false. | |||||
| /// </remarks> | |||||
| public IRestMessageChannel Channel { get; private set; } | public IRestMessageChannel Channel { get; private set; } | ||||
| /// <summary> | /// <summary> | ||||
| /// Gets the guild this interaction was executed in. | |||||
| /// Gets the guild this interaction was executed in if applicable. | |||||
| /// </summary> | /// </summary> | ||||
| /// <remarks> | |||||
| /// <see langword="null"/> if <see cref="DiscordRestConfig.APIOnRestInteractionCreation"/> is set to false. | |||||
| /// </remarks> | |||||
| public RestGuild Guild { get; private set; } | public RestGuild Guild { get; private set; } | ||||
| /// <inheritdoc/> | /// <inheritdoc/> | ||||
| @@ -122,7 +132,10 @@ namespace Discord.Rest | |||||
| if(Guild == null && model.GuildId.IsSpecified) | if(Guild == null && model.GuildId.IsSpecified) | ||||
| { | { | ||||
| Guild = await discord.GetGuildAsync(model.GuildId.Value); | |||||
| if (discord.APIOnInteractionCreation) | |||||
| Guild = await discord.GetGuildAsync(model.GuildId.Value); | |||||
| else | |||||
| Guild = null; | |||||
| } | } | ||||
| if (User == null) | if (User == null) | ||||
| @@ -141,7 +154,10 @@ namespace Discord.Rest | |||||
| { | { | ||||
| try | try | ||||
| { | { | ||||
| Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); | |||||
| if (discord.APIOnInteractionCreation) | |||||
| Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); | |||||
| else | |||||
| Channel = null; | |||||
| } | } | ||||
| catch(HttpException x) when(x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore | catch(HttpException x) when(x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore | ||||
| } | } | ||||