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.2 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
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. If this projects benefits you (*and you're financially stable*) consider donating or becoming a sponsor as it really helps out!
  6. ## Known issues
  7. 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.
  8. ## How to use
  9. Setting up labs in your project is really simple, here's how to do it:
  10. 1) Remove Discord.Net from your project
  11. 2) Add Discord.Net Labs nuget to your project
  12. 3) Enjoy!
  13. ## Branches
  14. ### Dev
  15. 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.
  16. ### release/3.x
  17. This branch is what will be pushed to nuget, sometimes its not up to date as we wait for other features to be finished.
  18. ### old/SlashCommandService
  19. 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)
  20. ### feature/xyz
  21. 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.
  22. ## Listening for Interactions
  23. 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.
  24. ```cs
  25. // Subscribe to the InteractionCreated event
  26. client.InteractionCreated += Client_InteractionCreated;
  27. ...
  28. private async Task Client_InteractionCreated(SocketInteraction interaction)
  29. {
  30. // Checking the type of this interaction
  31. switch (interaction)
  32. {
  33. // Slash commands
  34. case SocketSlashCommand commandInteraction:
  35. await MySlashCommandHandler(commandInteraction);
  36. break;
  37. // Button clicks/selection dropdowns
  38. case SocketMessageComponent componentInteraction:
  39. await MyMessageComponentHandler(componentInteraction);
  40. break;
  41. // Unused or Unknown/Unsupported
  42. default:
  43. break;
  44. }
  45. }
  46. ```
  47. ### Simple handling slash commands
  48. ```cs
  49. private async Task MySlashCommandHandler(SocketSlashCommand interaction)
  50. {
  51. // Checking command name
  52. if (interaction.Data.Name == "ping")
  53. {
  54. // Respond to interaction with message.
  55. // You can also use "ephemeral" so that only the original user of the interaction sees the message
  56. await interaction.RespondAsync($"Pong!", ephemeral: true);
  57. // Also you can followup with a additional messages, which also can be "ephemeral"
  58. await interaction.FollowupAsync($"PongPong!", ephemeral: true);
  59. }
  60. }
  61. ```
  62. ### Simple handling button clicks and selection dropdowns
  63. ```cs
  64. private async Task MyMessageComponentHandler(SocketMessageComponent interaction)
  65. {
  66. // Get the custom ID
  67. var customId = interaction.Data.CustomId;
  68. // Get the user
  69. var user = (SocketGuildUser) interaction.User;
  70. // Get the guild
  71. var guild = user.Guild;
  72. // Respond with the update message. This edits the message which this component resides.
  73. await interaction.UpdateAsync(msgProps => msgProps.Content = $"Clicked {interaction.Data.CustomId}!");
  74. // Also you can followup with a additional messages
  75. await interaction.FollowupAsync($"Clicked {interaction.Data.CustomId}!", ephemeral: true);
  76. // If you are using selection dropdowns, you can get the selected label and values using these
  77. var selectedLabel = ((SelectMenu) interaction.Message.Components.First().Components.First()).Options.FirstOrDefault(x => x.Value == interaction.Data.Values.FirstOrDefault())?.Label;
  78. var selectedValue = interaction.Data.Values.First();
  79. }
  80. ```
  81. > 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.
  82. ### Sending messages with buttons
  83. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  84. ```cs
  85. var builder = new ComponentBuilder().WithButton("Hello!", customId: "id_1", ButtonStyle.Primary, row: 0);
  86. await Context.Channel.SendMessageAsync("Test buttons!", component: builder.Build());
  87. ```
  88. ### Sending messages with selection dropdowns
  89. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  90. ```cs
  91. var builder = new ComponentBuilder()
  92. .WithSelectMenu(new SelectMenuBuilder()
  93. .WithCustomId("id_2")
  94. .WithPlaceholder("This is a placeholder")
  95. .AddOption(
  96. label: "Option",
  97. value: "value1",
  98. description: "Evan pog champ",
  99. emote: Emote.Parse("<:evanpog:810017136814194698>")
  100. )
  101. .AddOption("Option B", "value2", "Option B is poggers")
  102. );
  103. await Context.Channel.SendMessageAsync("Test selection!", component: builder.Build());
  104. ```
  105. > 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.
  106. ## Slash Commands & Context Menu Commands
  107. 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).
  108. ## Message Components
  109. 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)