From 24b7bb593aa3b8c33321bace31680c58f3a02ea5 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Wed, 9 Mar 2022 17:28:46 -0400 Subject: [PATCH 01/25] Fix: sharded client logout (#2179) --- src/Discord.Net.WebSocket/DiscordShardedClient.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index 8374f2877..a361889c0 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -178,7 +178,6 @@ namespace Discord.WebSocket await _shards[i].LogoutAsync(); } - CurrentUser = null; if (_automaticShards) { _shardIds = new int[0]; From 765c0c554475efb332a6c28d935dbd9951158e73 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Wed, 9 Mar 2022 17:28:56 -0400 Subject: [PATCH 02/25] Feature: attachment description and content type (#2180) --- .../Entities/Messages/FileAttachment.cs | 16 ++++++++++++++++ .../Entities/Messages/IAttachment.cs | 8 ++++++++ .../Entities/Messages/Attachment.cs | 12 ++++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Messages/FileAttachment.cs b/src/Discord.Net.Core/Entities/Messages/FileAttachment.cs index 35252693b..7470a72cd 100644 --- a/src/Discord.Net.Core/Entities/Messages/FileAttachment.cs +++ b/src/Discord.Net.Core/Entities/Messages/FileAttachment.cs @@ -7,13 +7,29 @@ using System.Threading.Tasks; namespace Discord { + /// + /// Represents an outgoing file attachment used to send a file to discord. + /// public struct FileAttachment : IDisposable { + /// + /// Gets or sets the filename. + /// public string FileName { get; set; } + /// + /// Gets or sets the description of the file. + /// public string Description { get; set; } + + /// + /// Gets or sets whether this file should be marked as a spoiler. + /// public bool IsSpoiler { get; set; } #pragma warning disable IDISP008 + /// + /// Gets the stream containing the file content. + /// public Stream Stream { get; } #pragma warning restore IDISP008 diff --git a/src/Discord.Net.Core/Entities/Messages/IAttachment.cs b/src/Discord.Net.Core/Entities/Messages/IAttachment.cs index e94e9f97c..277c06291 100644 --- a/src/Discord.Net.Core/Entities/Messages/IAttachment.cs +++ b/src/Discord.Net.Core/Entities/Messages/IAttachment.cs @@ -62,5 +62,13 @@ namespace Discord /// if the attachment is ephemeral; otherwise . /// bool Ephemeral { get; } + /// + /// Gets the description of the attachment; or if there is none set. + /// + string Description { get; } + /// + /// Gets the media's MIME type if present; otherwise . + /// + string ContentType { get; } } } diff --git a/src/Discord.Net.Rest/Entities/Messages/Attachment.cs b/src/Discord.Net.Rest/Entities/Messages/Attachment.cs index 4e4849c51..a5b83fb7b 100644 --- a/src/Discord.Net.Rest/Entities/Messages/Attachment.cs +++ b/src/Discord.Net.Rest/Entities/Messages/Attachment.cs @@ -23,8 +23,13 @@ namespace Discord public int? Width { get; } /// public bool Ephemeral { get; } + /// + public string Description { get; } + /// + public string ContentType { get; } - internal Attachment(ulong id, string filename, string url, string proxyUrl, int size, int? height, int? width, bool? ephemeral) + internal Attachment(ulong id, string filename, string url, string proxyUrl, int size, int? height, int? width, + bool? ephemeral, string description, string contentType) { Id = id; Filename = filename; @@ -34,13 +39,16 @@ namespace Discord Height = height; Width = width; Ephemeral = ephemeral.GetValueOrDefault(false); + Description = description; + ContentType = contentType; } internal static Attachment Create(Model model) { return new Attachment(model.Id, model.Filename, model.Url, model.ProxyUrl, model.Size, model.Height.IsSpecified ? model.Height.Value : (int?)null, model.Width.IsSpecified ? model.Width.Value : (int?)null, - model.Ephemeral.ToNullable()); + model.Ephemeral.ToNullable(), model.Description.GetValueOrDefault(), + model.ContentType.GetValueOrDefault()); } /// From f8ec3c79c2559288d743f5cd2fcb077cdb76306e Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Wed, 9 Mar 2022 17:29:10 -0400 Subject: [PATCH 03/25] Fix/ambigiuous reference (#2181) * fix: Ambigiuous reference when creating roles * Update RestGuild.cs --- src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs | 5 ----- src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs | 4 ---- 2 files changed, 9 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index 2c37bb2da..e89096f00 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -763,11 +763,6 @@ namespace Discord.Rest return null; } - /// - public Task CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), - bool isHoisted = false, RequestOptions options = null) - => CreateRoleAsync(name, permissions, color, isHoisted, false, options); - /// /// Creates a new role with the provided name. /// diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index bd5d811f1..c4b756410 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -999,10 +999,6 @@ namespace Discord.WebSocket return null; } - /// - public Task CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), - bool isHoisted = false, RequestOptions options = null) - => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, false, options); /// /// Creates a new role with the provided name. /// From 25aaa4948ad103556c09aeed813939d2f4ddd1a6 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Wed, 9 Mar 2022 17:29:24 -0400 Subject: [PATCH 04/25] fix: thread owner always null (#2182) --- .../Entities/Channels/SocketThreadChannel.cs | 29 +++++++++++++++++-- .../Entities/Users/SocketThreadUser.cs | 11 +++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs index c26a23afd..2e77e62e3 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs @@ -24,7 +24,29 @@ namespace Discord.WebSocket /// /// Gets the owner of the current thread. /// - public SocketThreadUser Owner { get; private set; } + public SocketThreadUser Owner + { + get + { + lock (_ownerLock) + { + var user = GetUser(_ownerId); + + if (user == null) + { + var guildMember = Guild.GetUser(_ownerId); + if (guildMember == null) + return null; + + user = SocketThreadUser.Create(Guild, this, guildMember); + _members[user.Id] = user; + return user; + } + else + return user; + } + } + } /// /// Gets the current users within this thread. @@ -83,6 +105,9 @@ namespace Discord.WebSocket private bool _usersDownloaded; private readonly object _downloadLock = new object(); + private readonly object _ownerLock = new object(); + + private ulong _ownerId; internal SocketThreadChannel(DiscordSocketClient discord, SocketGuild guild, ulong id, SocketGuildChannel parent, DateTimeOffset? createdAt) @@ -120,7 +145,7 @@ namespace Discord.WebSocket if (model.OwnerId.IsSpecified) { - Owner = GetUser(model.OwnerId.Value); + _ownerId = model.OwnerId.Value; } HasJoined = model.ThreadMember.IsSpecified; diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs index 025d34d0f..6eddd876d 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketThreadUser.cs @@ -147,6 +147,17 @@ namespace Discord.WebSocket return entity; } + internal static SocketThreadUser Create(SocketGuild guild, SocketThreadChannel thread, SocketGuildUser owner) + { + // this is used for creating the owner of the thread. + var entity = new SocketThreadUser(guild, thread, owner, owner.Id); + entity.Update(new Model + { + JoinTimestamp = thread.CreatedAt, + }); + return entity; + } + internal void Update(Model model) { ThreadJoinedAt = model.JoinTimestamp; From e3fc96bc44fabf1809a8a0f2b5a68e79f69c8602 Mon Sep 17 00:00:00 2001 From: Quin Lynch Date: Wed, 9 Mar 2022 17:33:25 -0400 Subject: [PATCH 05/25] meta: 3.4.1 --- CHANGELOG.md | 14 +++++++ Discord.Net.targets | 2 +- docs/docfx.json | 2 +- src/Discord.Net/Discord.Net.nuspec | 62 +++++++++++++++--------------- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 416f2ec6e..a96a77e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## [3.4.1] - 2022-03-9 + +### Added +- #2169 Component TypeConverters and CustomID TypeReaders (fb4250b) +- #2180 Attachment description and content type (765c0c5) +- #2162 Add configuration toggle to suppress Unknown dispatch warnings (1ba96d6) +- #2178 Add 10065 Error code (cc6918d) + +### Fixed +- #2179 Logging out sharded client throws (24b7bb5) +- #2182 Thread owner always returns null (25aaa49) +- #2165 Fix error with flag params when uploading files. (a5d3add) +- #2181 Fix ambiguous reference for creating roles (f8ec3c7) + ## [3.4.0] - 2022-3-2 ## Added diff --git a/Discord.Net.targets b/Discord.Net.targets index d0e17b3c5..187ff9d75 100644 --- a/Discord.Net.targets +++ b/Discord.Net.targets @@ -1,6 +1,6 @@ - 3.4.0 + 3.4.1 latest Discord.Net Contributors discord;discordapp diff --git a/docs/docfx.json b/docs/docfx.json index 2ad0164f4..3b7ef582b 100644 --- a/docs/docfx.json +++ b/docs/docfx.json @@ -60,7 +60,7 @@ "overwrite": "_overwrites/**/**.md", "globalMetadata": { "_appTitle": "Discord.Net Documentation", - "_appFooter": "Discord.Net (c) 2015-2022 3.4.0", + "_appFooter": "Discord.Net (c) 2015-2022 3.4.1", "_enableSearch": true, "_appLogoPath": "marketing/logo/SVG/Logomark Purple.svg", "_appFaviconPath": "favicon.ico" diff --git a/src/Discord.Net/Discord.Net.nuspec b/src/Discord.Net/Discord.Net.nuspec index d98287ffa..996e9bae9 100644 --- a/src/Discord.Net/Discord.Net.nuspec +++ b/src/Discord.Net/Discord.Net.nuspec @@ -2,7 +2,7 @@ Discord.Net - 3.4.0$suffix$ + 3.4.1$suffix$ Discord.Net Discord.Net Contributors foxbot @@ -14,44 +14,44 @@ https://github.com/RogueException/Discord.Net/raw/dev/docs/marketing/logo/PackageLogo.png - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + - - - - - - + + + + + + From fc31589056601a16a61f4404f67721ee05330187 Mon Sep 17 00:00:00 2001 From: Quin Lynch Date: Wed, 9 Mar 2022 17:36:29 -0400 Subject: [PATCH 06/25] Fix changelog formatting --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a96a77e17..6884d3564 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,7 @@ ## [3.4.0] - 2022-3-2 -## Added +### Added - #2146 Add FromDateTimeOffset in TimestampTag (553055b) - #2062 Add return statement to precondition handling (3e52fab) - #2131 Add support for sending Message Flags (1fb62de) @@ -27,13 +27,13 @@ - #2155 Add Interaction Service Complex Parameters (9ba64f6) - #2156 Add Display name support for enum type converter (c800674) -## Fixed +### Fixed - #2117 Fix stream access exception when ratelimited (a1cfa41) - #2128 Fix context menu comand message type (f601e9b) - #2135 Fix NRE when ratelimmited requests don't return a body (b95b942) - #2154 Fix usage of CacheMode.AllowDownload in channels (b3370c3) -## Misc +### Misc - #2149 Clarify Users property on SocketGuildChannel (5594739) - #2157 Enforce valid button styles (507a18d) From c286b9978ed13056651a7202a506d4fdeea218d3 Mon Sep 17 00:00:00 2001 From: Raiden Shogun <96011415+Almighty-Shogun@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:33:21 +0100 Subject: [PATCH 07/25] Fixed typo (#2206) `await arg2.Interaction.RespondAsync("Command exception: {arg3.ErrorReason}");` would never have showed the `ErrorReason` because a `$` was missing before the string. --- docs/guides/int_framework/samples/postexecution/error_review.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/int_framework/samples/postexecution/error_review.cs b/docs/guides/int_framework/samples/postexecution/error_review.cs index dd397b2c9..d5f8a9cb1 100644 --- a/docs/guides/int_framework/samples/postexecution/error_review.cs +++ b/docs/guides/int_framework/samples/postexecution/error_review.cs @@ -16,7 +16,7 @@ async Task SlashCommandExecuted(SlashCommandInfo arg1, Discord.IInteractionConte await arg2.Interaction.RespondAsync("Invalid number or arguments"); break; case InteractionCommandError.Exception: - await arg2.Interaction.RespondAsync("Command exception:{arg3.ErrorReason}"); + await arg2.Interaction.RespondAsync($"Command exception: {arg3.ErrorReason}"); break; case InteractionCommandError.Unsuccessful: await arg2.Interaction.RespondAsync("Command could not be executed"); From 47de5a2fb450a846a805f5185e8bf7bf46c533ca Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:37:30 +0100 Subject: [PATCH 08/25] Greatly reduce code complexity & make IF samples functional (#2205) * Greatly reduce code complexity * Fixes sharded client IF implementation --- .../InteractionFramework/CommandHandler.cs | 152 ------------------ .../{ => Enums}/ExampleEnum.cs | 0 .../InteractionHandler.cs | 81 ++++++++++ .../Modules/ComponentModule.cs | 18 --- .../{GeneralModule.cs => ExampleModule.cs} | 84 +++++----- .../Modules/MessageCommandModule.cs | 30 ---- .../Modules/SlashCommandModule.cs | 51 ------ .../Modules/UserCommandModule.cs | 17 -- samples/InteractionFramework/Program.cs | 75 ++++----- .../Modules/InteractionModule.cs | 2 +- samples/ShardedClient/Program.cs | 7 +- .../Services/InteractionHandlingService.cs | 4 +- 12 files changed, 170 insertions(+), 351 deletions(-) delete mode 100644 samples/InteractionFramework/CommandHandler.cs rename samples/InteractionFramework/{ => Enums}/ExampleEnum.cs (100%) create mode 100644 samples/InteractionFramework/InteractionHandler.cs delete mode 100644 samples/InteractionFramework/Modules/ComponentModule.cs rename samples/InteractionFramework/Modules/{GeneralModule.cs => ExampleModule.cs} (54%) delete mode 100644 samples/InteractionFramework/Modules/MessageCommandModule.cs delete mode 100644 samples/InteractionFramework/Modules/SlashCommandModule.cs delete mode 100644 samples/InteractionFramework/Modules/UserCommandModule.cs diff --git a/samples/InteractionFramework/CommandHandler.cs b/samples/InteractionFramework/CommandHandler.cs deleted file mode 100644 index 9a505246f..000000000 --- a/samples/InteractionFramework/CommandHandler.cs +++ /dev/null @@ -1,152 +0,0 @@ -using Discord; -using Discord.Interactions; -using Discord.WebSocket; -using System; -using System.Reflection; -using System.Threading.Tasks; - -namespace InteractionFramework -{ - public class CommandHandler - { - private readonly DiscordSocketClient _client; - private readonly InteractionService _commands; - private readonly IServiceProvider _services; - - public CommandHandler(DiscordSocketClient client, InteractionService commands, IServiceProvider services) - { - _client = client; - _commands = commands; - _services = services; - } - - public async Task InitializeAsync ( ) - { - // Add the public modules that inherit InteractionModuleBase to the InteractionService - await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); - // Another approach to get the assembly of a specific type is: - // typeof(CommandHandler).Assembly - - - // Process the InteractionCreated payloads to execute Interactions commands - _client.InteractionCreated += HandleInteraction; - - // Process the command execution results - _commands.SlashCommandExecuted += SlashCommandExecuted; - _commands.ContextCommandExecuted += ContextCommandExecuted; - _commands.ComponentCommandExecuted += ComponentCommandExecuted; - } - - # region Error Handling - - private Task ComponentCommandExecuted (ComponentCommandInfo arg1, Discord.IInteractionContext arg2, IResult arg3) - { - if (!arg3.IsSuccess) - { - switch (arg3.Error) - { - case InteractionCommandError.UnmetPrecondition: - // implement - break; - case InteractionCommandError.UnknownCommand: - // implement - break; - case InteractionCommandError.BadArgs: - // implement - break; - case InteractionCommandError.Exception: - // implement - break; - case InteractionCommandError.Unsuccessful: - // implement - break; - default: - break; - } - } - - return Task.CompletedTask; - } - - private Task ContextCommandExecuted (ContextCommandInfo arg1, Discord.IInteractionContext arg2, IResult arg3) - { - if (!arg3.IsSuccess) - { - switch (arg3.Error) - { - case InteractionCommandError.UnmetPrecondition: - // implement - break; - case InteractionCommandError.UnknownCommand: - // implement - break; - case InteractionCommandError.BadArgs: - // implement - break; - case InteractionCommandError.Exception: - // implement - break; - case InteractionCommandError.Unsuccessful: - // implement - break; - default: - break; - } - } - - return Task.CompletedTask; - } - - private Task SlashCommandExecuted (SlashCommandInfo arg1, Discord.IInteractionContext arg2, IResult arg3) - { - if (!arg3.IsSuccess) - { - switch (arg3.Error) - { - case InteractionCommandError.UnmetPrecondition: - // implement - break; - case InteractionCommandError.UnknownCommand: - // implement - break; - case InteractionCommandError.BadArgs: - // implement - break; - case InteractionCommandError.Exception: - // implement - break; - case InteractionCommandError.Unsuccessful: - // implement - break; - default: - break; - } - } - - return Task.CompletedTask; - } - # endregion - - # region Execution - - private async Task HandleInteraction (SocketInteraction arg) - { - try - { - // Create an execution context that matches the generic type parameter of your InteractionModuleBase modules - var ctx = new SocketInteractionContext(_client, arg); - await _commands.ExecuteCommandAsync(ctx, _services); - } - catch (Exception ex) - { - Console.WriteLine(ex); - - // If a Slash Command execution fails it is most likely that the original interaction acknowledgement will persist. It is a good idea to delete the original - // response, or at least let the user know that something went wrong during the command execution. - if(arg.Type == InteractionType.ApplicationCommand) - await arg.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync()); - } - } - # endregion - } -} diff --git a/samples/InteractionFramework/ExampleEnum.cs b/samples/InteractionFramework/Enums/ExampleEnum.cs similarity index 100% rename from samples/InteractionFramework/ExampleEnum.cs rename to samples/InteractionFramework/Enums/ExampleEnum.cs diff --git a/samples/InteractionFramework/InteractionHandler.cs b/samples/InteractionFramework/InteractionHandler.cs new file mode 100644 index 000000000..bc6f47285 --- /dev/null +++ b/samples/InteractionFramework/InteractionHandler.cs @@ -0,0 +1,81 @@ +using Discord; +using Discord.Interactions; +using Discord.WebSocket; +using Microsoft.Extensions.Configuration; +using System; +using System.Reflection; +using System.Threading.Tasks; + +namespace InteractionFramework +{ + public class InteractionHandler + { + private readonly DiscordSocketClient _client; + private readonly InteractionService _handler; + private readonly IServiceProvider _services; + private readonly IConfiguration _configuration; + + public InteractionHandler(DiscordSocketClient client, InteractionService handler, IServiceProvider services, IConfiguration config) + { + _client = client; + _handler = handler; + _services = services; + _configuration = config; + } + + public async Task InitializeAsync() + { + // Process when the client is ready, so we can register our commands. + _client.Ready += ReadyAsync; + _handler.Log += LogAsync; + + // Add the public modules that inherit InteractionModuleBase to the InteractionService + await _handler.AddModulesAsync(Assembly.GetEntryAssembly(), _services); + + // Process the InteractionCreated payloads to execute Interactions commands + _client.InteractionCreated += HandleInteraction; + } + + private async Task LogAsync(LogMessage log) + => Console.WriteLine(log); + + private async Task ReadyAsync() + { + // Context & Slash commands can be automatically registered, but this process needs to happen after the client enters the READY state. + // Since Global Commands take around 1 hour to register, we should use a test guild to instantly update and test our commands. + if (Program.IsDebug()) + await _handler.RegisterCommandsToGuildAsync(_configuration.GetValue("testGuild"), true); + else + await _handler.RegisterCommandsGloballyAsync(true); + } + + private async Task HandleInteraction(SocketInteraction interaction) + { + try + { + // Create an execution context that matches the generic type parameter of your InteractionModuleBase modules. + var context = new SocketInteractionContext(_client, interaction); + + // Execute the incoming command. + var result = await _handler.ExecuteCommandAsync(context, _services); + + if (!result.IsSuccess) + switch (result.Error) + { + case InteractionCommandError.UnmetPrecondition: + // implement + break; + default: + break; + } + } + catch + { + // If Slash Command execution fails it is most likely that the original interaction acknowledgement will persist. It is a good idea to delete the original + // response, or at least let the user know that something went wrong during the command execution. + if (interaction.Type is InteractionType.ApplicationCommand) + await interaction.GetOriginalResponseAsync().ContinueWith(async (msg) => await msg.Result.DeleteAsync()); + } + } + } +} diff --git a/samples/InteractionFramework/Modules/ComponentModule.cs b/samples/InteractionFramework/Modules/ComponentModule.cs deleted file mode 100644 index 643004ded..000000000 --- a/samples/InteractionFramework/Modules/ComponentModule.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Discord.Interactions; -using Discord.WebSocket; -using InteractionFramework.Attributes; -using System.Threading.Tasks; - -namespace InteractionFramework -{ - // As with all other modules, we create the context by defining what type of interaction this module is supposed to target. - internal class ComponentModule : InteractionModuleBase> - { - // With the Attribute DoUserCheck you can make sure that only the user this button targets can click it. This is defined by the first wildcard: *. - // See Attributes/DoUserCheckAttribute.cs for elaboration. - [DoUserCheck] - [ComponentInteraction("myButton:*")] - public async Task ClickButtonAsync(string userId) - => await RespondAsync(text: ":thumbsup: Clicked!"); - } -} diff --git a/samples/InteractionFramework/Modules/GeneralModule.cs b/samples/InteractionFramework/Modules/ExampleModule.cs similarity index 54% rename from samples/InteractionFramework/Modules/GeneralModule.cs rename to samples/InteractionFramework/Modules/ExampleModule.cs index 78740a960..1c0a6c8a2 100644 --- a/samples/InteractionFramework/Modules/GeneralModule.cs +++ b/samples/InteractionFramework/Modules/ExampleModule.cs @@ -1,32 +1,25 @@ using Discord; using Discord.Interactions; +using InteractionFramework.Attributes; +using System; using System.Threading.Tasks; namespace InteractionFramework.Modules { // Interation modules must be public and inherit from an IInterationModuleBase - public class GeneralModule : InteractionModuleBase + public class ExampleModule : InteractionModuleBase { // Dependencies can be accessed through Property injection, public properties with public setters will be set by the service provider public InteractionService Commands { get; set; } - private CommandHandler _handler; + private InteractionHandler _handler; // Constructor injection is also a valid way to access the dependecies - public GeneralModule(CommandHandler handler) + public ExampleModule(InteractionHandler handler) { _handler = handler; } - // Slash Commands are declared using the [SlashCommand], you need to provide a name and a description, both following the Discord guidelines - [SlashCommand("ping", "Recieve a pong")] - // By setting the DefaultPermission to false, you can disable the command by default. No one can use the command until you give them permission - [DefaultPermission(false)] - public async Task Ping ( ) - { - await RespondAsync("pong"); - } - // You can use a number of parameter types in you Slash Command handlers (string, int, double, bool, IUser, IChannel, IMentionable, IRole, Enums) by default. Optionally, // you can implement your own TypeConverters to support a wider range of parameter types. For more information, refer to the library documentation. // Optional method parameters(parameters with a default value) also will be displayed as optional on Discord. @@ -34,9 +27,15 @@ namespace InteractionFramework.Modules // [Summary] lets you customize the name and the description of a parameter [SlashCommand("echo", "Repeat the input")] public async Task Echo(string echo, [Summary(description: "mention the user")]bool mention = false) - { - await RespondAsync(echo + (mention ? Context.User.Mention : string.Empty)); - } + => await RespondAsync(echo + (mention ? Context.User.Mention : string.Empty)); + + [SlashCommand("ping", "Pings the bot and returns its latency.")] + public async Task GreetUserAsync() + => await RespondAsync(text: $":ping_pong: It took me {Context.Client.Latency}ms to respond to you!", ephemeral: true); + + [SlashCommand("bitrate", "Gets the bitrate of a specific voice channel.")] + public async Task GetBitrateAsync([ChannelTypes(ChannelType.Voice, ChannelType.Stage)] IChannel channel) + => await RespondAsync(text: $"This voice channel has a bitrate of {(channel as IVoiceChannel).Bitrate}"); // [Group] will create a command group. [SlashCommand]s and [ComponentInteraction]s will be registered with the group prefix [Group("test_group", "This is a command group")] @@ -46,25 +45,7 @@ namespace InteractionFramework.Modules // choice option [SlashCommand("choice_example", "Enums create choices")] public async Task ChoiceExample(ExampleEnum input) - { - await RespondAsync(input.ToString()); - } - } - - // User Commands can only have one parameter, which must be a type of SocketUser - [UserCommand("SayHello")] - public async Task SayHello(IUser user) - { - await RespondAsync($"Hello, {user.Mention}"); - } - - // Message Commands can only have one parameter, which must be a type of SocketMessage - [MessageCommand("Delete")] - [Attributes.RequireOwner] - public async Task DeleteMesage(IMessage message) - { - await message.DeleteAsync(); - await RespondAsync("Deleted message."); + => await RespondAsync(input.ToString()); } // Use [ComponentInteraction] to handle message component interactions. Message component interaction with the matching customId will be executed. @@ -80,9 +61,40 @@ namespace InteractionFramework.Modules // Select Menu interactions, contain ids of the menu options that were selected by the user. You can access the option ids from the method parameters. // You can also use the wild card pattern with Select Menus, in that case, the wild card captures will be passed on to the method first, followed by the option ids. [ComponentInteraction("roleSelect")] - public async Task RoleSelect(params string[] selections) + public async Task RoleSelect(string[] selections) + { + throw new NotImplementedException(); + } + + // With the Attribute DoUserCheck you can make sure that only the user this button targets can click it. This is defined by the first wildcard: *. + // See Attributes/DoUserCheckAttribute.cs for elaboration. + [DoUserCheck] + [ComponentInteraction("myButton:*")] + public async Task ClickButtonAsync(string userId) + => await RespondAsync(text: ":thumbsup: Clicked!"); + + // This command will greet target user in the channel this was executed in. + [UserCommand("greet")] + public async Task GreetUserAsync(IUser user) + => await RespondAsync(text: $":wave: {Context.User} said hi to you, <@{user.Id}>!"); + + // Pins a message in the channel it is in. + [MessageCommand("pin")] + public async Task PinMessageAsync(IMessage message) { - // implement + // make a safety cast to check if the message is ISystem- or IUserMessage + if (message is not IUserMessage userMessage) + await RespondAsync(text: ":x: You cant pin system messages!"); + + // if the pins in this channel are equal to or above 50, no more messages can be pinned. + else if ((await Context.Channel.GetPinnedMessagesAsync()).Count >= 50) + await RespondAsync(text: ":x: You cant pin any more messages, the max has already been reached in this channel!"); + + else + { + await userMessage.PinAsync(); + await RespondAsync(":white_check_mark: Successfully pinned message!"); + } } } } diff --git a/samples/InteractionFramework/Modules/MessageCommandModule.cs b/samples/InteractionFramework/Modules/MessageCommandModule.cs deleted file mode 100644 index d07d276f5..000000000 --- a/samples/InteractionFramework/Modules/MessageCommandModule.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Discord; -using Discord.Interactions; -using Discord.WebSocket; -using System.Threading.Tasks; - -namespace InteractionFramework.Modules -{ - // A transient module for executing commands. This module will NOT keep any information after the command is executed. - internal class MessageCommandModule : InteractionModuleBase> - { - // Pins a message in the channel it is in. - [MessageCommand("pin")] - public async Task PinMessageAsync(IMessage message) - { - // make a safety cast to check if the message is ISystem- or IUserMessage - if (message is not IUserMessage userMessage) - await RespondAsync(text: ":x: You cant pin system messages!"); - - // if the pins in this channel are equal to or above 50, no more messages can be pinned. - else if ((await Context.Channel.GetPinnedMessagesAsync()).Count >= 50) - await RespondAsync(text: ":x: You cant pin any more messages, the max has already been reached in this channel!"); - - else - { - await userMessage.PinAsync(); - await RespondAsync(":white_check_mark: Successfully pinned message!"); - } - } - } -} diff --git a/samples/InteractionFramework/Modules/SlashCommandModule.cs b/samples/InteractionFramework/Modules/SlashCommandModule.cs deleted file mode 100644 index a066ea18c..000000000 --- a/samples/InteractionFramework/Modules/SlashCommandModule.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Discord; -using Discord.Interactions; -using Discord.WebSocket; -using System; -using System.Threading.Tasks; - -namespace InteractionFramework.Modules -{ - public enum Hobby - { - Gaming, - - Art, - - Reading - } - - // A transient module for executing commands. This module will NOT keep any information after the command is executed. - class SlashCommandModule : InteractionModuleBase> - { - // Will be called before execution. Here you can populate several entities you may want to retrieve before executing a command. - // I.E. database objects - public override void BeforeExecute(ICommandInfo command) - { - // Anything - throw new NotImplementedException(); - } - - // Will be called after execution - public override void AfterExecute(ICommandInfo command) - { - // Anything - throw new NotImplementedException(); - } - - [SlashCommand("ping", "Pings the bot and returns its latency.")] - public async Task GreetUserAsync() - => await RespondAsync(text: $":ping_pong: It took me {Context.Client.Latency}ms to respond to you!", ephemeral: true); - - [SlashCommand("hobby", "Choose your hobby from the list!")] - public async Task ChooseAsync(Hobby hobby) - => await RespondAsync(text: $":thumbsup: Your hobby is: {hobby}."); - - [SlashCommand("bitrate", "Gets the bitrate of a specific voice channel.")] - public async Task GetBitrateAsync([ChannelTypes(ChannelType.Voice, ChannelType.Stage)] IChannel channel) - { - var voiceChannel = channel as IVoiceChannel; - await RespondAsync(text: $"This voice channel has a bitrate of {voiceChannel.Bitrate}"); - } - } -} diff --git a/samples/InteractionFramework/Modules/UserCommandModule.cs b/samples/InteractionFramework/Modules/UserCommandModule.cs deleted file mode 100644 index 60c5246ce..000000000 --- a/samples/InteractionFramework/Modules/UserCommandModule.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Discord; -using Discord.Interactions; -using Discord.WebSocket; -using System.Threading.Tasks; - -namespace InteractionFramework.Modules -{ - // A transient module for executing commands. This module will NOT keep any information after the command is executed. - class UserCommandModule : InteractionModuleBase> - { - // This command will greet target user in the channel this was executed in. - [UserCommand("greet")] - public async Task GreetUserAsync(IUser user) - => await RespondAsync(text: $":wave: {Context.User} said hi to you, <@{user.Id}>!"); - } -} - diff --git a/samples/InteractionFramework/Program.cs b/samples/InteractionFramework/Program.cs index 49db29714..b9c4697af 100644 --- a/samples/InteractionFramework/Program.cs +++ b/samples/InteractionFramework/Program.cs @@ -9,69 +9,60 @@ using System.Threading.Tasks; namespace InteractionFramework { - class Program + public class Program { - // Entry point of the program. - static void Main ( string[] args ) + private readonly IConfiguration _configuration; + private readonly IServiceProvider _services; + + private readonly DiscordSocketConfig _socketConfig = new() + { + GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers, + AlwaysDownloadUsers = true, + }; + + public Program() { - // One of the more flexable ways to access the configuration data is to use the Microsoft's Configuration model, - // this way we can avoid hard coding the environment secrets. I opted to use the Json and environment variable providers here. - IConfiguration config = new ConfigurationBuilder() + _configuration = new ConfigurationBuilder() .AddEnvironmentVariables(prefix: "DC_") .AddJsonFile("appsettings.json", optional: true) .Build(); - RunAsync(config).GetAwaiter().GetResult(); + _services = new ServiceCollection() + .AddSingleton(_configuration) + .AddSingleton(_socketConfig) + .AddSingleton() + .AddSingleton(x => new InteractionService(x.GetRequiredService())) + .AddSingleton() + .BuildServiceProvider(); } - static async Task RunAsync (IConfiguration configuration) - { - // Dependency injection is a key part of the Interactions framework but it needs to be disposed at the end of the app's lifetime. - using var services = ConfigureServices(configuration); + static void Main(string[] args) + => new Program().RunAsync() + .GetAwaiter() + .GetResult(); - var client = services.GetRequiredService(); - var commands = services.GetRequiredService(); + public async Task RunAsync() + { + var client = _services.GetRequiredService(); client.Log += LogAsync; - commands.Log += LogAsync; - - // Slash Commands and Context Commands are can be automatically registered, but this process needs to happen after the client enters the READY state. - // Since Global Commands take around 1 hour to register, we should use a test guild to instantly update and test our commands. To determine the method we should - // register the commands with, we can check whether we are in a DEBUG environment and if we are, we can register the commands to a predetermined test guild. - client.Ready += async ( ) => - { - if (IsDebug()) - // Id of the test guild can be provided from the Configuration object - await commands.RegisterCommandsToGuildAsync(configuration.GetValue("testGuild"), true); - else - await commands.RegisterCommandsGloballyAsync(true); - }; // Here we can initialize the service that will register and execute our commands - await services.GetRequiredService().InitializeAsync(); + await _services.GetRequiredService() + .InitializeAsync(); // Bot token can be provided from the Configuration object we set up earlier - await client.LoginAsync(TokenType.Bot, configuration["token"]); + await client.LoginAsync(TokenType.Bot, _configuration["token"]); await client.StartAsync(); + // Never quit the program until manually forced to. await Task.Delay(Timeout.Infinite); } - static Task LogAsync(LogMessage message) - { - Console.WriteLine(message.ToString()); - return Task.CompletedTask; - } - - static ServiceProvider ConfigureServices ( IConfiguration configuration ) - => new ServiceCollection() - .AddSingleton(configuration) - .AddSingleton() - .AddSingleton(x => new InteractionService(x.GetRequiredService())) - .AddSingleton() - .BuildServiceProvider(); + private async Task LogAsync(LogMessage message) + => Console.WriteLine(message.ToString()); - static bool IsDebug ( ) + public static bool IsDebug() { #if DEBUG return true; diff --git a/samples/ShardedClient/Modules/InteractionModule.cs b/samples/ShardedClient/Modules/InteractionModule.cs index 089328e7d..6c2f0e940 100644 --- a/samples/ShardedClient/Modules/InteractionModule.cs +++ b/samples/ShardedClient/Modules/InteractionModule.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; namespace ShardedClient.Modules { // A display of portability, which shows how minimal the difference between the 2 frameworks is. - public class InteractionModule : InteractionModuleBase> + public class InteractionModule : InteractionModuleBase { [SlashCommand("info", "Information about this shard.")] public async Task InfoAsync() diff --git a/samples/ShardedClient/Program.cs b/samples/ShardedClient/Program.cs index 717ce1d80..2b8f49edb 100644 --- a/samples/ShardedClient/Program.cs +++ b/samples/ShardedClient/Program.cs @@ -45,8 +45,11 @@ namespace ShardedClient client.ShardReady += ReadyAsync; client.Log += LogAsync; - await services.GetRequiredService().InitializeAsync(); - await services.GetRequiredService().InitializeAsync(); + await services.GetRequiredService() + .InitializeAsync(); + + await services.GetRequiredService() + .InitializeAsync(); // Tokens should be considered secret data, and never hard-coded. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); diff --git a/samples/ShardedClient/Services/InteractionHandlingService.cs b/samples/ShardedClient/Services/InteractionHandlingService.cs index 59b479361..3c41d7f33 100644 --- a/samples/ShardedClient/Services/InteractionHandlingService.cs +++ b/samples/ShardedClient/Services/InteractionHandlingService.cs @@ -31,9 +31,9 @@ namespace ShardedClient.Services { await _service.AddModulesAsync(typeof(InteractionHandlingService).Assembly, _provider); #if DEBUG - await _service.AddCommandsToGuildAsync(_client.Guilds.First(x => x.Id == 1)); + await _service.RegisterCommandsToGuildAsync(1 /* implement */); #else - await _service.AddCommandsGloballyAsync(); + await _service.RegisterCommandsGloballyAsync(); #endif } From d5342e458500d42b94f7b1471f96c40ebfc56b35 Mon Sep 17 00:00:00 2001 From: Robin Sue Date: Sat, 26 Mar 2022 13:42:07 +0100 Subject: [PATCH 09/25] Fix Serilog Level Mapping (#2202) --- docs/guides/other_libs/samples/ModifyLogMethod.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/guides/other_libs/samples/ModifyLogMethod.cs b/docs/guides/other_libs/samples/ModifyLogMethod.cs index 0f7c11daf..b4870cfd1 100644 --- a/docs/guides/other_libs/samples/ModifyLogMethod.cs +++ b/docs/guides/other_libs/samples/ModifyLogMethod.cs @@ -6,8 +6,8 @@ private static async Task LogAsync(LogMessage message) LogSeverity.Error => LogEventLevel.Error, LogSeverity.Warning => LogEventLevel.Warning, LogSeverity.Info => LogEventLevel.Information, - LogSeverity.Verbose => LogEventLevel.Verbose, - LogSeverity.Debug => LogEventLevel.Debug, + LogSeverity.Verbose => LogEventLevel.Debug, + LogSeverity.Debug => LogEventLevel.Verbose, _ => LogEventLevel.Information }; Log.Write(severity, message.Exception, "[{Source}] {Message}", message.Source, message.Message); From 82473bce69f323448dff8cda3c1ddda4bbf5a383 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:43:16 +0100 Subject: [PATCH 10/25] Update GuildMemberUpdated comment regarding presence (#2193) --- src/Discord.Net.WebSocket/BaseSocketClient.Events.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index 134f8136b..b8d3b6a10 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -451,7 +451,7 @@ namespace Discord.WebSocket remove { _userUpdatedEvent.Remove(value); } } internal readonly AsyncEvent> _userUpdatedEvent = new AsyncEvent>(); - /// Fired when a guild member is updated, or a member presence is updated. + /// Fired when a guild member is updated. public event Func, SocketGuildUser, Task> GuildMemberUpdated { add { _guildMemberUpdatedEvent.Add(value); } From 741ed809d64dd6c9a364f5ab6d8f82b5000677b5 Mon Sep 17 00:00:00 2001 From: d4n Date: Sat, 26 Mar 2022 07:44:13 -0500 Subject: [PATCH 11/25] Add missing methods to IComponentInteraction (#2201) --- .../IComponentInteraction.cs | 21 ++++++++++++++++++- .../MessageComponents/RestMessageComponent.cs | 8 +++++++ .../SocketMessageComponent.cs | 16 ++------------ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/IComponentInteraction.cs b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/IComponentInteraction.cs index 2a46e8f18..299ee795d 100644 --- a/src/Discord.Net.Core/Entities/Interactions/MessageComponents/IComponentInteraction.cs +++ b/src/Discord.Net.Core/Entities/Interactions/MessageComponents/IComponentInteraction.cs @@ -1,3 +1,6 @@ +using System; +using System.Threading.Tasks; + namespace Discord { /// @@ -6,7 +9,7 @@ namespace Discord public interface IComponentInteraction : IDiscordInteraction { /// - /// Gets the data received with this interaction, contains the button that was clicked. + /// Gets the data received with this component interaction. /// new IComponentInteractionData Data { get; } @@ -14,5 +17,21 @@ namespace Discord /// Gets the message that contained the trigger for this interaction. /// IUserMessage Message { get; } + + /// + /// Updates the message which this component resides in with the type + /// + /// A delegate containing the properties to modify the message with. + /// The options to be used when sending the request. + /// A task that represents the asynchronous operation of updating the message. + Task UpdateAsync(Action func, RequestOptions options = null); + + /// + /// Defers an interaction with the response type 5 (). + /// + /// to defer ephemerally, otherwise . + /// The options to be used when sending the request. + /// A task that represents the asynchronous operation of acknowledging the interaction. + Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null); } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs index 359b92249..002510eac 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/MessageComponents/RestMessageComponent.cs @@ -492,5 +492,13 @@ namespace Discord.Rest /// IUserMessage IComponentInteraction.Message => Message; + + /// + Task IComponentInteraction.UpdateAsync(Action func, RequestOptions options) + => Task.FromResult(Update(func, options)); + + /// + Task IComponentInteraction.DeferLoadingAsync(bool ephemeral, RequestOptions options) + => Task.FromResult(DeferLoading(ephemeral, options)); } } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs index b06979381..aeff465bd 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/MessageComponents/SocketMessageComponent.cs @@ -202,12 +202,7 @@ namespace Discord.WebSocket HasResponded = true; } - /// - /// Updates the message which this component resides in with the type - /// - /// A delegate containing the properties to modify the message with. - /// The request options for this request. - /// A task that represents the asynchronous operation of updating the message. + /// public async Task UpdateAsync(Action func, RequestOptions options = null) { var args = new MessageProperties(); @@ -383,14 +378,7 @@ namespace Discord.WebSocket return await InteractionHelper.SendFollowupAsync(Discord, args, Token, Channel, options).ConfigureAwait(false); } - /// - /// Defers an interaction and responds with type 5 () - /// - /// to send this message ephemerally, otherwise . - /// The request options for this request. - /// - /// A task that represents the asynchronous operation of acknowledging the interaction. - /// + /// public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null) { if (!InteractionHelper.CanSendResponse(this)) From d48a7bd3483bb9ba414feb70740633877e77334c Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Sat, 26 Mar 2022 13:45:54 +0100 Subject: [PATCH 12/25] Fix: serialization error on thread creation timestamp. (#2188) --- src/Discord.Net.Rest/API/Common/ThreadMetadata.cs | 2 +- .../Entities/Channels/SocketThreadChannel.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.Rest/API/Common/ThreadMetadata.cs b/src/Discord.Net.Rest/API/Common/ThreadMetadata.cs index 15854fab4..6735504c8 100644 --- a/src/Discord.Net.Rest/API/Common/ThreadMetadata.cs +++ b/src/Discord.Net.Rest/API/Common/ThreadMetadata.cs @@ -21,6 +21,6 @@ namespace Discord.API public Optional Invitable { get; set; } [JsonProperty("create_timestamp")] - public Optional CreatedAt { get; set; } + public Optional CreatedAt { get; set; } } } diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs index 2e77e62e3..78462b062 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs @@ -121,7 +121,7 @@ namespace Discord.WebSocket internal new static SocketThreadChannel Create(SocketGuild guild, ClientState state, Model model) { var parent = guild.GetChannel(model.CategoryId.Value); - var entity = new SocketThreadChannel(guild.Discord, guild, model.Id, parent, model.ThreadMetadata.GetValueOrDefault()?.CreatedAt.ToNullable()); + var entity = new SocketThreadChannel(guild.Discord, guild, model.Id, parent, model.ThreadMetadata.GetValueOrDefault()?.CreatedAt.GetValueOrDefault(null)); entity.Update(state, model); return entity; } From d656722bd9477c410832e2d8c22b6baf1fb2f960 Mon Sep 17 00:00:00 2001 From: KeylAmi Date: Sat, 26 Mar 2022 08:46:33 -0400 Subject: [PATCH 13/25] Fix: modal response failing (#2187) * Update bugreport.yml * Update bugreport.yml removed d.net reference. fixed spelling. * Update bugreport.yml Adjusted verbiage for clarity * Fix for modal response failing Credit to @Cenggo for finding issue. --- src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs index a55a1307a..4866bd1da 100644 --- a/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/ModalCommandInfo.cs @@ -49,7 +49,7 @@ namespace Discord.Interactions try { var args = new object[Parameters.Count]; - var captureCount = additionalArgs.Length; + var captureCount = additionalArgs?.Length ?? 0; for(var i = 0; i < Parameters.Count; i++) { From 305d7f9e137b86e50412204e7dd4aa2dfe733094 Mon Sep 17 00:00:00 2001 From: FeroxFoxxo Date: Sun, 27 Mar 2022 01:52:31 +1300 Subject: [PATCH 14/25] Fix: Integration model from GuildIntegration and added INTEGRATION gateway events (#2168) * fix integration models; add integration events * fix description on IGUILD for integration * fix typo in integration documentation * fix documentation in connection visibility * removed public identitiers from app and connection * Removed REST endpoints that are not part of the API. * Added documentation for rest integrations * added optional types * Fixed rest interaction field with not being IsSpecified --- .../Guilds/GuildIntegrationProperties.cs | 21 ---- .../Entities/Guilds/IGuild.cs | 21 +++- .../Entities/Guilds/IntegrationAccount.cs | 18 --- .../IIntegration.cs} | 47 ++++++-- .../Integrations/IIntegrationAccount.cs | 23 ++++ .../Integrations/IIntegrationApplication.cs | 33 ++++++ .../Integrations/IntegrationExpireBehavior.cs | 17 +++ .../Entities/Users/ConnectionVisibility.cs | 17 +++ .../Entities/Users/IConnection.cs | 59 +++++++--- src/Discord.Net.Rest/API/Common/Connection.cs | 18 ++- .../API/Common/Integration.cs | 25 +++-- .../API/Common/IntegrationAccount.cs | 2 +- .../API/Common/IntegrationApplication.cs | 20 ++++ src/Discord.Net.Rest/ClientHelper.cs | 2 +- src/Discord.Net.Rest/DiscordRestApiClient.cs | 39 +------ .../Entities/Guilds/GuildHelper.cs | 16 +-- .../Entities/Guilds/RestGuild.cs | 12 +- .../Entities/Guilds/RestGuildIntegration.cs | 104 ------------------ .../Entities/Integrations/RestIntegration.cs | 102 +++++++++++++++++ .../Integrations/RestIntegrationAccount.cs | 29 +++++ .../RestIntegrationApplication.cs | 39 +++++++ .../Entities/Users/RestConnection.cs | 53 ++++++--- .../API/Gateway/IntegrationDeletedEvent.cs | 14 +++ .../BaseSocketClient.Events.cs | 26 +++++ .../DiscordSocketClient.cs | 86 +++++++++++++++ .../Entities/Guilds/SocketGuild.cs | 12 +- 26 files changed, 596 insertions(+), 259 deletions(-) delete mode 100644 src/Discord.Net.Core/Entities/Guilds/GuildIntegrationProperties.cs delete mode 100644 src/Discord.Net.Core/Entities/Guilds/IntegrationAccount.cs rename src/Discord.Net.Core/Entities/{Guilds/IGuildIntegration.cs => Integrations/IIntegration.cs} (61%) create mode 100644 src/Discord.Net.Core/Entities/Integrations/IIntegrationAccount.cs create mode 100644 src/Discord.Net.Core/Entities/Integrations/IIntegrationApplication.cs create mode 100644 src/Discord.Net.Core/Entities/Integrations/IntegrationExpireBehavior.cs create mode 100644 src/Discord.Net.Core/Entities/Users/ConnectionVisibility.cs create mode 100644 src/Discord.Net.Rest/API/Common/IntegrationApplication.cs delete mode 100644 src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs create mode 100644 src/Discord.Net.Rest/Entities/Integrations/RestIntegration.cs create mode 100644 src/Discord.Net.Rest/Entities/Integrations/RestIntegrationAccount.cs create mode 100644 src/Discord.Net.Rest/Entities/Integrations/RestIntegrationApplication.cs create mode 100644 src/Discord.Net.WebSocket/API/Gateway/IntegrationDeletedEvent.cs diff --git a/src/Discord.Net.Core/Entities/Guilds/GuildIntegrationProperties.cs b/src/Discord.Net.Core/Entities/Guilds/GuildIntegrationProperties.cs deleted file mode 100644 index 2ca19b50a..000000000 --- a/src/Discord.Net.Core/Entities/Guilds/GuildIntegrationProperties.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace Discord -{ - /// - /// Provides properties used to modify an with the specified changes. - /// - public class GuildIntegrationProperties - { - /// - /// Gets or sets the behavior when an integration subscription lapses. - /// - public Optional ExpireBehavior { get; set; } - /// - /// Gets or sets the period (in seconds) where the integration will ignore lapsed subscriptions. - /// - public Optional ExpireGracePeriod { get; set; } - /// - /// Gets or sets whether emoticons should be synced for this integration. - /// - public Optional EnableEmoticons { get; set; } - } -} diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index 3111ff495..b4625abbf 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -718,8 +718,25 @@ namespace Discord /// Task> GetVoiceRegionsAsync(RequestOptions options = null); - Task> GetIntegrationsAsync(RequestOptions options = null); - Task CreateIntegrationAsync(ulong id, string type, RequestOptions options = null); + /// + /// Gets a collection of all the integrations this guild contains. + /// + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous get operation. The task result contains a read-only collection of + /// integrations the guild can has. + /// + Task> GetIntegrationsAsync(RequestOptions options = null); + + /// + /// Deletes an integration. + /// + /// The id for the integration. + /// The options to be used when sending the request. + /// + /// A task that represents the asynchronous removal operation. + /// + Task DeleteIntegrationAsync(ulong id, RequestOptions options = null); /// /// Gets a collection of all invites in this guild. diff --git a/src/Discord.Net.Core/Entities/Guilds/IntegrationAccount.cs b/src/Discord.Net.Core/Entities/Guilds/IntegrationAccount.cs deleted file mode 100644 index 340115fde..000000000 --- a/src/Discord.Net.Core/Entities/Guilds/IntegrationAccount.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Diagnostics; - -namespace Discord -{ - [DebuggerDisplay("{DebuggerDisplay,nq}")] - public struct IntegrationAccount - { - /// Gets the ID of the account. - /// A unique identifier of this integration account. - public string Id { get; } - /// Gets the name of the account. - /// A string containing the name of this integration account. - public string Name { get; private set; } - - public override string ToString() => Name; - private string DebuggerDisplay => $"{Name} ({Id})"; - } -} diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuildIntegration.cs b/src/Discord.Net.Core/Entities/Integrations/IIntegration.cs similarity index 61% rename from src/Discord.Net.Core/Entities/Guilds/IGuildIntegration.cs rename to src/Discord.Net.Core/Entities/Integrations/IIntegration.cs index 6fe3f7b55..304d58792 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuildIntegration.cs +++ b/src/Discord.Net.Core/Entities/Integrations/IIntegration.cs @@ -3,15 +3,16 @@ using System; namespace Discord { /// - /// Holds information for a guild integration feature. + /// Holds information for an integration feature. + /// Nullable fields not provided for Discord bot integrations, but are for Twitch etc. /// - public interface IGuildIntegration + public interface IIntegration { /// /// Gets the integration ID. /// /// - /// An representing the unique identifier value of this integration. + /// A representing the unique identifier value of this integration. /// ulong Id { get; } /// @@ -45,30 +46,52 @@ namespace Discord /// /// true if this integration is syncing; otherwise false. /// - bool IsSyncing { get; } + bool? IsSyncing { get; } /// /// Gets the ID that this integration uses for "subscribers". /// - ulong ExpireBehavior { get; } + ulong? RoleId { get; } + /// + /// Gets whether emoticons should be synced for this integration (twitch only currently). + /// + bool? HasEnabledEmoticons { get; } + /// + /// Gets the behavior of expiring subscribers. + /// + IntegrationExpireBehavior? ExpireBehavior { get; } /// /// Gets the grace period before expiring "subscribers". /// - ulong ExpireGracePeriod { get; } + int? ExpireGracePeriod { get; } + /// + /// Gets the user for this integration. + /// + IUser User { get; } + /// + /// Gets integration account information. + /// + IIntegrationAccount Account { get; } /// /// Gets when this integration was last synced. /// /// /// A containing a date and time of day when the integration was last synced. /// - DateTimeOffset SyncedAt { get; } + DateTimeOffset? SyncedAt { get; } /// - /// Gets integration account information. + /// Gets how many subscribers this integration has. /// - IntegrationAccount Account { get; } - + int? SubscriberCount { get; } + /// + /// Gets whether this integration been revoked. + /// + bool? IsRevoked { get; } + /// + /// Gets the bot/OAuth2 application for a discord integration. + /// + IIntegrationApplication Application { get; } + IGuild Guild { get; } ulong GuildId { get; } - ulong RoleId { get; } - IUser User { get; } } } diff --git a/src/Discord.Net.Core/Entities/Integrations/IIntegrationAccount.cs b/src/Discord.Net.Core/Entities/Integrations/IIntegrationAccount.cs new file mode 100644 index 000000000..322ffa5c2 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Integrations/IIntegrationAccount.cs @@ -0,0 +1,23 @@ +namespace Discord +{ + /// + /// Provides the account information for an . + /// + public interface IIntegrationAccount + { + /// + /// Gets the ID of the account. + /// + /// + /// A unique identifier of this integration account. + /// + string Id { get; } + /// + /// Gets the name of the account. + /// + /// + /// A string containing the name of this integration account. + /// + string Name { get; } + } +} diff --git a/src/Discord.Net.Core/Entities/Integrations/IIntegrationApplication.cs b/src/Discord.Net.Core/Entities/Integrations/IIntegrationApplication.cs new file mode 100644 index 000000000..9085ae686 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Integrations/IIntegrationApplication.cs @@ -0,0 +1,33 @@ +namespace Discord +{ + /// + /// Provides the bot/OAuth2 application for an . + /// + public interface IIntegrationApplication + { + /// + /// Gets the id of the app. + /// + ulong Id { get; } + /// + /// Gets the name of the app. + /// + string Name { get; } + /// + /// Gets the icon hash of the app. + /// + string Icon { get; } + /// + /// Gets the description of the app. + /// + string Description { get; } + /// + /// Gets the summary of the app. + /// + string Summary { get; } + /// + /// Gets the bot associated with this application. + /// + IUser Bot { get; } + } +} diff --git a/src/Discord.Net.Core/Entities/Integrations/IntegrationExpireBehavior.cs b/src/Discord.Net.Core/Entities/Integrations/IntegrationExpireBehavior.cs new file mode 100644 index 000000000..642e247eb --- /dev/null +++ b/src/Discord.Net.Core/Entities/Integrations/IntegrationExpireBehavior.cs @@ -0,0 +1,17 @@ +namespace Discord +{ + /// + /// The behavior of expiring subscribers for an . + /// + public enum IntegrationExpireBehavior + { + /// + /// Removes a role from an expired subscriber. + /// + RemoveRole = 0, + /// + /// Kicks an expired subscriber from the guild. + /// + Kick = 1 + } +} diff --git a/src/Discord.Net.Core/Entities/Users/ConnectionVisibility.cs b/src/Discord.Net.Core/Entities/Users/ConnectionVisibility.cs new file mode 100644 index 000000000..ed041c9f9 --- /dev/null +++ b/src/Discord.Net.Core/Entities/Users/ConnectionVisibility.cs @@ -0,0 +1,17 @@ +namespace Discord +{ + /// + /// The visibility of the connected account. + /// + public enum ConnectionVisibility + { + /// + /// Invisible to everyone except the user themselves. + /// + None = 0, + /// + /// Visible to everyone. + /// + Everyone = 1 + } +} diff --git a/src/Discord.Net.Core/Entities/Users/IConnection.cs b/src/Discord.Net.Core/Entities/Users/IConnection.cs index 1e65d971f..94b23a4b5 100644 --- a/src/Discord.Net.Core/Entities/Users/IConnection.cs +++ b/src/Discord.Net.Core/Entities/Users/IConnection.cs @@ -4,24 +4,53 @@ namespace Discord { public interface IConnection { - /// Gets the ID of the connection account. - /// A representing the unique identifier value of this connection. + /// + /// Gets the ID of the connection account. + /// + /// + /// A representing the unique identifier value of this connection. + /// string Id { get; } - /// Gets the service of the connection (twitch, youtube). - /// A string containing the name of this type of connection. - string Type { get; } - /// Gets the username of the connection account. - /// A string containing the name of this connection. + /// + /// Gets the username of the connection account. + /// + /// + /// A string containing the name of this connection. + /// string Name { get; } - /// Gets whether the connection is revoked. - /// A value which if true indicates that this connection has been revoked, otherwise false. - bool IsRevoked { get; } - - /// Gets a of integration IDs. + /// + /// Gets the service of the connection (twitch, youtube). + /// + /// + /// A string containing the name of this type of connection. + /// + string Type { get; } + /// + /// Gets whether the connection is revoked. + /// /// - /// An containing - /// representations of unique identifier values of integrations. + /// A value which if true indicates that this connection has been revoked, otherwise false. /// - IReadOnlyCollection IntegrationIds { get; } + bool? IsRevoked { get; } + /// + /// Gets a of integration parials. + /// + IReadOnlyCollection Integrations { get; } + /// + /// Gets whether the connection is verified. + /// + bool Verified { get; } + /// + /// Gets whether friend sync is enabled for this connection. + /// + bool FriendSync { get; } + /// + /// Gets whether activities related to this connection will be shown in presence updates. + /// + bool ShowActivity { get; } + /// + /// Visibility of this connection. + /// + ConnectionVisibility Visibility { get; } } } diff --git a/src/Discord.Net.Rest/API/Common/Connection.cs b/src/Discord.Net.Rest/API/Common/Connection.cs index bd8de3902..0a9940e23 100644 --- a/src/Discord.Net.Rest/API/Common/Connection.cs +++ b/src/Discord.Net.Rest/API/Common/Connection.cs @@ -7,14 +7,22 @@ namespace Discord.API { [JsonProperty("id")] public string Id { get; set; } - [JsonProperty("type")] - public string Type { get; set; } [JsonProperty("name")] public string Name { get; set; } + [JsonProperty("type")] + public string Type { get; set; } [JsonProperty("revoked")] - public bool Revoked { get; set; } - + public Optional Revoked { get; set; } [JsonProperty("integrations")] - public IReadOnlyCollection Integrations { get; set; } + public Optional> Integrations { get; set; } + [JsonProperty("verified")] + public bool Verified { get; set; } + [JsonProperty("friend_sync")] + public bool FriendSync { get; set; } + [JsonProperty("show_activity")] + public bool ShowActivity { get; set; } + [JsonProperty("visibility")] + public ConnectionVisibility Visibility { get; set; } + } } diff --git a/src/Discord.Net.Rest/API/Common/Integration.cs b/src/Discord.Net.Rest/API/Common/Integration.cs index 47d67e149..5a2b00001 100644 --- a/src/Discord.Net.Rest/API/Common/Integration.cs +++ b/src/Discord.Net.Rest/API/Common/Integration.cs @@ -5,6 +5,9 @@ namespace Discord.API { internal class Integration { + [JsonProperty("guild_id")] + public Optional GuildId { get; set; } + [JsonProperty("id")] public ulong Id { get; set; } [JsonProperty("name")] @@ -14,18 +17,26 @@ namespace Discord.API [JsonProperty("enabled")] public bool Enabled { get; set; } [JsonProperty("syncing")] - public bool Syncing { get; set; } + public Optional Syncing { get; set; } [JsonProperty("role_id")] - public ulong RoleId { get; set; } + public Optional RoleId { get; set; } + [JsonProperty("enable_emoticons")] + public Optional EnableEmoticons { get; set; } [JsonProperty("expire_behavior")] - public ulong ExpireBehavior { get; set; } + public Optional ExpireBehavior { get; set; } [JsonProperty("expire_grace_period")] - public ulong ExpireGracePeriod { get; set; } + public Optional ExpireGracePeriod { get; set; } [JsonProperty("user")] - public User User { get; set; } + public Optional User { get; set; } [JsonProperty("account")] - public IntegrationAccount Account { get; set; } + public Optional Account { get; set; } [JsonProperty("synced_at")] - public DateTimeOffset SyncedAt { get; set; } + public Optional SyncedAt { get; set; } + [JsonProperty("subscriber_count")] + public Optional SubscriberAccount { get; set; } + [JsonProperty("revoked")] + public Optional Revoked { get; set; } + [JsonProperty("application")] + public Optional Application { get; set; } } } diff --git a/src/Discord.Net.Rest/API/Common/IntegrationAccount.cs b/src/Discord.Net.Rest/API/Common/IntegrationAccount.cs index a8d33931c..6b8328074 100644 --- a/src/Discord.Net.Rest/API/Common/IntegrationAccount.cs +++ b/src/Discord.Net.Rest/API/Common/IntegrationAccount.cs @@ -5,7 +5,7 @@ namespace Discord.API internal class IntegrationAccount { [JsonProperty("id")] - public ulong Id { get; set; } + public string Id { get; set; } [JsonProperty("name")] public string Name { get; set; } } diff --git a/src/Discord.Net.Rest/API/Common/IntegrationApplication.cs b/src/Discord.Net.Rest/API/Common/IntegrationApplication.cs new file mode 100644 index 000000000..4e07398b8 --- /dev/null +++ b/src/Discord.Net.Rest/API/Common/IntegrationApplication.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; + +namespace Discord.API +{ + internal class IntegrationApplication + { + [JsonProperty("id")] + public ulong Id { get; set; } + [JsonProperty("name")] + public string Name { get; set; } + [JsonProperty("icon")] + public Optional Icon { get; set; } + [JsonProperty("description")] + public string Description { get; set; } + [JsonProperty("summary")] + public string Summary { get; set; } + [JsonProperty("bot")] + public Optional Bot { get; set; } + } +} diff --git a/src/Discord.Net.Rest/ClientHelper.cs b/src/Discord.Net.Rest/ClientHelper.cs index 5debea27e..c6ad6a9fb 100644 --- a/src/Discord.Net.Rest/ClientHelper.cs +++ b/src/Discord.Net.Rest/ClientHelper.cs @@ -49,7 +49,7 @@ namespace Discord.Rest public static async Task> GetConnectionsAsync(BaseDiscordClient client, RequestOptions options) { var models = await client.ApiClient.GetMyConnectionsAsync(options).ConfigureAwait(false); - return models.Select(RestConnection.Create).ToImmutableArray(); + return models.Select(model => RestConnection.Create(client, model)).ToImmutableArray(); } public static async Task GetInviteAsync(BaseDiscordClient client, diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index f6d579d79..645e6711c 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -1626,7 +1626,7 @@ namespace Discord.API #region Guild Integrations /// must not be equal to zero. - public async Task> GetGuildIntegrationsAsync(ulong guildId, RequestOptions options = null) + public async Task> GetIntegrationsAsync(ulong guildId, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); options = RequestOptions.CreateOrClone(options); @@ -1634,47 +1634,14 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); return await SendAsync>("GET", () => $"guilds/{guildId}/integrations", ids, options: options).ConfigureAwait(false); } - /// and must not be equal to zero. - /// must not be . - public async Task CreateGuildIntegrationAsync(ulong guildId, CreateGuildIntegrationParams args, RequestOptions options = null) - { - Preconditions.NotEqual(guildId, 0, nameof(guildId)); - Preconditions.NotNull(args, nameof(args)); - Preconditions.NotEqual(args.Id, 0, nameof(args.Id)); - options = RequestOptions.CreateOrClone(options); - - var ids = new BucketIds(guildId: guildId); - return await SendAsync("POST", () => $"guilds/{guildId}/integrations", ids, options: options).ConfigureAwait(false); - } - public async Task DeleteGuildIntegrationAsync(ulong guildId, ulong integrationId, RequestOptions options = null) - { - Preconditions.NotEqual(guildId, 0, nameof(guildId)); - Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); - options = RequestOptions.CreateOrClone(options); - - var ids = new BucketIds(guildId: guildId); - return await SendAsync("DELETE", () => $"guilds/{guildId}/integrations/{integrationId}", ids, options: options).ConfigureAwait(false); - } - public async Task ModifyGuildIntegrationAsync(ulong guildId, ulong integrationId, Rest.ModifyGuildIntegrationParams args, RequestOptions options = null) - { - Preconditions.NotEqual(guildId, 0, nameof(guildId)); - Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); - Preconditions.NotNull(args, nameof(args)); - Preconditions.AtLeast(args.ExpireBehavior, 0, nameof(args.ExpireBehavior)); - Preconditions.AtLeast(args.ExpireGracePeriod, 0, nameof(args.ExpireGracePeriod)); - options = RequestOptions.CreateOrClone(options); - - var ids = new BucketIds(guildId: guildId); - return await SendJsonAsync("PATCH", () => $"guilds/{guildId}/integrations/{integrationId}", args, ids, options: options).ConfigureAwait(false); - } - public async Task SyncGuildIntegrationAsync(ulong guildId, ulong integrationId, RequestOptions options = null) + public async Task DeleteIntegrationAsync(ulong guildId, ulong integrationId, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(integrationId, 0, nameof(integrationId)); options = RequestOptions.CreateOrClone(options); var ids = new BucketIds(guildId: guildId); - return await SendAsync("POST", () => $"guilds/{guildId}/integrations/{integrationId}/sync", ids, options: options).ConfigureAwait(false); + await SendAsync("DELETE", () => $"guilds/{guildId}/integrations/{integrationId}", ids, options: options).ConfigureAwait(false); } #endregion diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 25f474dcc..7dbe20881 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -305,19 +305,15 @@ namespace Discord.Rest #endregion #region Integrations - public static async Task> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client, + public static async Task> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client, RequestOptions options) { - var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false); - return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray(); - } - public static async Task CreateIntegrationAsync(IGuild guild, BaseDiscordClient client, - ulong id, string type, RequestOptions options) - { - var args = new CreateGuildIntegrationParams(id, type); - var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false); - return RestGuildIntegration.Create(client, guild, model); + var models = await client.ApiClient.GetIntegrationsAsync(guild.Id, options).ConfigureAwait(false); + return models.Select(x => RestIntegration.Create(client, guild, x)).ToImmutableArray(); } + public static async Task DeleteIntegrationAsync(IGuild guild, BaseDiscordClient client, ulong id, + RequestOptions options) => + await client.ApiClient.DeleteIntegrationAsync(guild.Id, id, options).ConfigureAwait(false); #endregion #region Interactions diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index e89096f00..d7ab65a55 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -720,10 +720,10 @@ namespace Discord.Rest #endregion #region Integrations - public Task> GetIntegrationsAsync(RequestOptions options = null) + public Task> GetIntegrationsAsync(RequestOptions options = null) => GuildHelper.GetIntegrationsAsync(this, Discord, options); - public Task CreateIntegrationAsync(ulong id, string type, RequestOptions options = null) - => GuildHelper.CreateIntegrationAsync(this, Discord, id, type, options); + public Task DeleteIntegrationAsync(ulong id, RequestOptions options = null) + => GuildHelper.DeleteIntegrationAsync(this, Discord, id, options); #endregion #region Invites @@ -1370,11 +1370,11 @@ namespace Discord.Rest => await GetVoiceRegionsAsync(options).ConfigureAwait(false); /// - async Task> IGuild.GetIntegrationsAsync(RequestOptions options) + async Task> IGuild.GetIntegrationsAsync(RequestOptions options) => await GetIntegrationsAsync(options).ConfigureAwait(false); /// - async Task IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options) - => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false); + async Task IGuild.DeleteIntegrationAsync(ulong id, RequestOptions options) + => await DeleteIntegrationAsync(id, options).ConfigureAwait(false); /// async Task> IGuild.GetInvitesAsync(RequestOptions options) diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs deleted file mode 100644 index 9759e64d2..000000000 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuildIntegration.cs +++ /dev/null @@ -1,104 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading.Tasks; -using Model = Discord.API.Integration; - -namespace Discord.Rest -{ - [DebuggerDisplay(@"{DebuggerDisplay,nq}")] - public class RestGuildIntegration : RestEntity, IGuildIntegration - { - private long _syncedAtTicks; - - /// - public string Name { get; private set; } - /// - public string Type { get; private set; } - /// - public bool IsEnabled { get; private set; } - /// - public bool IsSyncing { get; private set; } - /// - public ulong ExpireBehavior { get; private set; } - /// - public ulong ExpireGracePeriod { get; private set; } - /// - public ulong GuildId { get; private set; } - /// - public ulong RoleId { get; private set; } - public RestUser User { get; private set; } - /// - public IntegrationAccount Account { get; private set; } - internal IGuild Guild { get; private set; } - - /// - public DateTimeOffset SyncedAt => DateTimeUtils.FromTicks(_syncedAtTicks); - - internal RestGuildIntegration(BaseDiscordClient discord, IGuild guild, ulong id) - : base(discord, id) - { - Guild = guild; - } - internal static RestGuildIntegration Create(BaseDiscordClient discord, IGuild guild, Model model) - { - var entity = new RestGuildIntegration(discord, guild, model.Id); - entity.Update(model); - return entity; - } - - internal void Update(Model model) - { - Name = model.Name; - Type = model.Type; - IsEnabled = model.Enabled; - IsSyncing = model.Syncing; - ExpireBehavior = model.ExpireBehavior; - ExpireGracePeriod = model.ExpireGracePeriod; - _syncedAtTicks = model.SyncedAt.UtcTicks; - - RoleId = model.RoleId; - User = RestUser.Create(Discord, model.User); - } - - public async Task DeleteAsync() - { - await Discord.ApiClient.DeleteGuildIntegrationAsync(GuildId, Id).ConfigureAwait(false); - } - public async Task ModifyAsync(Action func) - { - if (func == null) throw new NullReferenceException(nameof(func)); - - var args = new GuildIntegrationProperties(); - func(args); - var apiArgs = new API.Rest.ModifyGuildIntegrationParams - { - EnableEmoticons = args.EnableEmoticons, - ExpireBehavior = args.ExpireBehavior, - ExpireGracePeriod = args.ExpireGracePeriod - }; - var model = await Discord.ApiClient.ModifyGuildIntegrationAsync(GuildId, Id, apiArgs).ConfigureAwait(false); - - Update(model); - } - public async Task SyncAsync() - { - await Discord.ApiClient.SyncGuildIntegrationAsync(GuildId, Id).ConfigureAwait(false); - } - - public override string ToString() => Name; - private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})"; - - /// - IGuild IGuildIntegration.Guild - { - get - { - if (Guild != null) - return Guild; - throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); - } - } - /// - IUser IGuildIntegration.User => User; - } -} diff --git a/src/Discord.Net.Rest/Entities/Integrations/RestIntegration.cs b/src/Discord.Net.Rest/Entities/Integrations/RestIntegration.cs new file mode 100644 index 000000000..e92ecdded --- /dev/null +++ b/src/Discord.Net.Rest/Entities/Integrations/RestIntegration.cs @@ -0,0 +1,102 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using Model = Discord.API.Integration; + +namespace Discord.Rest +{ + /// + /// Represents a Rest-based implementation of . + /// + [DebuggerDisplay(@"{DebuggerDisplay,nq}")] + public class RestIntegration : RestEntity, IIntegration + { + private long? _syncedAtTicks; + + /// + public string Name { get; private set; } + /// + public string Type { get; private set; } + /// + public bool IsEnabled { get; private set; } + /// + public bool? IsSyncing { get; private set; } + /// + public ulong? RoleId { get; private set; } + /// + public bool? HasEnabledEmoticons { get; private set; } + /// + public IntegrationExpireBehavior? ExpireBehavior { get; private set; } + /// + public int? ExpireGracePeriod { get; private set; } + /// + IUser IIntegration.User => User; + /// + public IIntegrationAccount Account { get; private set; } + /// + public DateTimeOffset? SyncedAt => DateTimeUtils.FromTicks(_syncedAtTicks); + /// + public int? SubscriberCount { get; private set; } + /// + public bool? IsRevoked { get; private set; } + /// + public IIntegrationApplication Application { get; private set; } + + internal IGuild Guild { get; private set; } + public RestUser User { get; private set; } + + internal RestIntegration(BaseDiscordClient discord, IGuild guild, ulong id) + : base(discord, id) + { + Guild = guild; + } + internal static RestIntegration Create(BaseDiscordClient discord, IGuild guild, Model model) + { + var entity = new RestIntegration(discord, guild, model.Id); + entity.Update(model); + return entity; + } + + internal void Update(Model model) + { + Name = model.Name; + Type = model.Type; + IsEnabled = model.Enabled; + + IsSyncing = model.Syncing.IsSpecified ? model.Syncing.Value : null; + RoleId = model.RoleId.IsSpecified ? model.RoleId.Value : null; + HasEnabledEmoticons = model.EnableEmoticons.IsSpecified ? model.EnableEmoticons.Value : null; + ExpireBehavior = model.ExpireBehavior.IsSpecified ? model.ExpireBehavior.Value : null; + ExpireGracePeriod = model.ExpireGracePeriod.IsSpecified ? model.ExpireGracePeriod.Value : null; + User = model.User.IsSpecified ? RestUser.Create(Discord, model.User.Value) : null; + Account = model.Account.IsSpecified ? RestIntegrationAccount.Create(model.Account.Value) : null; + SubscriberCount = model.SubscriberAccount.IsSpecified ? model.SubscriberAccount.Value : null; + IsRevoked = model.Revoked.IsSpecified ? model.Revoked.Value : null; + Application = model.Application.IsSpecified ? RestIntegrationApplication.Create(Discord, model.Application.Value) : null; + + _syncedAtTicks = model.SyncedAt.IsSpecified ? model.SyncedAt.Value.UtcTicks : null; + } + + public async Task DeleteAsync() + { + await Discord.ApiClient.DeleteIntegrationAsync(GuildId, Id).ConfigureAwait(false); + } + + public override string ToString() => Name; + private string DebuggerDisplay => $"{Name} ({Id}{(IsEnabled ? ", Enabled" : "")})"; + + /// + public ulong GuildId { get; private set; } + + /// + IGuild IIntegration.Guild + { + get + { + if (Guild != null) + return Guild; + throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object."); + } + } + } +} diff --git a/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationAccount.cs b/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationAccount.cs new file mode 100644 index 000000000..6d83aa1f0 --- /dev/null +++ b/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationAccount.cs @@ -0,0 +1,29 @@ +using Model = Discord.API.IntegrationAccount; + +namespace Discord.Rest +{ + /// + /// Represents a Rest-based implementation of . + /// + public class RestIntegrationAccount : IIntegrationAccount + { + internal RestIntegrationAccount() { } + + public string Id { get; private set; } + + public string Name { get; private set; } + + internal static RestIntegrationAccount Create(Model model) + { + var entity = new RestIntegrationAccount(); + entity.Update(model); + return entity; + } + + internal void Update(Model model) + { + model.Name = Name; + model.Id = Id; + } + } +} diff --git a/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationApplication.cs b/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationApplication.cs new file mode 100644 index 000000000..e532ac970 --- /dev/null +++ b/src/Discord.Net.Rest/Entities/Integrations/RestIntegrationApplication.cs @@ -0,0 +1,39 @@ +using Model = Discord.API.IntegrationApplication; + +namespace Discord.Rest +{ + /// + /// Represents a Rest-based implementation of . + /// + public class RestIntegrationApplication : RestEntity, IIntegrationApplication + { + public string Name { get; private set; } + + public string Icon { get; private set; } + + public string Description { get; private set; } + + public string Summary { get; private set; } + + public IUser Bot { get; private set; } + + internal RestIntegrationApplication(BaseDiscordClient discord, ulong id) + : base(discord, id) { } + + internal static RestIntegrationApplication Create(BaseDiscordClient discord, Model model) + { + var entity = new RestIntegrationApplication(discord, model.Id); + entity.Update(model); + return entity; + } + + internal void Update(Model model) + { + Name = model.Name; + Icon = model.Icon.IsSpecified ? model.Icon.Value : null; + Description = model.Description; + Summary = model.Summary; + Bot = RestUser.Create(Discord, model.Bot.Value); + } + } +} diff --git a/src/Discord.Net.Rest/Entities/Users/RestConnection.cs b/src/Discord.Net.Rest/Entities/Users/RestConnection.cs index 1afb813c0..496279727 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestConnection.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestConnection.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using System.Collections.Immutable; +using System.Collections.ObjectModel; using System.Diagnostics; +using System.Linq; using Model = Discord.API.Connection; namespace Discord.Rest @@ -9,28 +11,49 @@ namespace Discord.Rest public class RestConnection : IConnection { /// - public string Id { get; } + public string Id { get; private set; } /// - public string Type { get; } + public string Name { get; private set; } /// - public string Name { get; } + public string Type { get; private set; } /// - public bool IsRevoked { get; } + public bool? IsRevoked { get; private set; } /// - public IReadOnlyCollection IntegrationIds { get; } + public IReadOnlyCollection Integrations { get; private set; } + /// + public bool Verified { get; private set; } + /// + public bool FriendSync { get; private set; } + /// + public bool ShowActivity { get; private set; } + /// + public ConnectionVisibility Visibility { get; private set; } - internal RestConnection(string id, string type, string name, bool isRevoked, IReadOnlyCollection integrationIds) - { - Id = id; - Type = type; - Name = name; - IsRevoked = isRevoked; + internal BaseDiscordClient Discord { get; } - IntegrationIds = integrationIds; + internal RestConnection(BaseDiscordClient discord) { + Discord = discord; } - internal static RestConnection Create(Model model) + + internal static RestConnection Create(BaseDiscordClient discord, Model model) + { + var entity = new RestConnection(discord); + entity.Update(model); + return entity; + } + + internal void Update(Model model) { - return new RestConnection(model.Id, model.Type, model.Name, model.Revoked, model.Integrations.ToImmutableArray()); + Id = model.Id; + Name = model.Name; + Type = model.Type; + IsRevoked = model.Revoked.IsSpecified ? model.Revoked.Value : null; + Integrations = model.Integrations.IsSpecified ?model.Integrations.Value + .Select(intergration => RestIntegration.Create(Discord, null, intergration)).ToImmutableArray() : null; + Verified = model.Verified; + FriendSync = model.FriendSync; + ShowActivity = model.ShowActivity; + Visibility = model.Visibility; } /// @@ -40,6 +63,6 @@ namespace Discord.Rest /// Name of the connection. /// public override string ToString() => Name; - private string DebuggerDisplay => $"{Name} ({Id}, {Type}{(IsRevoked ? ", Revoked" : "")})"; + private string DebuggerDisplay => $"{Name} ({Id}, {Type}{(IsRevoked.GetValueOrDefault() ? ", Revoked" : "")})"; } } diff --git a/src/Discord.Net.WebSocket/API/Gateway/IntegrationDeletedEvent.cs b/src/Discord.Net.WebSocket/API/Gateway/IntegrationDeletedEvent.cs new file mode 100644 index 000000000..cf6e70ca6 --- /dev/null +++ b/src/Discord.Net.WebSocket/API/Gateway/IntegrationDeletedEvent.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; + +namespace Discord.API.Gateway +{ + internal class IntegrationDeletedEvent + { + [JsonProperty("id")] + public ulong Id { get; set; } + [JsonProperty("guild_id")] + public ulong GuildId { get; set; } + [JsonProperty("application_id")] + public Optional ApplicationID { get; set; } + } +} diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs index b8d3b6a10..c47591418 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.Events.cs @@ -415,6 +415,32 @@ namespace Discord.WebSocket #endregion + #region Integrations + /// Fired when an integration is created. + public event Func IntegrationCreated + { + add { _integrationCreated.Add(value); } + remove { _integrationCreated.Remove(value); } + } + internal readonly AsyncEvent> _integrationCreated = new AsyncEvent>(); + + /// Fired when an integration is updated. + public event Func IntegrationUpdated + { + add { _integrationUpdated.Add(value); } + remove { _integrationUpdated.Remove(value); } + } + internal readonly AsyncEvent> _integrationUpdated = new AsyncEvent>(); + + /// Fired when an integration is deleted. + public event Func, Task> IntegrationDeleted + { + add { _integrationDeleted.Add(value); } + remove { _integrationDeleted.Remove(value); } + } + internal readonly AsyncEvent, Task>> _integrationDeleted = new AsyncEvent, Task>>(); + #endregion + #region Users /// Fired when a user joins a guild. public event Func UserJoined diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index b692f0691..f33d89047 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -2017,6 +2017,92 @@ namespace Discord.WebSocket break; #endregion + #region Integrations + case "INTEGRATION_CREATE": + { + await _gatewayLogger.DebugAsync("Received Dispatch (INTEGRATION_CREATE)").ConfigureAwait(false); + + var data = (payload as JToken).ToObject(_serializer); + + // Integrations from Gateway should always have guild IDs specified. + if (!data.GuildId.IsSpecified) + return; + + var guild = State.GetGuild(data.GuildId.Value); + + if (guild != null) + { + if (!guild.IsSynced) + { + await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); + return; + } + + await TimedInvokeAsync(_integrationCreated, nameof(IntegrationCreated), RestIntegration.Create(this, guild, data)).ConfigureAwait(false); + } + else + { + await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false); + return; + } + } + break; + case "INTEGRATION_UPDATE": + { + await _gatewayLogger.DebugAsync("Received Dispatch (INTEGRATION_UPDATE)").ConfigureAwait(false); + + var data = (payload as JToken).ToObject(_serializer); + + // Integrations from Gateway should always have guild IDs specified. + if (!data.GuildId.IsSpecified) + return; + + var guild = State.GetGuild(data.GuildId.Value); + + if (guild != null) + { + if (!guild.IsSynced) + { + await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); + return; + } + + await TimedInvokeAsync(_integrationUpdated, nameof(IntegrationUpdated), RestIntegration.Create(this, guild, data)).ConfigureAwait(false); + } + else + { + await UnknownGuildAsync(type, data.GuildId.Value).ConfigureAwait(false); + return; + } + } + break; + case "INTEGRATION_DELETE": + { + await _gatewayLogger.DebugAsync("Received Dispatch (INTEGRATION_DELETE)").ConfigureAwait(false); + + var data = (payload as JToken).ToObject(_serializer); + + var guild = State.GetGuild(data.GuildId); + + if (guild != null) + { + if (!guild.IsSynced) + { + await UnsyncedGuildAsync(type, guild.Id).ConfigureAwait(false); + return; + } + + await TimedInvokeAsync(_integrationDeleted, nameof(IntegrationDeleted), guild, data.Id, data.ApplicationID).ConfigureAwait(false); + } + else + { + await UnknownGuildAsync(type, data.GuildId).ConfigureAwait(false); + return; + } + } + break; + #endregion + #region Users case "USER_UPDATE": { diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index c4b756410..47bd57552 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -847,10 +847,10 @@ namespace Discord.WebSocket #endregion #region Integrations - public Task> GetIntegrationsAsync(RequestOptions options = null) + public Task> GetIntegrationsAsync(RequestOptions options = null) => GuildHelper.GetIntegrationsAsync(this, Discord, options); - public Task CreateIntegrationAsync(ulong id, string type, RequestOptions options = null) - => GuildHelper.CreateIntegrationAsync(this, Discord, id, type, options); + public Task DeleteIntegrationAsync(ulong id, RequestOptions options = null) + => GuildHelper.DeleteIntegrationAsync(this, Discord, id, options); #endregion #region Interactions @@ -1888,11 +1888,11 @@ namespace Discord.WebSocket => await GetVoiceRegionsAsync(options).ConfigureAwait(false); /// - async Task> IGuild.GetIntegrationsAsync(RequestOptions options) + async Task> IGuild.GetIntegrationsAsync(RequestOptions options) => await GetIntegrationsAsync(options).ConfigureAwait(false); /// - async Task IGuild.CreateIntegrationAsync(ulong id, string type, RequestOptions options) - => await CreateIntegrationAsync(id, type, options).ConfigureAwait(false); + async Task IGuild.DeleteIntegrationAsync(ulong id, RequestOptions options) + => await DeleteIntegrationAsync(id, options).ConfigureAwait(false); /// async Task> IGuild.GetInvitesAsync(RequestOptions options) From 91d8fabb70ad9008b4d205b3c38675289f2ca08e Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Sat, 26 Mar 2022 10:35:25 -0300 Subject: [PATCH 15/25] Fix: GuildPermissions.All not including newer permissions (#2209) --- src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs b/src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs index 649944ede..4c3125907 100644 --- a/src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs +++ b/src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs @@ -13,7 +13,7 @@ namespace Discord /// Gets a that grants all guild permissions for webhook users. public static readonly GuildPermissions Webhook = new GuildPermissions(0b0_00000_0000000_0000000_0001101100000_000000); /// Gets a that grants all guild permissions. - public static readonly GuildPermissions All = new GuildPermissions(0b1_11111_1111111_1111111_1111111111111_111111); + public static readonly GuildPermissions All = new GuildPermissions(ulong.MaxValue); /// Gets a packed value representing all the permissions in this . public ulong RawValue { get; } From 73399459eacbc15187edf4dc9e26fd934b8a7fad Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Sat, 26 Mar 2022 16:21:26 -0300 Subject: [PATCH 16/25] feature: add a way to remove type readers from the interaction/command service. (#2210) * Add remove methods * add inline docs Co-authored-by: Cenngo --- src/Discord.Net.Commands/CommandService.cs | 35 +++++++++++++ .../InteractionService.cs | 52 +++++++++++++++++++ src/Discord.Net.Interactions/Map/TypeMap.cs | 12 +++++ 3 files changed, 99 insertions(+) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index d6dfc2fb7..57e0e430e 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -403,6 +403,41 @@ namespace Discord.Commands AddNullableTypeReader(type, reader); } } + + /// + /// Removes a type reader from the list of type readers. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the readers from. + /// if the default readers for should be removed; otherwise . + /// The removed collection of type readers. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(Type type, bool isDefaultTypeReader, out IDictionary readers) + { + readers = new Dictionary(); + + if (isDefaultTypeReader) + { + var isSuccess = _defaultTypeReaders.TryRemove(type, out var result); + if (isSuccess) + readers.Add(result?.GetType(), result); + + return isSuccess; + } + else + { + var isSuccess = _typeReaders.TryRemove(type, out var result); + + if (isSuccess) + readers = result; + + return isSuccess; + } + } + internal bool HasDefaultTypeReader(Type type) { if (_defaultTypeReaders.ContainsKey(type)) diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index 927e39735..deb6fa931 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -905,9 +905,61 @@ namespace Discord.Interactions public void AddGenericTypeReader(Type targetType, Type readerType) => _typeReaderMap.AddGeneric(targetType, readerType); + /// + /// Removes a type reader for the type . + /// + /// The type to remove the readers from. + /// The reader if the resulting remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(out TypeReader reader) + => TryRemoveTypeReader(typeof(T), out reader); + + /// + /// Removes a type reader for the given type. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the reader from. + /// The reader if the resulting remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveTypeReader(Type type, out TypeReader reader) + => _typeReaderMap.TryRemoveConcrete(type, out reader); + + /// + /// Removes a generic type reader from the type . + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the readers from. + /// The removed readers type. + /// if the remove operation was successful; otherwise . + public bool TryRemoveGenericTypeReader(out Type readerType) + => TryRemoveGenericTypeReader(typeof(T), out readerType); + + /// + /// Removes a generic type reader from the given type. + /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// + /// The type to remove the reader from. + /// The readers type if the remove operation was successful. + /// if the remove operation was successful; otherwise . + public bool TryRemoveGenericTypeReader(Type type, out Type readerType) + => _typeReaderMap.TryRemoveGeneric(type, out readerType); + /// /// Serialize an object using a into a to be placed in a Component CustomId. /// + /// + /// Removing a from the will not dereference the from the loaded module/command instances. + /// You need to reload the modules for the changes to take effect. + /// /// Type of the object to be serialized. /// Object to be serialized. /// Services that will be passed on to the . diff --git a/src/Discord.Net.Interactions/Map/TypeMap.cs b/src/Discord.Net.Interactions/Map/TypeMap.cs index ef1ef4a53..520ed7231 100644 --- a/src/Discord.Net.Interactions/Map/TypeMap.cs +++ b/src/Discord.Net.Interactions/Map/TypeMap.cs @@ -74,6 +74,18 @@ namespace Discord.Interactions _generics[targetType] = converterType; } + public bool TryRemoveConcrete(out TConverter converter) + => TryRemoveConcrete(typeof(TTarget), out converter); + + public bool TryRemoveConcrete(Type type, out TConverter converter) + => _concretes.TryRemove(type, out converter); + + public bool TryRemoveGeneric(out Type converterType) + => TryRemoveGeneric(typeof(TTarget), out converterType); + + public bool TryRemoveGeneric(Type targetType, out Type converterType) + => _generics.TryRemove(targetType, out converterType); + private Type GetMostSpecific(Type type) { if (_generics.TryGetValue(type, out var matching)) From c4131cfc8bc2aa22d2c133f766782b0ae407df36 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Thu, 31 Mar 2022 21:24:36 +0200 Subject: [PATCH 17/25] Fix: ShardedClients not pushing PresenceUpdates (#2219) --- src/Discord.Net.WebSocket/DiscordShardedClient.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index a361889c0..3a14692e0 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -449,6 +449,7 @@ namespace Discord.WebSocket client.UserBanned += (user, guild) => _userBannedEvent.InvokeAsync(user, guild); client.UserUnbanned += (user, guild) => _userUnbannedEvent.InvokeAsync(user, guild); client.UserUpdated += (oldUser, newUser) => _userUpdatedEvent.InvokeAsync(oldUser, newUser); + client.PresenceUpdated += (user, oldPresence, newPresence) => _presenceUpdated.InvokeAsync(user, oldPresence, newPresence); client.GuildMemberUpdated += (oldUser, newUser) => _guildMemberUpdatedEvent.InvokeAsync(oldUser, newUser); client.UserVoiceStateUpdated += (user, oldVoiceState, newVoiceState) => _userVoiceStateUpdatedEvent.InvokeAsync(user, oldVoiceState, newVoiceState); client.VoiceServerUpdated += (server) => _voiceServerUpdatedEvent.InvokeAsync(server); From 1c680db2bafaf33bd1a660731f74fbfbbe6cb746 Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Tue, 5 Apr 2022 00:11:15 +0300 Subject: [PATCH 18/25] add respondwithmodal methods to restinteractinmodulebase (#2227) --- .../Extensions/RestExtensions.cs | 25 ++++++++++++++ .../RestInteractionModuleBase.cs | 34 +++++++++++++++++++ .../Utilities/ApplicationCommandRestUtil.cs | 21 ++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/Discord.Net.Interactions/Extensions/RestExtensions.cs diff --git a/src/Discord.Net.Interactions/Extensions/RestExtensions.cs b/src/Discord.Net.Interactions/Extensions/RestExtensions.cs new file mode 100644 index 000000000..2641617e0 --- /dev/null +++ b/src/Discord.Net.Interactions/Extensions/RestExtensions.cs @@ -0,0 +1,25 @@ +using Discord.Interactions; +using System; + +namespace Discord.Rest +{ + public static class RestExtensions + { + /// + /// Respond to an interaction with a . + /// + /// Type of the implementation. + /// The interaction to respond to. + /// The request options for this request. + /// Serialized payload to be used to create a HTTP response. + public static string RespondWithModal(this RestInteraction interaction, string customId, RequestOptions options = null, Action modifyModal = null) + where T : class, IModal + { + if (!ModalUtils.TryGet(out var modalInfo)) + throw new ArgumentException($"{typeof(T).FullName} isn't referenced by any registered Modal Interaction Command and doesn't have a cached {typeof(ModalInfo)}"); + + var modal = modalInfo.ToModal(customId, modifyModal); + return interaction.RespondWithModal(modal, options); + } + } +} diff --git a/src/Discord.Net.Interactions/RestInteractionModuleBase.cs b/src/Discord.Net.Interactions/RestInteractionModuleBase.cs index a07614f7f..e83c91fef 100644 --- a/src/Discord.Net.Interactions/RestInteractionModuleBase.cs +++ b/src/Discord.Net.Interactions/RestInteractionModuleBase.cs @@ -65,5 +65,39 @@ namespace Discord.Interactions else await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); } + + /// + /// Responds to the interaction with a modal. + /// + /// The modal to respond with. + /// The request options for this request. + /// A string that contains json to write back to the incoming http request. + /// + /// + protected override async Task RespondWithModalAsync(Modal modal, RequestOptions options = null) + { + if (Context.Interaction is not RestInteraction restInteraction) + throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); + + var payload = restInteraction.RespondWithModal(modal, options); + + if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) + await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); + else + await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); + } + + protected override async Task RespondWithModalAsync(string customId, RequestOptions options = null) + { + if (Context.Interaction is not RestInteraction restInteraction) + throw new InvalidOperationException($"Invalid interaction type. Interaction must be a type of {nameof(RestInteraction)} in order to execute this method"); + + var payload = restInteraction.RespondWithModal(customId, options); + + if (Context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) + await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); + else + await InteractionService._restResponseCallback(Context, payload).ConfigureAwait(false); + } } } diff --git a/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs b/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs index 46f0f4a4a..c2052b7c7 100644 --- a/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs +++ b/src/Discord.Net.Interactions/Utilities/ApplicationCommandRestUtil.cs @@ -196,5 +196,26 @@ namespace Discord.Interactions }).ToList(), Options = commandOption.Options?.Select(x => x.ToApplicationCommandOptionProps()).ToList() }; + + public static Modal ToModal(this ModalInfo modalInfo, string customId, Action modifyModal = null) + { + var builder = new ModalBuilder(modalInfo.Title, customId); + + foreach (var input in modalInfo.Components) + switch (input) + { + case TextInputComponentInfo textComponent: + builder.AddTextInput(textComponent.Label, textComponent.CustomId, textComponent.Style, textComponent.Placeholder, textComponent.IsRequired ? textComponent.MinLength : null, + textComponent.MaxLength, textComponent.IsRequired, textComponent.InitialValue); + break; + default: + throw new InvalidOperationException($"{input.GetType().FullName} isn't a valid component info class"); + } + + if(modifyModal is not null) + modifyModal(builder); + + return builder.Build(); + } } } From d2118f02fb422b88e0d2f4f88cc4003c56967dd3 Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Tue, 5 Apr 2022 00:11:54 +0300 Subject: [PATCH 19/25] Adds a action delegate parameter to `RespondWithModalAsync()` for modifying the modal (#2226) * add modifyModal deleagate parameter to RespondWithModalAsync extension method * change the position of the new parameter to avoid introducing a breaking change --- .../Extensions/IDiscordInteractionExtensions.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs b/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs index 5c379cf42..8f0987661 100644 --- a/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs +++ b/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs @@ -10,9 +10,10 @@ namespace Discord.Interactions /// /// Type of the implementation. /// The interaction to respond to. + /// Delegate that can be used to modify the modal. /// The request options for this request. /// A task that represents the asynchronous operation of responding to the interaction. - public static async Task RespondWithModalAsync(this IDiscordInteraction interaction, string customId, RequestOptions options = null) + public static async Task RespondWithModalAsync(this IDiscordInteraction interaction, string customId, RequestOptions options = null, Action modifyModal = null) where T : class, IModal { if (!ModalUtils.TryGet(out var modalInfo)) @@ -31,6 +32,9 @@ namespace Discord.Interactions throw new InvalidOperationException($"{input.GetType().FullName} isn't a valid component info class"); } + if (modifyModal is not null) + modifyModal(builder); + await interaction.RespondWithModalAsync(builder.Build(), options).ConfigureAwait(false); } } From a7449484772733d8bc122321ddebbd52e98b1b53 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Mon, 4 Apr 2022 23:14:36 +0200 Subject: [PATCH 20/25] feature: Global interaction post execution event. (#2213) * Init * Variable set to event * Put internal above private * Revert "Put internal above private" This reverts commit 77784f001faa58a90edf34edc195d6942ba9b451. * Revert "Variable set to event" This reverts commit 2b0cb81d76e2150a8d692028486aa1d402efe8a3. * Revert "Init" This reverts commit 9892ce7b51d8cf2952d4ae5b67f4a2e4c7917ae2. * Potentially improved approach --- .../InteractionService.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index deb6fa931..01fb8cc9d 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -24,6 +24,29 @@ namespace Discord.Interactions public event Func Log { add { _logEvent.Add(value); } remove { _logEvent.Remove(value); } } internal readonly AsyncEvent> _logEvent = new (); + /// + /// Occurs when any type of interaction is executed. + /// + public event Func InteractionExecuted + { + add + { + SlashCommandExecuted += value; + ContextCommandExecuted += value; + ComponentCommandExecuted += value; + AutocompleteCommandExecuted += value; + ModalCommandExecuted += value; + } + remove + { + SlashCommandExecuted -= value; + ContextCommandExecuted -= value; + ComponentCommandExecuted -= value; + AutocompleteCommandExecuted -= value; + ModalCommandExecuted -= value; + } + } + /// /// Occurs when a Slash Command is executed. /// From 8522447c270b2d9a1409a8cf82070c0f326aac18 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Mon, 4 Apr 2022 23:16:13 +0200 Subject: [PATCH 21/25] Fix gateway interactions not running without bot scope. (#2217) * Init * Implement public channelId --- .../DiscordSocketClient.cs | 10 ++++++--- .../Entities/Interaction/SocketInteraction.cs | 22 ++++++++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index f33d89047..b2da962ab 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -2331,7 +2331,7 @@ namespace Discord.WebSocket SocketUser user = data.User.IsSpecified ? State.GetOrAddUser(data.User.Value.Id, (_) => SocketGlobalUser.Create(this, State, data.User.Value)) - : guild.AddOrUpdateUser(data.Member.Value); + : guild?.AddOrUpdateUser(data.Member.Value); // null if the bot scope isn't set, so the guild cannot be retrieved. SocketChannel channel = null; if(data.ChannelId.IsSpecified) @@ -2346,8 +2346,12 @@ namespace Discord.WebSocket } else { - await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false); - return; + if (guild != null) // The guild id is set, but the guild cannot be found as the bot scope is not set. + { + await UnknownChannelAsync(type, data.ChannelId.Value).ConfigureAwait(false); + return; + } + // The channel isnt required when responding to an interaction, so we can leave the channel null. } } } diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index 8b5bd9c32..5b2da04f5 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -19,13 +19,25 @@ namespace Discord.WebSocket /// Gets the this interaction was used in. /// /// - /// If the channel isn't cached or the bot doesn't have access to it then + /// If the channel isn't cached, the bot scope isn't used, or the bot doesn't have access to it then /// this property will be . /// 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; } @@ -62,8 +74,6 @@ namespace Discord.WebSocket /// public bool IsDMInteraction { get; private set; } - private ulong? _channelId; - internal SocketInteraction(DiscordSocketClient client, ulong id, ISocketMessageChannel channel, SocketUser user) : base(client, id) { @@ -111,7 +121,7 @@ namespace Discord.WebSocket { IsDMInteraction = !model.GuildId.IsSpecified; - _channelId = model.ChannelId.ToNullable(); + ChannelId = model.ChannelId.ToNullable(); Data = model.Data.IsSpecified ? model.Data.Value @@ -396,12 +406,12 @@ namespace Discord.WebSocket if (Channel != null) return Channel; - if (!_channelId.HasValue) + if (!ChannelId.HasValue) return null; try { - return (IMessageChannel)await Discord.GetChannelAsync(_channelId.Value, options).ConfigureAwait(false); + return (IMessageChannel)await Discord.GetChannelAsync(ChannelId.Value, options).ConfigureAwait(false); } catch(HttpException ex) when (ex.DiscordCode == DiscordErrorCode.MissingPermissions) { return null; } // bot can't view that channel, return null instead of throwing. } From ce410513f4009bf14756ca8994abebc559ae0b60 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Mon, 4 Apr 2022 18:19:44 -0300 Subject: [PATCH 22/25] feature: build overrides (#2212) * add build overrides * override docs * add server submodule * add overrides to build step * remove testing api url Co-Authored-By: Quahu Co-authored-by: Quahu --- .gitmodules | 3 + Discord.Net.sln | 17 +- azure/deploy.yml | 1 + docs/faq/build_overrides/what-are-they.md | 41 +++ docs/faq/toc.yml | 2 + .../BuildOverrides.cs | 278 ++++++++++++++++++ .../Discord.Net.BuildOverrides.csproj | 20 ++ .../Discord.Net.BuildOverrides/IOverride.cs | 34 +++ .../OverrideContext.cs | 30 ++ overrides/Discord.Net.BuildOverrides | 1 + 10 files changed, 426 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 100644 docs/faq/build_overrides/what-are-they.md create mode 100644 experiment/Discord.Net.BuildOverrides/BuildOverrides.cs create mode 100644 experiment/Discord.Net.BuildOverrides/Discord.Net.BuildOverrides.csproj create mode 100644 experiment/Discord.Net.BuildOverrides/IOverride.cs create mode 100644 experiment/Discord.Net.BuildOverrides/OverrideContext.cs create mode 160000 overrides/Discord.Net.BuildOverrides diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..71d50ed3a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "overrides/Discord.Net.BuildOverrides"] + path = overrides/Discord.Net.BuildOverrides + url = https://github.com/discord-net/Discord.Net.BuildOverrides diff --git a/Discord.Net.sln b/Discord.Net.sln index fc68eb71c..544283b8b 100644 --- a/Discord.Net.sln +++ b/Discord.Net.sln @@ -34,7 +34,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_InteractionFramework", "sa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_WebhookClient", "samples\WebhookClient\_WebhookClient.csproj", "{B61AAE66-15CC-40E4-873A-C23E697C3411}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IDN", "samples\idn\idn.csproj", "{4A03840B-9EBE-47E3-89AB-E0914DF21AFB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "idn", "samples\idn\idn.csproj", "{4A03840B-9EBE-47E3-89AB-E0914DF21AFB}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{C7CF5621-7D36-433B-B337-5A2E3C101A71}" EndProject @@ -44,6 +44,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions", EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{BB59D5B5-E7B0-4BF4-8F82-D14431B2799B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.BuildOverrides", "experiment\Discord.Net.BuildOverrides\Discord.Net.BuildOverrides.csproj", "{115F4921-B44D-4F69-996B-69796959C99D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -258,6 +260,18 @@ Global {4A03840B-9EBE-47E3-89AB-E0914DF21AFB}.Release|x64.Build.0 = Release|Any CPU {4A03840B-9EBE-47E3-89AB-E0914DF21AFB}.Release|x86.ActiveCfg = Release|Any CPU {4A03840B-9EBE-47E3-89AB-E0914DF21AFB}.Release|x86.Build.0 = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|x64.ActiveCfg = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|x64.Build.0 = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|x86.ActiveCfg = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Debug|x86.Build.0 = Debug|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|Any CPU.Build.0 = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|x64.ActiveCfg = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|x64.Build.0 = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|x86.ActiveCfg = Release|Any CPU + {115F4921-B44D-4F69-996B-69796959C99D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -279,6 +293,7 @@ Global {A23E46D2-1610-4AE5-820F-422D34810887} = {BB59D5B5-E7B0-4BF4-8F82-D14431B2799B} {B61AAE66-15CC-40E4-873A-C23E697C3411} = {BB59D5B5-E7B0-4BF4-8F82-D14431B2799B} {4A03840B-9EBE-47E3-89AB-E0914DF21AFB} = {BB59D5B5-E7B0-4BF4-8F82-D14431B2799B} + {115F4921-B44D-4F69-996B-69796959C99D} = {CC3D4B1C-9DE0-448B-8AE7-F3F1F3EC5C3A} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {D2404771-EEC8-45F2-9D71-F3373F6C1495} diff --git a/azure/deploy.yml b/azure/deploy.yml index 4742da3c8..d3460ad6c 100644 --- a/azure/deploy.yml +++ b/azure/deploy.yml @@ -8,6 +8,7 @@ steps: dotnet pack "src\Discord.Net.Providers.WS4Net\Discord.Net.Providers.WS4Net.csproj" --no-restore --no-build -v minimal -c $(buildConfiguration) -o "$(Build.ArtifactStagingDirectory)" /p:BuildNumber=$(buildNumber) /p:IsTagBuild=$(buildTag) dotnet pack "src\Discord.Net.Analyzers\Discord.Net.Analyzers.csproj" --no-restore --no-build -v minimal -c $(buildConfiguration) -o "$(Build.ArtifactStagingDirectory)" /p:BuildNumber=$(buildNumber) /p:IsTagBuild=$(buildTag) dotnet pack "src\Discord.Net.Interactions\Discord.Net.Interactions.csproj" --no-restore --no-build -v minimal -c $(buildConfiguration) -o "$(Build.ArtifactStagingDirectory)" /p:BuildNumber=$(buildNumber) /p:IsTagBuild=$(buildTag) + dotnet pack "experiment\Discord.Net.BuildOverrides\Discord.Net.BuildOverrides.csproj" --no-restore --no-build -v minimal -c $(buildConfiguration) -o "$(Build.ArtifactStagingDirectory)" /p:BuildNumber=$(buildNumber) /p:IsTagBuild=$(buildTag) displayName: Pack projects - task: NuGetCommand@2 diff --git a/docs/faq/build_overrides/what-are-they.md b/docs/faq/build_overrides/what-are-they.md new file mode 100644 index 000000000..f76fd6ddb --- /dev/null +++ b/docs/faq/build_overrides/what-are-they.md @@ -0,0 +1,41 @@ +--- +uid: FAQ.BuildOverrides.WhatAreThey +title: Build Overrides, What are they? +--- + +# Build Overrides + +Build overrides are a way for library developers to override the default behavior of the library on the fly. Adding them to your code is really simple. + +## Installing the package + +The build override package can be installed on nuget [here](TODO) or by using the package manager + +``` +PM> Install-Package Discord.Net.BuildOverrides +``` + +## Adding an override + +```cs +public async Task MainAsync() +{ + // hook into the log function + BuildOverrides.Log += (buildOverride, message) => + { + Console.WriteLine($"{buildOverride.Name}: {message}"); + return Task.CompletedTask; + }; + + // add your overrides + await BuildOverrides.AddOverrideAsync("example-override-name"); +} + +``` + +Overrides are normally built for specific problems, for example if someone is having an issue and we think we might have a fix then we can create a build override for them to test out the fix. + +## Security and Transparency + +Overrides can only be created and updated by library developers, you should only apply an override if a library developer askes you to. +Code for the overrides server and the overrides themselves can be found [here](https://github.com/discord-net/Discord.Net.BuildOverrides). \ No newline at end of file diff --git a/docs/faq/toc.yml b/docs/faq/toc.yml index 97e327aba..b727f5117 100644 --- a/docs/faq/toc.yml +++ b/docs/faq/toc.yml @@ -22,3 +22,5 @@ topicUid: FAQ.TextCommands.General - name: Legacy or Upgrade topicUid: FAQ.Legacy +- name: Build Overrides + topicUid: FAQ.BuildOverrides.WhatAreThey diff --git a/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs b/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs new file mode 100644 index 000000000..fd15e5728 --- /dev/null +++ b/experiment/Discord.Net.BuildOverrides/BuildOverrides.cs @@ -0,0 +1,278 @@ +using Discord.Overrides; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Http; +using System.Reflection; +using System.Runtime.Loader; +using System.Text; +using System.Threading.Tasks; + +namespace Discord +{ + /// + /// Represents an override that can be loaded. + /// + public sealed class Override + { + /// + /// Gets the ID of the override. + /// + public Guid Id { get; internal set; } + + /// + /// Gets the name of the override. + /// + public string Name { get; internal set; } + + /// + /// Gets the description of the override. + /// + public string Description { get; internal set; } + + /// + /// Gets the date this override was created. + /// + public DateTimeOffset CreatedAt { get; internal set; } + + /// + /// Gets the date the override was last modified. + /// + public DateTimeOffset LastUpdated { get; internal set; } + + internal static Override FromJson(string json) + { + var result = new Override(); + + using(var textReader = new StringReader(json)) + using(var reader = new JsonTextReader(textReader)) + { + var obj = JObject.ReadFrom(reader); + result.Id = obj["id"].ToObject(); + result.Name = obj["name"].ToObject(); + result.Description = obj["description"].ToObject(); + result.CreatedAt = obj["created_at"].ToObject(); + result.LastUpdated = obj["last_updated"].ToObject(); + } + + return result; + } + } + + /// + /// Represents a loaded override instance. + /// + public sealed class LoadedOverride + { + /// + /// Gets the aseembly containing the overrides definition. + /// + public Assembly Assembly { get; internal set; } + + /// + /// Gets an instance of the override. + /// + public IOverride Instance { get; internal set; } + + /// + /// Gets the overrides type. + /// + public Type Type { get; internal set; } + } + + public sealed class BuildOverrides + { + /// + /// Fired when an override logs a message. + /// + public static event Func Log + { + add => _logEvents.Add(value); + remove => _logEvents.Remove(value); + + } + + /// + /// Gets a read-only dictionary containing the currently loaded overrides. + /// + public IReadOnlyDictionary> LoadedOverrides + => _loadedOverrides.Select(x => new KeyValuePair> (x.Key, x.Value)).ToDictionary(x => x.Key, x => x.Value); + + private static AssemblyLoadContext _overrideDomain; + private static List> _logEvents = new(); + private static ConcurrentDictionary> _loadedOverrides = new ConcurrentDictionary>(); + + private const string ApiUrl = "https://overrides.discordnet.dev"; + + static BuildOverrides() + { + _overrideDomain = new AssemblyLoadContext("Discord.Net.Overrides.Runtime"); + + _overrideDomain.Resolving += _overrideDomain_Resolving; + } + + /// + /// Gets details about a specific override. + /// + /// + /// Note: This method does not load an override, it simply retrives the info about it. + /// + /// The name of the override to get. + /// + /// A task representing the asynchronous get operation. The tasks result is an + /// if it exists; otherwise . + /// + public static async Task GetOverrideAsync(string name) + { + using (var client = new HttpClient()) + { + var result = await client.GetAsync($"{ApiUrl}/override/{name}"); + + if (result.IsSuccessStatusCode) + { + var content = await result.Content.ReadAsStringAsync(); + + return Override.FromJson(content); + } + else + return null; + } + } + + /// + /// Adds an override to the current Discord.Net instance. + /// + /// + /// The override initialization is non-blocking, any errors that occor within + /// the overrides initialization procedure will be sent in the event. + /// + /// The name of the override to add. + /// + /// A task representing the asynchronous add operaton. The tasks result is a boolean + /// determining if the add operation was successful. + /// + public static async Task AddOverrideAsync(string name) + { + var ovrride = await GetOverrideAsync(name); + + if (ovrride == null) + return false; + + return await AddOverrideAsync(ovrride); + } + + /// + /// Adds an override to the current Discord.Net instance. + /// + /// + /// The override initialization is non-blocking, any errors that occor within + /// the overrides initialization procedure will be sent in the event. + /// + /// The override to add. + /// + /// A task representing the asynchronous add operaton. The tasks result is a boolean + /// determining if the add operation was successful. + /// + public static async Task AddOverrideAsync(Override ovrride) + { + // download it + var ms = new MemoryStream(); + + using (var client = new HttpClient()) + { + var result = await client.GetAsync($"{ApiUrl}/override/download/{ovrride.Id}"); + + if (!result.IsSuccessStatusCode) + return false; + + await (await result.Content.ReadAsStreamAsync()).CopyToAsync(ms); + } + + ms.Position = 0; + + // load the assembly + //var test = Assembly.Load(ms.ToArray()); + var asm = _overrideDomain.LoadFromStream(ms); + + // find out IOverride + var overrides = asm.GetTypes().Where(x => x.GetInterfaces().Any(x => x == typeof(IOverride))); + + List loaded = new(); + + var context = new OverrideContext((m) => HandleLog(ovrride, m), ovrride); + + foreach (var ovr in overrides) + { + var inst = (IOverride)Activator.CreateInstance(ovr); + + inst.RegisterPackageLookupHandler((s) => + { + return GetDependencyAsync(ovrride.Id, s); + }); + + _ = Task.Run(async () => + { + try + { + await inst.InitializeAsync(context); + } + catch (Exception x) + { + HandleLog(ovrride, $"Failed to initialize build override: {x}"); + } + }); + + loaded.Add(new LoadedOverride() + { + Assembly = asm, + Instance = inst, + Type = ovr + }); + } + + return _loadedOverrides.AddOrUpdate(ovrride, loaded, (_, __) => loaded) != null; + } + + internal static void HandleLog(Override ovr, string msg) + { + _ = Task.Run(async () => + { + foreach (var item in _logEvents) + { + await item.Invoke(ovr, msg).ConfigureAwait(false); + } + }); + } + + private static Assembly _overrideDomain_Resolving(AssemblyLoadContext arg1, AssemblyName arg2) + { + // resolve the override id + var v = _loadedOverrides.FirstOrDefault(x => x.Value.Any(x => x.Assembly.FullName == arg1.Assemblies.FirstOrDefault().FullName)); + + return GetDependencyAsync(v.Key.Id, $"{arg2}").GetAwaiter().GetResult(); + } + + private static async Task GetDependencyAsync(Guid id, string name) + { + using(var client = new HttpClient()) + { + var result = await client.PostAsync($"{ApiUrl}/override/{id}/dependency", new StringContent($"{{ \"info\": \"{name}\"}}", Encoding.UTF8, "application/json")); + + if (!result.IsSuccessStatusCode) + throw new Exception("Failed to get dependency"); + + using(var ms = new MemoryStream()) + { + var innerStream = await result.Content.ReadAsStreamAsync(); + await innerStream.CopyToAsync(ms); + ms.Position = 0; + return _overrideDomain.LoadFromStream(ms); + } + } + } + } +} diff --git a/experiment/Discord.Net.BuildOverrides/Discord.Net.BuildOverrides.csproj b/experiment/Discord.Net.BuildOverrides/Discord.Net.BuildOverrides.csproj new file mode 100644 index 000000000..25b1c40b0 --- /dev/null +++ b/experiment/Discord.Net.BuildOverrides/Discord.Net.BuildOverrides.csproj @@ -0,0 +1,20 @@ + + + + 9.0 + Discord.Net.BuildOverrides + Discord.BuildOverrides + A Discord.Net extension adding a way to add build overrides for testing. + net6.0;net5.0; + net6.0;net5.0; + + + + + + + + + + + diff --git a/experiment/Discord.Net.BuildOverrides/IOverride.cs b/experiment/Discord.Net.BuildOverrides/IOverride.cs new file mode 100644 index 000000000..17327ae2c --- /dev/null +++ b/experiment/Discord.Net.BuildOverrides/IOverride.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Discord.Overrides +{ + /// + /// Represents a generic build override for Discord.Net + /// + public interface IOverride + { + /// + /// Initializes the override. + /// + /// + /// This method is called by the class + /// and should not be called externally from it. + /// + /// Context used by an override to initialize. + /// + /// A task representing the asynchronous initialization operation. + /// + Task InitializeAsync(OverrideContext context); + + /// + /// Registers a callback to load a dependency for this override. + /// + /// The callback to load an external dependency. + void RegisterPackageLookupHandler(Func> func); + } +} diff --git a/experiment/Discord.Net.BuildOverrides/OverrideContext.cs b/experiment/Discord.Net.BuildOverrides/OverrideContext.cs new file mode 100644 index 000000000..1e88be74a --- /dev/null +++ b/experiment/Discord.Net.BuildOverrides/OverrideContext.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Discord.Overrides +{ + /// + /// Represents context thats passed to an override in the initialization step. + /// + public sealed class OverrideContext + { + /// + /// A callback used to log messages. + /// + public Action Log { get; private set; } + + /// + /// The info about the override. + /// + public Override Info { get; private set; } + + internal OverrideContext(Action log, Override info) + { + Log = log; + Info = info; + } + } +} diff --git a/overrides/Discord.Net.BuildOverrides b/overrides/Discord.Net.BuildOverrides new file mode 160000 index 000000000..9b2be5597 --- /dev/null +++ b/overrides/Discord.Net.BuildOverrides @@ -0,0 +1 @@ +Subproject commit 9b2be5597468329090015fa1b2775815b20be440 From 0439437a65e5a4ef4087c2a5115824b7908dfa99 Mon Sep 17 00:00:00 2001 From: TricolorHen061 <55330531+TricolorHen061@users.noreply.github.com> Date: Mon, 4 Apr 2022 14:20:09 -0700 Subject: [PATCH 23/25] Fix small typo in modal example (#2216) --- docs/guides/int_framework/samples/intro/modal.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/guides/int_framework/samples/intro/modal.cs b/docs/guides/int_framework/samples/intro/modal.cs index af72fe04e..65cc81abf 100644 --- a/docs/guides/int_framework/samples/intro/modal.cs +++ b/docs/guides/int_framework/samples/intro/modal.cs @@ -20,7 +20,7 @@ public class FoodModal : IModal // Responds to the modal. [ModalInteraction("food_menu")] -public async Task ModalResponce(FoodModal modal) +public async Task ModalResponse(FoodModal modal) { // Build the message to send. string message = "hey @everyone, I just learned " + From e38104bb3263d837b89c1449e9ef39f1e25a7e15 Mon Sep 17 00:00:00 2001 From: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Date: Mon, 4 Apr 2022 23:21:11 +0200 Subject: [PATCH 24/25] feature: Make bidirectional formatting optional (#2204) * Init * Clearing up comment on config entry. * Update user entities to remove storage of the setting Co-authored-by: Quin Lynch --- src/Discord.Net.Core/DiscordConfig.cs | 10 ++++++++++ src/Discord.Net.Core/Format.cs | 9 ++++++--- src/Discord.Net.Rest/BaseDiscordClient.cs | 2 ++ src/Discord.Net.Rest/Entities/Users/RestUser.cs | 6 ++++-- src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs | 4 ++-- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Discord.Net.Core/DiscordConfig.cs b/src/Discord.Net.Core/DiscordConfig.cs index 34bfc5e62..006a1ca17 100644 --- a/src/Discord.Net.Core/DiscordConfig.cs +++ b/src/Discord.Net.Core/DiscordConfig.cs @@ -187,5 +187,15 @@ namespace Discord /// This will still require a stable clock on your system. /// public bool UseInteractionSnowflakeDate { get; set; } = true; + + /// + /// Gets or sets if the Rest/Socket user override formats the string in respect to bidirectional unicode. + /// + /// + /// By default, the returned value will be "?Discord?#1234", to work with bidirectional usernames. + ///
+ /// If set to , this value will be "Discord#1234". + ///
+ public bool FormatUsersInBidirectionalUnicode { get; set; } = true; } } diff --git a/src/Discord.Net.Core/Format.cs b/src/Discord.Net.Core/Format.cs index a5951aa73..dc2a06540 100644 --- a/src/Discord.Net.Core/Format.cs +++ b/src/Discord.Net.Core/Format.cs @@ -107,13 +107,16 @@ namespace Discord } /// - /// Formats a user's username + discriminator while maintaining bidirectional unicode + /// Formats a user's username + discriminator. /// + /// To format the string in bidirectional unicode or not /// The user whos username and discriminator to format /// The username + discriminator - public static string UsernameAndDiscriminator(IUser user) + public static string UsernameAndDiscriminator(IUser user, bool doBidirectional) { - return $"\u2066{user.Username}\u2069#{user.Discriminator}"; + return doBidirectional + ? $"\u2066{user.Username}\u2069#{user.Discriminator}" + : $"{user.Username}#{user.Discriminator}"; } } } diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs index 2bf08e3c7..75f477c7c 100644 --- a/src/Discord.Net.Rest/BaseDiscordClient.cs +++ b/src/Discord.Net.Rest/BaseDiscordClient.cs @@ -36,6 +36,7 @@ namespace Discord.Rest /// public TokenType TokenType => ApiClient.AuthTokenType; internal bool UseInteractionSnowflakeDate { get; private set; } + internal bool FormatUsersInBidirectionalUnicode { get; private set; } /// Creates a new REST-only Discord client. internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client) @@ -49,6 +50,7 @@ namespace Discord.Rest _isFirstLogin = config.DisplayInitialLog; UseInteractionSnowflakeDate = config.UseInteractionSnowflakeDate; + FormatUsersInBidirectionalUnicode = config.FormatUsersInBidirectionalUnicode; ApiClient.RequestQueue.RateLimitTriggered += async (id, info, endpoint) => { diff --git a/src/Discord.Net.Rest/Entities/Users/RestUser.cs b/src/Discord.Net.Rest/Entities/Users/RestUser.cs index 70f990fe7..dfdb53815 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestUser.cs @@ -129,8 +129,10 @@ namespace Discord.Rest /// /// A string that resolves to Username#Discriminator of the user. /// - public override string ToString() => Format.UsernameAndDiscriminator(this); - private string DebuggerDisplay => $"{Format.UsernameAndDiscriminator(this)} ({Id}{(IsBot ? ", Bot" : "")})"; + public override string ToString() + => Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode); + + private string DebuggerDisplay => $"{Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode)} ({Id}{(IsBot ? ", Bot" : "")})"; #endregion #region IUser diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs index 35121d666..d70e61739 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs @@ -117,8 +117,8 @@ namespace Discord.WebSocket /// /// The full name of the user. /// - public override string ToString() => Format.UsernameAndDiscriminator(this); - private string DebuggerDisplay => $"{Format.UsernameAndDiscriminator(this)} ({Id}{(IsBot ? ", Bot" : "")})"; + public override string ToString() => Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode); + private string DebuggerDisplay => $"{Format.UsernameAndDiscriminator(this, Discord.FormatUsersInBidirectionalUnicode)} ({Id}{(IsBot ? ", Bot" : "")})"; internal SocketUser Clone() => MemberwiseClone() as SocketUser; } } From bfd0d9bede3993ab2502c54f0f5dfe528802e494 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Mon, 4 Apr 2022 19:17:18 -0300 Subject: [PATCH 25/25] fix: GuildMemberUpdated cacheable before entity incorrect (#2225) --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index b2da962ab..aaef4656a 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1311,7 +1311,7 @@ namespace Discord.WebSocket else { user = guild.AddOrUpdateUser(data); - var cacheableBefore = new Cacheable(user, user.Id, true, () => null); + var cacheableBefore = new Cacheable(null, user.Id, false, () => null); await TimedInvokeAsync(_guildMemberUpdatedEvent, nameof(GuildMemberUpdated), cacheableBefore, user).ConfigureAwait(false); } }