-
-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slash command service #1733
Closed
Closed
Slash command service #1733
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d35ba91
Rest features for ApplicationCommands
quinchs 27efb7d
Fixed couple of unfinished properties
quinchs fae3fbe
Worked on more rest routes for responding to application commands. Wo…
quinchs c322824
Merge branch 'dev' into Interactions
quinchs 670bd92
Working version of Interactions. All public classes/members now have …
quinchs 07964ac
Merge branch 'Interactions' of https://github.com/quinchs/Discord.Net…
quinchs a0f9646
Get, modify, and delete Guild/Global commands,
quinchs 09f6f34
Added APPLICATION_COMMAND_DELETE, APPLICATION_COMMAND_UPDATE, and APP…
quinchs 18ffc40
Create application-commands.md
quinchs 21c2a7f
SlashCommandBuilder added
quinchs ff08f34
Comment updates and explicit conversions for SocketInteractionDataOpt…
quinchs c5bba76
Merge remote-tracking branch 'origin/SlashCommandService' into SlashC…
quinchs f3c07fd
Merge remote-tracking branch 'origin/Interactions' into SlashCommandS…
quinchs f4a8b21
Worked on Slash command service, getting ready for big code grind
quinchs 16f6a4c
Fixed build errors and started an example project for development pur…
george-cosma f4b3214
Implemented the most basic form of the command service. Does not yet …
george-cosma 4223fa8
Implemented basic parameter recognition and passing them to the method.
george-cosma 50a88e0
Implemented SubCommands and SubCommandGroups properly.
george-cosma c76fdec
Implemented Global Attribute and added a way to register all commands…
george-cosma 6ba456a
Implemented [Choice( [] , [] )] and [Required] Attributes.
george-cosma 4019d51
Implemented ParameterName for custom names, implemented a public Buil…
george-cosma af9db9c
Merge pull request #1 from SlenderPlays/SlashCommandServiceV2
george-cosma 3e9b027
Merge pull request #1 from SlenderPlays/SlashCommandService
quinchs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
491 changes: 491 additions & 0 deletions
491
Discord.Net.SlashCommands/Builders/SlashCommandBuilder.cs
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
Discord.Net.SlashCommands/Discord.Net.SlashCommands.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>Discord.Net.SlashCommands</AssemblyName> | ||
<RootNamespace>Discord.SlashCommands</RootNamespace> | ||
<Description>A Discord.Net extension adding support for slash commands.</Description> | ||
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net461;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard2.0;netstandard2.1</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\src\Discord.Net.Core\Discord.Net.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.SlashCommands; | ||
using Discord.WebSocket; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace SlashCommandsExample | ||
{ | ||
class DiscordClient | ||
{ | ||
public static DiscordSocketClient socketClient { get; set; } = new DiscordSocketClient(); | ||
public static SlashCommandService _commands { get; set; } | ||
public static IServiceProvider _services { get; set; } | ||
|
||
private string botToken = "<YOUR TOKEN HERE>"; | ||
|
||
public DiscordClient() | ||
{ | ||
_commands = new SlashCommandService(); | ||
_services = new ServiceCollection() | ||
.AddSingleton(socketClient) | ||
.AddSingleton(_commands) | ||
.BuildServiceProvider(); | ||
|
||
socketClient.Log += SocketClient_Log; | ||
_commands.Log += SocketClient_Log; | ||
socketClient.InteractionCreated += InteractionHandler; | ||
socketClient.Ready += RegisterCommand; | ||
|
||
// This is for dev purposes. | ||
// To avoid the situation in which you accidentally push your bot token to upstream, you can use | ||
// EnviromentVariables to store your key. | ||
botToken = Environment.GetEnvironmentVariable("DiscordSlashCommandsBotToken", EnvironmentVariableTarget.User); | ||
// Uncomment the next line of code to set the environment variable. | ||
// ------------------------------------------------------------------ | ||
// | WARNING! | | ||
// | | | ||
// | MAKE SURE TO DELETE YOUR TOKEN AFTER YOU HAVE SET THE VARIABLE | | ||
// | | | ||
// ------------------------------------------------------------------ | ||
|
||
//Environment.SetEnvironmentVariable("DiscordSlashCommandsBotToken", | ||
// "[YOUR TOKEN GOES HERE DELETE & COMMENT AFTER USE]", | ||
// EnvironmentVariableTarget.User); | ||
} | ||
|
||
public async Task RegisterCommand() | ||
{ | ||
// Use this to manually register a command for testing. | ||
return; | ||
await socketClient.Rest.CreateGuildCommand(new SlashCommandCreationProperties() | ||
{ | ||
Name = "root", | ||
Description = "Root Command", | ||
Options = new List<ApplicationCommandOptionProperties>() | ||
{ | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "usr", | ||
Description = "User Folder", | ||
Type = ApplicationCommandOptionType.SubCommandGroup, | ||
Options = new List<ApplicationCommandOptionProperties>() | ||
{ | ||
// This doesn't work. This is good! | ||
//new ApplicationCommandOptionProperties() | ||
//{ | ||
// Name = "strstr", | ||
// Description = "Some random string I guess.", | ||
// Type = ApplicationCommandOptionType.String, | ||
//}, | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "zero", | ||
Description = "Zero's Home Folder - COMMAND", | ||
Type = ApplicationCommandOptionType.SubCommand, | ||
Options = new List<ApplicationCommandOptionProperties>() | ||
{ | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "file", | ||
Description = "the file you want accessed.", | ||
Type = ApplicationCommandOptionType.String | ||
} | ||
} | ||
}, | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "johhny", | ||
Description = "Johnny Test's Home Folder - COMMAND", | ||
Type = ApplicationCommandOptionType.SubCommand, | ||
Options = new List<ApplicationCommandOptionProperties>() | ||
{ | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "file", | ||
Description = "the file you want accessed.", | ||
Type = ApplicationCommandOptionType.String | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
new ApplicationCommandOptionProperties() | ||
{ | ||
Name = "random", | ||
Description = "Random things", | ||
Type = ApplicationCommandOptionType.SubCommand | ||
} | ||
} | ||
}, 386658607338618891) ; | ||
} | ||
|
||
public async Task RunAsync() | ||
{ | ||
await socketClient.LoginAsync(TokenType.Bot, botToken); | ||
await socketClient.StartAsync(); | ||
|
||
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services); | ||
await _commands.RegisterCommandsAsync(socketClient, new List<ulong>() | ||
{ | ||
386658607338618891 | ||
}, | ||
new CommandRegistrationOptions(OldCommandOptions.DELETE_UNUSED,ExistingCommandOptions.OVERWRITE)); | ||
|
||
// If you would like to register your commands manually use: | ||
//-----------------------------------------// | ||
// | ||
// await _commands.BuildCommands(); | ||
// | ||
//-----------------------------------------// | ||
// Though I wouldn't highly recommend it unless you want to do something very specific with them | ||
// such as only registering some commands on only some guilds, or editing them manually. | ||
|
||
await Task.Delay(-1); | ||
} | ||
|
||
private async Task InteractionHandler(SocketInteraction arg) | ||
{ | ||
if(arg.Type == InteractionType.ApplicationCommand) | ||
{ | ||
await _commands.ExecuteAsync(arg); | ||
} | ||
} | ||
|
||
private Task SocketClient_Log(LogMessage arg) | ||
{ | ||
Console.WriteLine("[Discord] " + arg.ToString()); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using Discord.Commands; | ||
using Discord.Commands.SlashCommands.Types; | ||
using Discord.SlashCommands; | ||
using Discord.WebSocket; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SlashCommandsExample.Modules | ||
{ | ||
// You can make the whole module Global | ||
//[Global] | ||
public class DevModule : SlashCommandModule<SocketInteraction> | ||
{ | ||
[SlashCommand("ping", "Ping the bot to see if it's alive!")] | ||
[Global] | ||
public async Task PingAsync() | ||
{ | ||
await Reply(":white_check_mark: **Bot Online**"); | ||
} | ||
|
||
[SlashCommand("echo", "I'll repeate everything you said to me, word for word.")] | ||
public async Task EchoAsync( | ||
[Description("The message you want repetead")] | ||
[Required] | ||
string message) | ||
{ | ||
await Reply($"{Interaction.Member?.Nickname ?? Interaction.Member?.Username} told me to say this: \r\n{message}"); | ||
} | ||
|
||
[SlashCommand("overload","Just hit me with every type of data you got, man!")] | ||
public async Task OverloadAsync( | ||
[ParameterName("var1")] | ||
bool? boolean, | ||
[ParameterName("var2")] | ||
int? integer, | ||
[ParameterName("var3")] | ||
string myString, | ||
SocketGuildChannel channel, | ||
SocketGuildUser user, | ||
SocketRole role | ||
) | ||
{ | ||
await Reply($"You gave me:\r\n {boolean}, {integer}, {myString}, <#{channel?.Id}>, {user?.Mention}, {role?.Mention}"); | ||
|
||
} | ||
|
||
[SlashCommand("stats","Get the stats from Game(tm) for players or teams.")] | ||
public async Task GetStatsAsync( | ||
[Required] | ||
[Choice("XBOX","xbox")] | ||
[Choice("PlayStation","ps")] | ||
[Choice("PC","pc")] | ||
string platform, | ||
[Choice("Player",1)] | ||
[Choice("Team",2)] | ||
int searchType | ||
) | ||
{ | ||
await Reply($"Well I got this: {platform}, {searchType}"); | ||
} | ||
|
||
[CommandGroup("root")] | ||
//[Global] | ||
public class DevModule_Root : SlashCommandModule<SocketInteraction> | ||
{ | ||
[SlashCommand("rng", "Gives you a random number from this \"machine\"")] | ||
public async Task RNGAsync() | ||
{ | ||
var rand = new Random(); | ||
await Reply(rand.Next(0, 101).ToString()); | ||
} | ||
|
||
[CommandGroup("usr")] | ||
public class DevModule_Root_Usr : SlashCommandModule<SocketInteraction> | ||
{ | ||
[SlashCommand("zero", "Gives you a file from user zero from this \"machine\"")] | ||
public async Task ZeroAsync([Description("The file you want.")] string file) | ||
{ | ||
await Reply($"You don't have permissiont to access {file} from user \"zero\"."); | ||
} | ||
[SlashCommand("johnny", "Gives you a file from user Johnny Test from this \"machine\"")] | ||
public async Task JohnnyAsync([Description("The file you want.")] string file) | ||
{ | ||
await Reply($"You don't have permissiont to access {file} from user \"johnny\"."); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
/* | ||
The base way of defining a command using the regular command service: | ||
|
||
public class PingModule : ModuleBase<SocketCommandContext> | ||
{ | ||
[Command("ping")] | ||
[Summary("Pong! Check if the bot is alive.")] | ||
public async Task PingAsync() | ||
{ | ||
await ReplyAsync(":white_check_mark: **Bot Online**"); | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Discord.SlashCommands; | ||
using Discord.WebSocket; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace SlashCommandsExample.Modules | ||
{ | ||
// Doesn't inherit from SlashCommandModule | ||
public class InvalidDefinition : Object | ||
{ | ||
// commands | ||
} | ||
|
||
// Isn't public | ||
class PrivateDefinition : SlashCommandModule<SocketInteraction> | ||
{ | ||
// commands | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* This project, is at this moment used for testing and debugging the new and experimental Slash Commands. | ||
* After all testing has been done, and the project is ready to be integrated into the main Discord.Net ecosystem | ||
* this project should be re-made into one that could be used as an example usage of the new Slash Command Service. | ||
*/ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.SlashCommands; | ||
using Discord.WebSocket; | ||
|
||
namespace SlashCommandsExample | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
|
||
DiscordClient discordClient = new DiscordClient(); | ||
// This could instead be handled in another thread, if for whatever reason you want to continue execution in the main Thread. | ||
discordClient.RunAsync().GetAwaiter().GetResult(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"profiles": { | ||
"SlashCommandsExample": { | ||
"commandName": "Project" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: You can declare
Main
asTask Main(string[] args)
and simplyawait
yourRunAsync()
call to avoid mixing synchronous and asynchronous code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really matter here, as they both ultimately compile down to the same IL internally; at least, as far as this example is concerned.