From e83fdbf4fa70a358cff93d60c68a4893bc3c641e Mon Sep 17 00:00:00 2001 From: Armano den Boef Date: Sat, 30 Apr 2022 16:42:35 +0200 Subject: [PATCH] init --- src/Discord.Net.Rest/DiscordRestClient.cs | 6 +-- .../CommandBase/RestCommandBase.cs | 8 +-- .../CommandBase/RestCommandBaseData.cs | 8 +-- .../MessageCommands/RestMessageCommand.cs | 10 ++-- .../MessageCommands/RestMessageCommandData.cs | 4 +- .../UserCommands/RestUserCommand.cs | 10 ++-- .../UserCommands/RestUserCommandData.cs | 4 +- .../MessageComponents/RestMessageComponent.cs | 8 +-- .../Entities/Interactions/Modals/RestModal.cs | 4 +- .../Entities/Interactions/RestInteraction.cs | 50 +++++++++++++------ .../Interactions/RestPingInteraction.cs | 4 +- .../RestAutocompleteInteraction.cs | 4 +- .../SlashCommands/RestSlashCommand.cs | 10 ++-- .../SlashCommands/RestSlashCommandData.cs | 8 +-- 14 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/Discord.Net.Rest/DiscordRestClient.cs b/src/Discord.Net.Rest/DiscordRestClient.cs index b1948f80a..f01502d2c 100644 --- a/src/Discord.Net.Rest/DiscordRestClient.cs +++ b/src/Discord.Net.Rest/DiscordRestClient.cs @@ -113,7 +113,7 @@ namespace Discord.Rest /// A that represents the incoming http interaction. /// /// Thrown when the signature doesn't match the public key. - public Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body) + public RestInteraction ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, string body) => ParseHttpInteractionAsync(publicKey, signature, timestamp, Encoding.UTF8.GetBytes(body)); /// @@ -127,7 +127,7 @@ namespace Discord.Rest /// A that represents the incoming http interaction. /// /// Thrown when the signature doesn't match the public key. - public async Task ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body) + public RestInteraction ParseHttpInteractionAsync(string publicKey, string signature, string timestamp, byte[] body) { if (!IsValidHttpInteraction(publicKey, signature, timestamp, body)) { @@ -138,7 +138,7 @@ namespace Discord.Rest using (var jsonReader = new JsonTextReader(textReader)) { var model = Serializer.Deserialize(jsonReader); - return await RestInteraction.CreateAsync(this, model); + return RestInteraction.Create(this, model); } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs index 196416f0e..0bfee24fb 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs @@ -39,16 +39,16 @@ namespace Discord.Rest { } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestCommandBase Create(DiscordRestClient client, Model model) { var entity = new RestCommandBase(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } - internal override async Task UpdateAsync(DiscordRestClient client, Model model) + internal override void Update(DiscordRestClient client, Model model) { - await base.UpdateAsync(client, model).ConfigureAwait(false); + base.Update(client, model); } /// diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBaseData.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBaseData.cs index 4227c802a..3ee2d2256 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBaseData.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBaseData.cs @@ -27,20 +27,20 @@ namespace Discord.Rest { } - internal static async Task CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal static RestCommandBaseData Create(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { var entity = new RestCommandBaseData(client, model); - await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); + entity.Update(client, model, guild, channel); return entity; } - internal virtual async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal virtual void Update(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { Name = model.Name; if (model.Resolved.IsSpecified && ResolvableData == null) { ResolvableData = new RestResolvableData(); - await ResolvableData.PopulateAsync(client, guild, channel, model).ConfigureAwait(false); + ResolvableData.PopulateAsync(client, guild, channel, model).ConfigureAwait(false); } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommand.cs index 609fe0829..6fa1d570a 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommand.cs @@ -20,22 +20,22 @@ namespace Discord.Rest } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestMessageCommand Create(DiscordRestClient client, Model model) { var entity = new RestMessageCommand(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } - internal override async Task UpdateAsync(DiscordRestClient client, Model model) + internal override void Update(DiscordRestClient client, Model model) { - await base.UpdateAsync(client, model).ConfigureAwait(false); + base.Update(client, model); var dataModel = model.Data.IsSpecified ? (DataModel)model.Data.Value : null; - Data = await RestMessageCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); + Data = RestMessageCommandData.Create(client, dataModel, Guild, Channel); } //IMessageCommandInteraction diff --git a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommandData.cs b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommandData.cs index 127d539d9..a520d0a69 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommandData.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/MessageCommands/RestMessageCommandData.cs @@ -28,10 +28,10 @@ namespace Discord.Rest internal RestMessageCommandData(DiscordRestClient client, Model model) : base(client, model) { } - internal new static async Task CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal new static RestMessageCommandData Create(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { var entity = new RestMessageCommandData(client, model); - await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); + entity.Update(client, model, guild, channel); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommand.cs index 7f55fd61b..adecb0632 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommand.cs @@ -23,22 +23,22 @@ namespace Discord.Rest { } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestUserCommand Create(DiscordRestClient client, Model model) { var entity = new RestUserCommand(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } - internal override async Task UpdateAsync(DiscordRestClient client, Model model) + internal override void Update(DiscordRestClient client, Model model) { - await base.UpdateAsync(client, model).ConfigureAwait(false); + base.Update(client, model); var dataModel = model.Data.IsSpecified ? (DataModel)model.Data.Value : null; - Data = await RestUserCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); + Data = RestUserCommandData.Create(client, dataModel, Guild, Channel); } //IUserCommandInteractionData diff --git a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommandData.cs b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommandData.cs index e18499d42..d1f442fb1 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommandData.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/ContextMenuCommands/UserCommands/RestUserCommandData.cs @@ -26,10 +26,10 @@ namespace Discord.Rest internal RestUserCommandData(DiscordRestClient client, Model model) : base(client, model) { } - internal new static async Task CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal new static RestUserCommandData Create(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { var entity = new RestUserCommandData(client, model); - await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); + entity.Update(client, model, guild, channel); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index 002510eac..883945834 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -37,15 +37,15 @@ namespace Discord.Rest Data = new RestMessageComponentData(dataModel); } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestMessageComponent Create(DiscordRestClient client, Model model) { var entity = new RestMessageComponent(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } - internal override async Task UpdateAsync(DiscordRestClient discord, Model model) + internal override void Update(DiscordRestClient discord, Model model) { - await base.UpdateAsync(discord, model).ConfigureAwait(false); + base.Update(discord, model); if (model.Message.IsSpecified && model.ChannelId.IsSpecified) { diff --git a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs index 5f54fe051..33e097196 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/Modals/RestModal.cs @@ -26,10 +26,10 @@ namespace Discord.Rest Data = new RestModalData(dataModel); } - internal new static async Task CreateAsync(DiscordRestClient client, ModelBase model) + internal new static RestModal Create(DiscordRestClient client, ModelBase model) { var entity = new RestModal(client, model); - await entity.UpdateAsync(client, model); + entity.Update(client, model); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs index 8a8921abe..0ff768926 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestInteraction.cs @@ -28,10 +28,16 @@ namespace Discord.Rest /// public int Version { get; private set; } + private Lazy _user; + /// /// Gets the user who invoked the interaction. /// - public RestUser User { get; private set; } + public RestUser User + { + get + => _user.Value; + } /// public string UserLocale { get; private set; } @@ -48,15 +54,27 @@ namespace Discord.Rest public bool IsValidToken => InteractionHelper.CanRespondOrFollowup(this); + private Lazy _channel; + /// /// Gets the channel that this interaction was executed in. /// - public IRestMessageChannel Channel { get; private set; } + public IRestMessageChannel Channel + { + get + => _channel.Value; + } + + private Lazy _guild; /// /// Gets the guild this interaction was executed in. /// - public RestGuild Guild { get; private set; } + public RestGuild Guild + { + get + => _guild.Value; + } /// public bool HasResponded { get; protected set; } @@ -72,11 +90,11 @@ namespace Discord.Rest : DateTime.UtcNow; } - internal static async Task CreateAsync(DiscordRestClient client, Model model) + internal static RestInteraction Create(DiscordRestClient client, Model model) { if(model.Type == InteractionType.Ping) { - return await RestPingInteraction.CreateAsync(client, model); + return RestPingInteraction.Create(client, model); } if (model.Type == InteractionType.ApplicationCommand) @@ -90,26 +108,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 => RestSlashCommand.Create(client, model), + ApplicationCommandType.Message => RestMessageCommand.Create(client, model), + ApplicationCommandType.User => RestUserCommand.Create(client, model), _ => null }; } if (model.Type == InteractionType.MessageComponent) - return await RestMessageComponent.CreateAsync(client, model).ConfigureAwait(false); + return RestMessageComponent.Create(client, model); if (model.Type == InteractionType.ApplicationCommandAutocomplete) - return await RestAutocompleteInteraction.CreateAsync(client, model).ConfigureAwait(false); + return RestAutocompleteInteraction.Create(client, model); if (model.Type == InteractionType.ModalSubmit) - return await RestModal.CreateAsync(client, model).ConfigureAwait(false); + return RestModal.Create(client, model); return null; } - internal virtual async Task UpdateAsync(DiscordRestClient discord, Model model) + internal virtual void Update(DiscordRestClient discord, Model model) { IsDMInteraction = !model.GuildId.IsSpecified; @@ -122,18 +140,18 @@ namespace Discord.Rest if(Guild == null && model.GuildId.IsSpecified) { - Guild = await discord.GetGuildAsync(model.GuildId.Value); + _guild = new(() => discord.GetGuildAsync(model.GuildId.Value).GetAwaiter().GetResult()); // tbd } if (User == null) { if (model.Member.IsSpecified && model.GuildId.IsSpecified) { - User = RestGuildUser.Create(Discord, Guild, model.Member.Value); + _user = new(() => RestGuildUser.Create(Discord, Guild, model.Member.Value)); } else { - User = RestUser.Create(Discord, model.User.Value); + _user = new(() => RestUser.Create(Discord, model.User.Value)); } } @@ -141,7 +159,7 @@ namespace Discord.Rest { try { - Channel = (IRestMessageChannel)await discord.GetChannelAsync(model.ChannelId.Value); + _channel = new(() => (IRestMessageChannel)discord.GetChannelAsync(model.ChannelId.Value).GetAwaiter().GetResult()); // tbd } catch(HttpException x) when(x.DiscordCode == DiscordErrorCode.MissingPermissions) { } // ignore } diff --git a/src/Discord.Net.Rest/Entities/Interactions/RestPingInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/RestPingInteraction.cs index bd15bc2d3..56a54a88e 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/RestPingInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/RestPingInteraction.cs @@ -18,10 +18,10 @@ namespace Discord.Rest { } - internal static new async Task CreateAsync(DiscordRestClient client, Model model) + internal static new RestPingInteraction Create(DiscordRestClient client, Model model) { var entity = new RestPingInteraction(client, model.Id); - await entity.UpdateAsync(client, model); + entity.Update(client, model); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs index 24dbae37a..def1a74f7 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs @@ -32,10 +32,10 @@ namespace Discord.Rest Data = new RestAutocompleteInteractionData(dataModel); } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestAutocompleteInteraction Create(DiscordRestClient client, Model model) { var entity = new RestAutocompleteInteraction(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommand.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommand.cs index 21184fcf6..cd52201ae 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommand.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommand.cs @@ -23,22 +23,22 @@ namespace Discord.Rest { } - internal new static async Task CreateAsync(DiscordRestClient client, Model model) + internal new static RestSlashCommand Create(DiscordRestClient client, Model model) { var entity = new RestSlashCommand(client, model); - await entity.UpdateAsync(client, model).ConfigureAwait(false); + entity.Update(client, model); return entity; } - internal override async Task UpdateAsync(DiscordRestClient client, Model model) + internal override void Update(DiscordRestClient client, Model model) { - await base.UpdateAsync(client, model).ConfigureAwait(false); + base.Update(client, model); var dataModel = model.Data.IsSpecified ? (DataModel)model.Data.Value : null; - Data = await RestSlashCommandData.CreateAsync(client, dataModel, Guild, Channel).ConfigureAwait(false); + Data = RestSlashCommandData.Create(client, dataModel, Guild, Channel); } //ISlashCommandInteraction diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandData.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandData.cs index f967cc628..50879a62c 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandData.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestSlashCommandData.cs @@ -14,15 +14,15 @@ namespace Discord.Rest internal RestSlashCommandData(DiscordRestClient client, Model model) : base(client, model) { } - internal static new async Task CreateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal static new RestSlashCommandData Create(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { var entity = new RestSlashCommandData(client, model); - await entity.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); + entity.Update(client, model, guild, channel); return entity; } - internal override async Task UpdateAsync(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) + internal override void Update(DiscordRestClient client, Model model, RestGuild guild, IRestMessageChannel channel) { - await base.UpdateAsync(client, model, guild, channel).ConfigureAwait(false); + base.Update(client, model, guild, channel); Options = model.Options.IsSpecified ? model.Options.Value.Select(x => new RestSlashCommandDataOption(this, x)).ToImmutableArray()