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 5.7 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Playwo's [InteractivityAddon](https://github.com/Playwo/Discord.InteractivityAddon). The reason is that his package depends on the base discord.net lib, you can get around this by cloning his repo and building it with discord.net labs instead of discord.net.
  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. The main branch we pull off of to introduce new features into, the dev branch is the same as Discord.Nets dev branch
  15. ### Interactions
  16. This branch is for anything todo with Discord Interactions, such as [Slash commands](https://discord.com/developers/docs/interactions/slash-commands) and [Message Components](https://discord.com/developers/docs/interactions/message-components). This branch is stable enough to use but does not contain all the features of interactions.
  17. ### SlashCommandService
  18. This branch is on pause and does not work currently, Once everything is stable with the Interaction branch we will continue working on a slash command service for it.
  19. ### web/SlashCommandService
  20. webmilio's spin on the SlashCommandService branch, again the state of this is unknown.
  21. ## Listening for interactions
  22. ```cs
  23. // Subscribe to the InteractionCreated event
  24. client.InteractionCreated += Client_InteractionCreated;
  25. ...
  26. private async Task Client_InteractionCreated(SocketInteraction arg)
  27. {
  28. switch (arg.Type) // We want to check the type of this interaction
  29. {
  30. //Slash commands
  31. case InteractionType.ApplicationCommand:
  32. await MySlashCommandHandler(arg);
  33. break;
  34. //Button clicks/selection dropdowns
  35. case InteractionType.MessageComponent:
  36. await MyMessageComponentHandler(arg);
  37. break;
  38. //Unused
  39. case InteractionType.Ping:
  40. break;
  41. //Unknown/Unsupported
  42. default:
  43. Console.WriteLine("Unsupported interaction type: " + arg.Type);
  44. break;
  45. }
  46. }
  47. ```
  48. ### Handling button clicks and selection dropdowns
  49. ```cs
  50. private async Task MyMessageComponentHandler(SocketInteraction arg)
  51. {
  52. // Parse the arg
  53. var parsedArg = (SocketMessageComponent) arg;
  54. // Get the custom ID
  55. var customId = parsedArg.Data.CustomId;
  56. // Get the user
  57. var user = (SocketGuildUser) arg.User;
  58. // Get the guild
  59. var guild = user.Guild;
  60. // Respond with the update message response type. This edits the original message if you have set AlwaysAcknowledgeInteractions to false.
  61. // You can also use "ephemeral" so that only the original user of the interaction sees the message
  62. await parsedArg.RespondAsync($"Clicked {parsedArg.Data.CustomId}!", type: InteractionResponseType.UpdateMessage, ephemeral: true);
  63. // You can also followup with a second message
  64. await parsedArg.FollowupAsync($"Clicked {parsedArg.Data.CustomId}!", type: InteractionResponseType.ChannelMessageWithSource, ephemeral: true);
  65. //If you are using selection dropdowns, you can get the selected label and values using these:
  66. var selectedLabel = ((SelectMenu) parsedArg.Message.Components.First().Components.First()).Options.FirstOrDefault(x => x.Value == parsedArg.Data.Values.FirstOrDefault())?.Label;
  67. var selectedValue = parsedArg.Data.Values.First();
  68. }
  69. ```
  70. > 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.
  71. ### Sending messages with buttons
  72. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  73. ```cs
  74. var builder = new ComponentBuilder().WithButton("Hello!", customId: "id_1", ButtonStyle.Primary, row: 0);
  75. await Context.Channel.SendMessageAsync("Test buttons!", component: builder.Build());
  76. ```
  77. ### Sending messages with selection dropdowns
  78. Theres a new field in all `SendMessageAsync` functions that takes in a `MessageComponent`, you can use it like so:
  79. ```cs
  80. var builder = new ComponentBuilder()
  81. .WithSelectMenu(new SelectMenuBuilder()
  82. .WithCustomId("id_2")
  83. .WithLabel("Select menu!")
  84. .WithPlaceholder("This is a placeholder")
  85. .WithOptions(new List<SelectMenuOptionBuilder>()
  86. {
  87. new SelectMenuOptionBuilder()
  88. .WithLabel("Option A")
  89. .WithEmote(Emote.Parse("<:evanpog:810017136814194698>"))
  90. .WithDescription("Evan pog champ")
  91. .WithValue("value1"),
  92. new SelectMenuOptionBuilder()
  93. .WithLabel("Option B")
  94. .WithDescription("Option B is poggers")
  95. .WithValue("value2")
  96. }));
  97. await Context.Channel.SendMessageAsync("Test selection!", component: builder.Build());
  98. ```
  99. > 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.
  100. ## Slash commands
  101. Slash command example how to's can be found [here](https://github.com/Discord-Net-Labs/Discord.Net-Labs/tree/Interactions/docs/guides/slash-commands). If you want to read some code using slash commands, you can do that [here](https://github.com/quinchs/SwissbotCore/blob/master/SwissbotCore/Handlers/AutoMod/Censor.cs)