@@ -32,9 +32,15 @@ namespace Discord.Rest | |||
/// Initializes a new <see cref="DiscordRestClient"/> with the provided configuration. | |||
/// </summary> | |||
/// <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 | |||
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) | |||
=> 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 | |||
internal readonly bool APIOnInteractionCreation; | |||
public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, string body) | |||
=> IsValidHttpInteraction(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(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> | |||
public RestClientProvider RestClientProvider { get; set; } = DefaultRestClientProvider.Instance; | |||
public bool APIOnRestInteractionCreation { get; set; } = true; | |||
} | |||
} |
@@ -38,15 +38,26 @@ namespace Discord.Rest | |||
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) | |||
{ | |||
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) | |||
{ | |||
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; | |||
@@ -31,6 +31,10 @@ namespace Discord.Rest | |||
/// <summary> | |||
/// Gets the user who invoked the interaction. | |||
/// </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; } | |||
/// <inheritdoc/> | |||
@@ -51,11 +55,17 @@ namespace Discord.Rest | |||
/// <summary> | |||
/// Gets the channel that this interaction was executed in. | |||
/// </summary> | |||
/// <remarks> | |||
/// <see langword="null"/> if <see cref="DiscordRestConfig.APIOnRestInteractionCreation"/> is set to false. | |||
/// </remarks> | |||
public IRestMessageChannel Channel { get; private set; } | |||
/// <summary> | |||
/// Gets the guild this interaction was executed in. | |||
/// Gets the guild this interaction was executed in if applicable. | |||
/// </summary> | |||
/// <remarks> | |||
/// <see langword="null"/> if <see cref="DiscordRestConfig.APIOnRestInteractionCreation"/> is set to false. | |||
/// </remarks> | |||
public RestGuild Guild { get; private set; } | |||
/// <inheritdoc/> | |||
@@ -122,7 +132,10 @@ namespace Discord.Rest | |||
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) | |||
@@ -141,7 +154,10 @@ namespace Discord.Rest | |||
{ | |||
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 | |||
} | |||