@@ -34,12 +34,12 @@ namespace Discord.Rest | |||
/// <param name="config">The configuration to be used with the client.</param> | |||
public DiscordRestClient(DiscordRestConfig config) : base(config, CreateApiClient(config)) | |||
{ | |||
APIOnInteractionCreation = config.APIOnRestInteractionCreation; | |||
_apiOnCreation = config.APIOnRestInteractionCreation; | |||
} | |||
// used for socket client rest access | |||
internal DiscordRestClient(DiscordRestConfig config, API.DiscordRestApiClient api) : base(config, api) | |||
{ | |||
APIOnInteractionCreation = config.APIOnRestInteractionCreation; | |||
_apiOnCreation = config.APIOnRestInteractionCreation; | |||
} | |||
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config) | |||
@@ -88,7 +88,7 @@ namespace Discord.Rest | |||
#region Rest interactions | |||
internal readonly bool APIOnInteractionCreation; | |||
private readonly bool _apiOnCreation; | |||
public bool IsValidHttpInteraction(string publicKey, string signature, string timestamp, string body) | |||
=> IsValidHttpInteraction(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body)); | |||
@@ -121,8 +121,8 @@ namespace Discord.Rest | |||
/// A <see cref="RestInteraction"/> that represents the incoming http interaction. | |||
/// </returns> | |||
/// <exception cref="BadSignatureException">Thrown when the signature doesn't match the public key.</exception> | |||
public Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body) | |||
=> ParseHttpInteractionAsync(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body)); | |||
public Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body, bool? doApiCallOnCreation = null) | |||
=> ParseHttpInteractionAsync(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body), doApiCallOnCreation); | |||
/// <summary> | |||
/// Creates a <see cref="RestInteraction"/> from a http message. | |||
@@ -135,7 +135,7 @@ namespace Discord.Rest | |||
/// A <see cref="RestInteraction"/> that represents the incoming http interaction. | |||
/// </returns> | |||
/// <exception cref="BadSignatureException">Thrown when the signature doesn't match the public key.</exception> | |||
public async Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body) | |||
public async Task<RestInteraction> ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body, bool? doApiCallOnCreation = null) | |||
{ | |||
if (!IsValidHttpInteraction(publicKey, signature, timestamp, body)) | |||
{ | |||
@@ -146,12 +146,12 @@ namespace Discord.Rest | |||
using (var jsonReader = new JsonTextReader(textReader)) | |||
{ | |||
var model = Serializer.Deserialize<API.Interaction>(jsonReader); | |||
return await RestInteraction.CreateAsync(this, model); | |||
return await RestInteraction.CreateAsync(this, model, doApiCallOnCreation ?? _apiOnCreation); | |||
} | |||
} | |||
#endregion | |||
public async Task<RestApplication> GetApplicationInfoAsync(RequestOptions options = null) | |||
{ | |||
return _applicationInfo ??= await ClientHelper.GetApplicationInfoAsync(this, options).ConfigureAwait(false); | |||
@@ -27,20 +27,20 @@ namespace Discord.Rest | |||
{ | |||
} | |||
internal static async Task<RestCommandBaseData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal static async Task<RestCommandBaseData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
var entity = new RestCommandBaseData(client, model); | |||
await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal virtual async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal virtual async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
Name = model.Name; | |||
if (model.Resolved.IsSpecified && ResolvableData == null) | |||
{ | |||
ResolvableData = new RestResolvableData<Model>(); | |||
await ResolvableData.PopulateAsync(client, guild, channel, model).ConfigureAwait(false); | |||
await ResolvableData.PopulateAsync(client, guild, channel, model, doApiCall).ConfigureAwait(false); | |||
} | |||
} | |||
@@ -22,7 +22,7 @@ namespace Discord.Rest | |||
internal readonly Dictionary<ulong, Attachment> Attachments | |||
= new Dictionary<ulong, Attachment>(); | |||
internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model) | |||
internal async Task PopulateAsync(DiscordRestClient discord, RestGuild guild, IRestMessageChannel channel, T model, bool doApiCall) | |||
{ | |||
var resolved = model.Resolved.Value; | |||
@@ -38,7 +38,7 @@ namespace Discord.Rest | |||
if (resolved.Channels.IsSpecified) | |||
{ | |||
var channels = discord.APIOnInteractionCreation ? await guild.GetChannelsAsync().ConfigureAwait(false) : null; | |||
var channels = doApiCall ? await guild.GetChannelsAsync().ConfigureAwait(false) : null; | |||
foreach (var channelModel in resolved.Channels.Value) | |||
{ | |||
@@ -88,7 +88,7 @@ namespace Discord.Rest | |||
foreach (var msg in resolved.Messages.Value) | |||
{ | |||
channel ??= (IRestMessageChannel)(Channels.FirstOrDefault(x => x.Key == msg.Value.ChannelId).Value | |||
?? (discord.APIOnInteractionCreation | |||
?? (doApiCall | |||
? await discord.GetChannelAsync(msg.Value.ChannelId).ConfigureAwait(false) | |||
: null)); | |||
@@ -20,22 +20,22 @@ namespace Discord.Rest | |||
} | |||
internal new static async Task<RestMessageCommand> CreateAsync(DiscordRestClient client, Model model) | |||
internal new static async Task<RestMessageCommand> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestMessageCommand(client, model); | |||
await entity.UpdateAsync(client, model).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model) | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
await base.UpdateAsync(client, model).ConfigureAwait(false); | |||
await base.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
var dataModel = model.Data.IsSpecified | |||
? (DataModel)model.Data.Value | |||
: null; | |||
Data = await RestMessageCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); | |||
Data = await RestMessageCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false); | |||
} | |||
//IMessageCommandInteraction | |||
@@ -23,15 +23,15 @@ namespace Discord.Rest | |||
/// <b>Note</b> Not implemented for <see cref="RestMessageCommandData"/> | |||
/// </remarks> | |||
public override IReadOnlyCollection<IApplicationCommandInteractionDataOption> Options | |||
=> throw new System.NotImplementedException(); | |||
=> throw new NotImplementedException(); | |||
internal RestMessageCommandData(DiscordRestClient client, Model model) | |||
: base(client, model) { } | |||
internal new static async Task<RestMessageCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal new static async Task<RestMessageCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
var entity = new RestMessageCommandData(client, model); | |||
await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
@@ -23,14 +23,14 @@ namespace Discord.Rest | |||
{ | |||
} | |||
internal new static async Task<RestUserCommand> CreateAsync(DiscordRestClient client, Model model) | |||
internal new static async Task<RestUserCommand> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestUserCommand(client, model); | |||
await entity.UpdateAsync(client, model).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model) | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
await base.UpdateAsync(client, model).ConfigureAwait(false); | |||
@@ -38,7 +38,7 @@ namespace Discord.Rest | |||
? (DataModel)model.Data.Value | |||
: null; | |||
Data = await RestUserCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); | |||
Data = await RestUserCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false); | |||
} | |||
//IUserCommandInteractionData | |||
@@ -26,10 +26,10 @@ namespace Discord.Rest | |||
internal RestUserCommandData(DiscordRestClient client, Model model) | |||
: base(client, model) { } | |||
internal new static async Task<RestUserCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal new static async Task<RestUserCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
var entity = new RestUserCommandData(client, model); | |||
await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
@@ -37,15 +37,15 @@ namespace Discord.Rest | |||
Data = new RestMessageComponentData(dataModel); | |||
} | |||
internal new static async Task<RestMessageComponent> CreateAsync(DiscordRestClient client, Model model) | |||
internal new static async Task<RestMessageComponent> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestMessageComponent(client, model); | |||
await entity.UpdateAsync(client, model).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal override async Task UpdateAsync(DiscordRestClient discord, Model model) | |||
internal override async Task UpdateAsync(DiscordRestClient discord, Model model, bool doApiCall) | |||
{ | |||
await base.UpdateAsync(discord, model).ConfigureAwait(false); | |||
await base.UpdateAsync(discord, model, doApiCall).ConfigureAwait(false); | |||
if (model.Message.IsSpecified && model.ChannelId.IsSpecified) | |||
{ | |||
@@ -26,10 +26,10 @@ namespace Discord.Rest | |||
Data = new RestModalData(dataModel); | |||
} | |||
internal new static async Task<RestModal> CreateAsync(DiscordRestClient client, ModelBase model) | |||
internal new static async Task<RestModal> CreateAsync(DiscordRestClient client, ModelBase model, bool doApiCall) | |||
{ | |||
var entity = new RestModal(client, model); | |||
await entity.UpdateAsync(client, model); | |||
await entity.UpdateAsync(client, model, doApiCall); | |||
return entity; | |||
} | |||
@@ -100,11 +100,11 @@ namespace Discord.Rest | |||
: DateTime.UtcNow; | |||
} | |||
internal static async Task<RestInteraction> CreateAsync(DiscordRestClient client, Model model) | |||
internal static async Task<RestInteraction> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
if(model.Type == InteractionType.Ping) | |||
{ | |||
return await RestPingInteraction.CreateAsync(client, model); | |||
return await RestPingInteraction.CreateAsync(client, model, doApiCall); | |||
} | |||
if (model.Type == InteractionType.ApplicationCommand) | |||
@@ -118,26 +118,26 @@ namespace Discord.Rest | |||
return dataModel.Type switch | |||
{ | |||
ApplicationCommandType.Slash => await RestSlashCommand.CreateAsync(client, model).ConfigureAwait(false), | |||
ApplicationCommandType.Message => await RestMessageCommand.CreateAsync(client, model).ConfigureAwait(false), | |||
ApplicationCommandType.User => await RestUserCommand.CreateAsync(client, model).ConfigureAwait(false), | |||
ApplicationCommandType.Slash => await RestSlashCommand.CreateAsync(client, model, doApiCall).ConfigureAwait(false), | |||
ApplicationCommandType.Message => await RestMessageCommand.CreateAsync(client, model, doApiCall).ConfigureAwait(false), | |||
ApplicationCommandType.User => await RestUserCommand.CreateAsync(client, model, doApiCall).ConfigureAwait(false), | |||
_ => null | |||
}; | |||
} | |||
if (model.Type == InteractionType.MessageComponent) | |||
return await RestMessageComponent.CreateAsync(client, model).ConfigureAwait(false); | |||
return await RestMessageComponent.CreateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
if (model.Type == InteractionType.ApplicationCommandAutocomplete) | |||
return await RestAutocompleteInteraction.CreateAsync(client, model).ConfigureAwait(false); | |||
return await RestAutocompleteInteraction.CreateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
if (model.Type == InteractionType.ModalSubmit) | |||
return await RestModal.CreateAsync(client, model).ConfigureAwait(false); | |||
return await RestModal.CreateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
return null; | |||
} | |||
internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model) | |||
internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model, bool doApiCall) | |||
{ | |||
IsDMInteraction = !model.GuildId.IsSpecified; | |||
@@ -151,7 +151,7 @@ namespace Discord.Rest | |||
if (Guild == null && model.GuildId.IsSpecified) | |||
{ | |||
GuildId = model.GuildId.Value; | |||
if (discord.APIOnInteractionCreation) | |||
if (doApiCall) | |||
Guild = await discord.GetGuildAsync(model.GuildId.Value); | |||
else | |||
Guild = null; | |||
@@ -174,7 +174,7 @@ namespace Discord.Rest | |||
try | |||
{ | |||
ChannelId = model.ChannelId.Value; | |||
if (discord.APIOnInteractionCreation) | |||
if (doApiCall) | |||
Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); | |||
else | |||
Channel = null; | |||
@@ -201,9 +201,6 @@ namespace Discord.Rest | |||
return json.ToString(); | |||
} | |||
public async Task<RestGuild> GetGuildAsync() | |||
=> await | |||
/// <inheritdoc/> | |||
public abstract string Defer(bool ephemeral = false, RequestOptions options = null); | |||
/// <summary> | |||
@@ -18,10 +18,10 @@ namespace Discord.Rest | |||
{ | |||
} | |||
internal static new async Task<RestPingInteraction> CreateAsync(DiscordRestClient client, Model model) | |||
internal static new async Task<RestPingInteraction> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestPingInteraction(client, model.Id); | |||
await entity.UpdateAsync(client, model); | |||
await entity.UpdateAsync(client, model, doApiCall); | |||
return entity; | |||
} | |||
@@ -32,10 +32,10 @@ namespace Discord.Rest | |||
Data = new RestAutocompleteInteractionData(dataModel); | |||
} | |||
internal new static async Task<RestAutocompleteInteraction> CreateAsync(DiscordRestClient client, Model model) | |||
internal new static async Task<RestAutocompleteInteraction> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestAutocompleteInteraction(client, model); | |||
await entity.UpdateAsync(client, model).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
@@ -23,22 +23,22 @@ namespace Discord.Rest | |||
{ | |||
} | |||
internal new static async Task<RestSlashCommand> CreateAsync(DiscordRestClient client, Model model) | |||
internal new static async Task<RestSlashCommand> CreateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
var entity = new RestSlashCommand(client, model); | |||
await entity.UpdateAsync(client, model).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model) | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model, bool doApiCall) | |||
{ | |||
await base.UpdateAsync(client, model).ConfigureAwait(false); | |||
await base.UpdateAsync(client, model, doApiCall).ConfigureAwait(false); | |||
var dataModel = model.Data.IsSpecified | |||
? (DataModel)model.Data.Value | |||
: null; | |||
Data = await RestSlashCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); | |||
Data = await RestSlashCommandData.CreateAsync(client, dataModel, Guild, Channel, doApiCall).ConfigureAwait(false); | |||
} | |||
//ISlashCommandInteraction | |||
@@ -14,15 +14,15 @@ namespace Discord.Rest | |||
internal RestSlashCommandData(DiscordRestClient client, Model model) | |||
: base(client, model) { } | |||
internal static new async Task<RestSlashCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal static new async Task<RestSlashCommandData> CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
var entity = new RestSlashCommandData(client, model); | |||
await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); | |||
await entity.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false); | |||
return entity; | |||
} | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) | |||
internal override async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel, bool doApiCall) | |||
{ | |||
await base.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); | |||
await base.UpdateAsync(client, model, guild, channel, doApiCall).ConfigureAwait(false); | |||
Options = model.Options.IsSpecified | |||
? model.Options.Value.Select(x => new RestSlashCommandDataOption(this, x)).ToImmutableArray() | |||