You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Discord.Net Labs
  2. [![NuGet](https://img.shields.io/nuget/vpre/Discord.Net.Labs.svg?maxAge=2592000?style=plastic)](https://www.nuget.org/packages/Discord.Net.Labs)
  3. [![Discord](https://discord.com/api/guilds/848176216011046962/widget.png)](https://discord.gg/dvSfUTet3K)
  4. This repo is a custom fork of Discord.Net that introduces the newest features of discord for testing and experimenting. Nothing here is guaranteed to work but you are more than welcome to submit bugs in the issues tabs
  5. ## Known issues
  6. Labs will not work with normal package of Playwo's [InteractivityAddon](https://www.nuget.org/packages/Discord.InteractivityAddon). The reason is that his package depends on the base discord.net lib. You can instead use the [InteractivityAddon.Labs](https://www.nuget.org/packages/Discord.InteractivityAddon.Labs) package which implements some of the features added in Discord.Net-Labs.
  7. ## How to use
  8. Setting up labs in your project is really simple, here's how to do it:
  9. 1) Remove Discord.Net from your project
  10. 2) Add Discord.Net Labs nuget to your project
  11. 3) Enjoy!
  12. ## Branches
  13. ### Dev
  14. This branch is kept up to date with dnets dev branch. we pull of it to ensure that labs will work with pre existing dnet code.
  15. ### release/2.x
  16. This branch is what will be pushed to nuget, sometimes its not up to date as we wait for other features to be finished.
  17. ### old/SlashCommandService
  18. This branch is on pause and does not work currently, There is a pull request open to implement a working version of a slash command service. It can be found [here](https://github.com/Discord-Net-Labs/Discord.Net-Labs/pull/52)
  19. ### feature/xyz
  20. These branches are features for new things, you are more than welcome to clone them and give feedback in the discord server or issues tab.
  21. ## Listening for Interactions
  22. Interaction docs can be found [here](https://github.com/Discord-Net-Labs/Discord.Net-Labs/tree/release/3.x/docs/guides/interactions). They are much more in depth than this readme.
  23. ```cs
  24. // Subscribe to the InteractionCreated event
  25. client.InteractionCreated += Client_InteractionCreated;
  26. ...
  27. private async Task Client_InteractionCreated(SocketInteraction interaction)
  28. {
  29. // Checking the type of this interaction
  30. switch (interaction)
  31. {
  32. // Slash commands
  33. case SocketSlashCommand commandInteraction:
  34. await MySlashCommandHandler(commandInteraction);
  35. break;
  36. // Button clicks/selection dropdowns
  37. case SocketMessageComponent componentInteraction:
  38. await MyMessageComponentHandler(componentInteraction);
  39. break;
  40. // Unused or Unknown/Unsupported
  41. default:
  42. break;
  43. }
  44. }
  45. ```
  46. ### Simple handling slash commands
  47. ```cs
  48. private async Task MySlashCommandHandler(SocketSlashCommand interaction)
  49. {
  50. // Checking command name
  51. if (interaction.Data.Name == "ping")
  52. {
  53. // Respond to interaction with message.
  54. // You can also use "ephemeral" so that only the original user of the interaction sees the message
  55. await interaction.RespondAsync($"Pong!", ephemeral: true);
  56. // Also you can followup with a additional messages, which also can be "ephemeral"
  57. await interaction.FollowupAsync($"PongPong!", ephemeral: true);
  58. }
  59. }
  60. ```
  61. ### Simple handling button clicks and selection dropdowns
  62. ```cs
  63. private async Task MyMessageComponentHandler(SocketMessageComponent interaction)
  64. {
  65. // Get the custom ID
  66. var customId = interaction.Data.CustomId;
  67. // Get the user
  68. var user = (SocketGuildUser) interaction.User;
  69. // Get the guild
  70. var guild = user.Guild;
  71. // Respond with the update message. This edits the message which this component resides.
  72. await interaction.UpdateAsync(msgProps => msgProps.Content = $"Clicked {interaction.Data.CustomId}!");
  73. // Also you can followup with a additional messages
  74. await interaction.FollowupAsync($"Clicked {interaction.Data.CustomId}!", ephemeral: true);
  75. // If you are using selection dropdowns, you can get the selected label and values using these
  76. var selectedLabel = ((SelectMenu) interaction.Message.Components.First().Components.First()).Options.FirstOrDefault(x => x.Value == interaction.Data.Values.FirstOrDefault())?.Label;
  77. var selectedValue = interaction.Data.Values.First();
  78. }
  79. ```
  80. > Note: The example above assumes that the selection dropdown is expecting only 1 returned value, if you configured your dropdown for multiple values, you'll need to modify the code slightly.
  81. ### Sending messages with buttons
  82. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  83. ```cs
  84. var builder = new ComponentBuilder().WithButton("Hello!", customId: "id_1", ButtonStyle.Primary, row: 0);
  85. await Context.Channel.SendMessageAsync("Test buttons!", component: builder.Build());
  86. ```
  87. ### Sending messages with selection dropdowns
  88. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  89. ```cs
  90. var builder = new ComponentBuilder()
  91. .WithSelectMenu(new SelectMenuBuilder()
  92. .WithCustomId("id_2")
  93. .WithPlaceholder("This is a placeholder")
  94. .AddOption(
  95. label: "Option",
  96. value: "value1",
  97. description: "Evan pog champ",
  98. emote: Emote.Parse("<:evanpog:810017136814194698>")
  99. )
  100. .AddOption("Option B", "value2", "Option B is poggers")
  101. );
  102. await Context.Channel.SendMessageAsync("Test selection!", component: builder.Build());
  103. ```
  104. > Note: You can only have 5 buttons per row and 5 rows per message. If a row contains a selection dropdown it cannot contain any buttons.
  105. ## Slash Commands & Context Menu Commands
  106. Slash command & Context command examples and how to's can be found [here](https://github.com/Discord-Net-Labs/Discord.Net-Labs/tree/release/3.x/docs/guides/interactions/application-commands).
  107. ## Message Components
  108. Message components (buttons, menus, etc) examples and how to's can be found [here](https://github.com/Discord-Net-Labs/Discord.Net-Labs/tree/release/3.x/docs/guides/interactions/message-components)