You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 30, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+1-323Lines changed: 1 addition & 323 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,326 +11,4 @@ Join the [Discord server](https://discord.gg/2ZhXXVJYhU) for any questions, help
11
11
12
12
# Documentation
13
13
14
-
DSharpPlus doesn't currently have a slash command framework. You can use this library to implement slash commands and context menus into your bot.
15
-
16
-
I have done my best to make this as similar to CommandsNext as possible to make it a smooth experience. However, the library does not support registering or editing commands at runtime. While you can make commands at runtime using the methods on the client, if you have a command class registered for that guild/globally if you're making global commands, it will be overwritten (therefore probably deleted) on the next startup due to the limitations of the bulk overwrite endpoint.
17
-
18
-
Now, on to the actual guide:
19
-
## Installing
20
-
Simply search for `IDoEverything.DSharpPlus.SlashCommands` and install the latest version. If you're using command line:
The current version of the library depends on the DSharpPlus nightly version. If you're using the stable nuget version, [update to the nightly version](https://dsharpplus.github.io/articles/misc/nightly_builds.html).
27
-
# Important: Authorizing your bot
28
-
29
-
For a bot to make slash commands in a server, it must be authorized with the applications.commands scope as well. In the OAuth2 section of the developer portal, you can check the applications.commands box to generate an invite link. You can check the bot box as well to generate a link that authorizes both. If a bot is already authorized with the bot scope, you can still authorize with just the applications.commands scope without having to kick out the bot.
30
-
31
-
If your bot isn't properly authorized, a 403 exception will be thrown on startup.
32
-
33
-
# Setup
34
-
35
-
Add the using reference to your bot class:
36
-
```cs
37
-
usingDSharpPlus.SlashCommands;
38
-
```
39
-
40
-
You can then register a `SlashCommandsExtension` on your `DiscordClient`, similar to how you register a `CommandsNextExtension`
41
-
42
-
```cs
43
-
varslash=discord.UseSlashCommands();
44
-
```
45
-
46
-
## Making a command class
47
-
Similar to CommandsNext, you can make a module for slash commands and make it inherit from `ApplicationCommandModule`
You have to then register it with your `SlashCommandsExtension`.
55
-
56
-
Slash commands can be registered either globally or for a certain guild. However, if you try to register them globally, they can take up to an hour to cache across all guilds. So, it is recommended that you only register them for a certain guild for testing, and only register them globally once they're ready to be used.
57
-
58
-
To register your command class,
59
-
```cs
60
-
//To register them for a single server, recommended for testing
61
-
slash.RegisterCommands<SlashCommands>(guild_id);
62
-
63
-
//To register them globally, once you're confident that they're ready to be used by everyone
64
-
slash.RegisterCommands<SlashCommands>();
65
-
```
66
-
*Make sure that you register them before your `ConnectAsync`*
67
-
68
-
## Making Slash Commands!
69
-
On to the exciting part.
70
-
71
-
Slash command methods must be `Task`s and have the `SlashCommand` attribute. The first argument for the method must be an `InteractionContext`. Let's make a simple slash command:
To make a response, you must run `CreateResponseAsync` on your `InteractionContext`. `CreateResponseAsync` takes two arguments. The first is a [`InteractionResponseType`](https://dsharpplus.github.io/api/DSharpPlus.InteractionResponseType.html):
81
-
*`DeferredChannelMessageWithSource` - Acknowledges the interaction, doesn't require any content.
82
-
*`ChannelMessageWithSource` - Sends a message to the channel, requires you to specify some data to send.
83
-
84
-
An interaction expires in 3 seconds unless you make a response. If the code you execute before making a response has the potential to take more than 3 seconds, you should first create a `DeferredChannelMessageWithSource` response, and then edit it after your code executes.
85
-
86
-
The second argument is a type of [`DiscordInteractionResponseBuilder`](https://dsharpplus.github.io/api/DSharpPlus.Entities.DiscordInteractionResponseBuilder.html). It functions similarly to the DiscordMessageBuilder, except you cannot send files, and you can have multiple embeds.
87
-
88
-
If you want to send a file, you'll have to edit the response.
89
-
90
-
A simple response would be like:
91
-
```cs
92
-
[SlashCommand("test", "A slash command made to test the DSharpPlusSlashCommands library!")]
//Some time consuming task like a database call or a complex operation
106
-
107
-
awaitctx.EditResponseAsync(newDiscordWebhookBuilder().WithContent("Thanks for waiting!"));
108
-
}
109
-
```
110
-
You can also override `BeforeExecutionAsync` and `AfterExecutionAsync` to run code before and after all the commands in a module. This does not apply to groups, you have the override them individually for the group's class.
111
-
`BeforeExecutionAsync` can also be used to prevent the command from running.
112
-
113
-
### Arguments
114
-
If you want the user to be able to give more data to the command, you can add some arguments.
115
-
116
-
Arguments must have the `Option` attribute, and can be of type:
117
-
*`string`
118
-
*`long` or `long?`
119
-
*`bool` or `bool?`
120
-
*`double` or `double?`
121
-
*`DiscordUser` - This can be cast to `DiscordMember` if the command is run in a guild
122
-
*`DiscordChannel`
123
-
*`DiscordRole`
124
-
*`SnowflakeObject` - This can accept both a user and a role; you can cast it `DiscordUser`, `DiscordMember` or `DiscordRole` to get the actual object
125
-
*`Enum` - This can used for choices through an enum; read further
126
-
127
-
If you want to make them optional, you can assign a default value.
128
-
129
-
You can also predefine some choices for the option. Choices only work for `string`, `long` or `double` arguments. THere are several ways to use them:
130
-
1. Using the `Choice` attribute. You can add multiple attributes to add multiple choices.
131
-
2. You can define choices using enums. See the example below.
132
-
3. You can use a `ChoiceProvider` to run code to get the choices from a database or similar. See the example below.
133
-
134
-
(second and third method contributed by @Epictek)
135
-
136
-
Some examples:
137
-
```cs
138
-
//Attribute choices
139
-
[SlashCommand("ban", "Bans a user")]
140
-
publicasyncTaskBan(InteractionContextctx, [Option("user", "User to ban")] DiscordUseruser,
141
-
[Choice("None", 0)]
142
-
[Choice("1 Day", 1)]
143
-
[Choice("1 Week", 7)]
144
-
[Option("deletedays", "Number of days of message history to delete")] longdeleteDays=0)
You can have slash commands in groups. Their structure is explained [here](https://discord.com/developers/docs/interactions/slash-commands#nested-subcommands-and-groups). You can simply mark your command class with the `[SlashCommandGroup]` attribute.
Responding works exactly the same as slash commands. You cannot define any arguments.
242
-
243
-
### Pre-execution checks
244
-
You can define some custom attributes that function as pre-execution checks, working very similarly to `CommandsNext`. Simply create an attribute that inherits `SlashCheckBaseAttribute` for slash commands, and `ContextMenuCheckBaseAttribute` for context menus and override the methods.
245
-
246
-
There are also some built in ones for slash commands, the same ones as on `CommandsNext` but prefix with `Slash` - for example the `SlashRequirePermissionsAttribute`
awaite.Context.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, newDiscordInteractionResponseBuilder().WithContent($"Only <@{att.Id}> can run this command!"));
You'll have to foreach over it to register events.
328
-
329
-
### Module Lifespans
330
-
Youcanspecifyamodule's lifespan by applying the `SlashModuleLifespan` attribute on it. Modules are transient by default.
331
-
332
-
# Issues and contributing
333
-
Ifyoufindanyissuesorbugs, youshouldjointhediscordserveranddiscussit. Ifit's an actual bug, you can create an [issue](https://github.com/IDoEverything/DSharpPlus.SlashCommands/issues). If you would like to contribute or make changes, feel free to open a [pull request](https://github.com/IDoEverything/DSharpPlus.SlashCommands/pulls).
0 commit comments