-
-
Notifications
You must be signed in to change notification settings - Fork 318
Introducing a new command framework #1553
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
Conversation
This doesn't include long, float and etc.
Also added DSharpPlus.CH into the sln and updated README.md
Also did some renaming on some of the proprties.
Did some renaming as well.
Also implemented a permission middleware for example. Some bug fixing is also present.
Updated TODO.md
horror, part 1
Also some roslyn analysing fixing.
This needs more work. Also added more to the TODO.md
Just need to make parameter mapping, conversion and binding.
This does not include error handling for it. Will do later.
I also did some bug fixing that I noticed in the code.
Also fixed so nullable is possible.
Also made so " " can be used in options as I forgot about that.
|
I'll think about it |
OoLunar
left a comment
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.
This is the most thorough I've been on a PR in a long while. I hold nothing against you. I am not mad at you. I appreciate all the work you've put into this.
With that being said, I have a ton of grievances with this, mostly excluding formatting, see comments below. Additionally, I still don't understand what purpose this extension would serve over the existing CommandsNext and SlashCommands extensions.
| @@ -0,0 +1,16 @@ | |||
| namespace DSharpPlus.UnifiedCommands; | |||
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.
Directory.Packages.props
Outdated
| <Project> | ||
| <ItemGroup> | ||
| <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0-1.final" /> | ||
| <PackageVersion Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> |
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.
I am against adding MSE.Configuration as it's usually the developer which configures this dependency. Instead we should stick to using our own Configuration classes which the developer can configure themselves.
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.
I didn't mean to have it like a configuration where you configure the class. I just following ASP.NET patterns a bit. But I will remove it to decrease dependencies as the use case is pretty useless.
tools/AutoUpdateChannelDescription/DSharpPlus.Tools.AutoUpdateChannelDescription.csproj
Show resolved
Hide resolved
| this.Discord.UseInteractivity(icfg); | ||
|
|
||
| this.SlashCommandService = this.Discord.UseSlashCommands(); | ||
| /* this.SlashCommandService = this.Discord.UseSlashCommands(); |
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.
Why are we commenting out the slash commands tests?
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.
I commented it out when testing the application part of the framework as SlashCommands would prevent me from testing that part. I haven't commented it out yet as I am still working on the application part.
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.
What would be an alternative solution?
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.
An alternative solution could be adding an option into the config.json for toggling on/off UnifiedCommands and SlashCommands.
DSharpPlus.UnifiedCommands/Application/Internals/ApplicationHandler.cs
Outdated
Show resolved
Hide resolved
DSharpPlus.UnifiedCommands/Application/Internals/ApplicationHandler.cs
Outdated
Show resolved
Hide resolved
| public bool IsNullable { get; set; } = false; | ||
|
|
||
| public ApplicationMethodParameterData(string name) | ||
| => Name = name; |
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.
Inline
| public ObjectFactory Factory { get; } | ||
|
|
||
| public ApplicationModuleData(ObjectFactory factory) | ||
| => Factory = factory; |
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.
Inline
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.
don't inline those please, having it indented by one level is fine - these lines can become very long, and id rather have all cases wrapped than some
| { | ||
| public string Name { get; } | ||
| public string Description { get; } | ||
| // public Type Type { get; } |
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.
Why is this commented out and what function does it serve? Can we use generic attributes for it?
OoLunar
left a comment
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.
This is the most thorough I've been on a PR in a long while. I hold nothing against you. I am not mad at you. I appreciate all the work you've put into this.
With that being said, I have a ton of grievances with this, mostly excluding formatting, see comments below. Additionally, I still don't understand what purpose this extension would serve over the existing CommandsNext and SlashCommands extensions.
|
I (was going to) initially started this as a personal project to have a more ASP.NET like experience with programing discord bots with D#+. As I don't like property/field di and I want proper implementation of scopes. And some other minor stuff. Talked about it a bit and it caught attention from @akiraveliara and @inftord. Namely this is just trying to be a more "modern" and easier to use/convenient command framework. Therefor following a public IApplicationResult Command()
{
return Reply("Hello there");
}kind of way as that's how ASP.NET does their stuff. |
Plerx2493
left a comment
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.
I added some comments about logging exceptions to the user. Feel free to correct me if anything is wrong
| { | ||
| _client.Logger.LogError( | ||
| e, | ||
| "Exception was thrown while trying to execute command {MethodName}. Was thrown from ", |
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.
Is this formatting right? looks as if there is something missing at the end. AFAIK the logger appends the exception which will be worded weird.:
Exception was thrown while trying to execute command {MethodName}. Was thrown from
System.Exception
....
| if (parameter.ParameterType != typeof(string)) | ||
| { | ||
| throw new InvalidMessageModuleStructure( | ||
| "You need to use string if you are using RemainingTextAttribute."); |
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.
Would be nice if the error message contains the methodname, so the user can see easily where they have to change something
| parameterData.Type = parameterType == typeof(double) | ||
| ? MessageParameterDataType.Double | ||
| : throw new InvalidMessageModuleStructure( | ||
| $"You cannot use type {parameterType.Name} as a parameter."); |
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.
Would be nice if the error message contains the methodname, so the user can see easily where they have to change something
| ApplicationOptionAttribute? name = parameter.GetCustomAttribute<ApplicationOptionAttribute>(); | ||
| if (name is null) | ||
| { | ||
| throw new Exception("Parameter needs to have `ApplicationNameAttribute` marked."); |
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.
Would be nice if the error message contains the methodname, so the user can see easily where they have to change something
| { | ||
| data.Type = parameter.ParameterType == typeof(DiscordAttachment) | ||
| ? ApplicationCommandOptionType.Attachment | ||
| : throw new Exception("Not a valid parameter type."); |
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.
Would be nice if the error message contains the methodname, so the user can see easily where they have to change something
akiraveliara
left a comment
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.
a note about formatting: don't inline initializer blocks, and if you wrap a line because it gets too long, use allman formatting. the pull request in its entirety is also sorely missing xmldocs.
it should be considered to unify the command conditions as far as possible. one of the main pain points with slash commands and commandsnext today is that they more or less duplicate this code.
you should probably consider adding todo tags for currently unfinished implementation so we can use IDE tooling to fill them all out before releasing a stable version.
i would furthermore like to reiterate my comments about changing Application* to Interaction, as well as restoring MessageCommand to its full length
DSharpPlus.UnifiedCommands/Application/Conditions/IApplicationCondition.cs
Outdated
Show resolved
Hide resolved
| @@ -0,0 +1,6 @@ | |||
| namespace DSharpPlus.UnifiedCommands.Application.Entities; | |||
|
|
|||
| public class DiscordModalBuilder | |||
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.
could be moved to corelib, once finished
| internal void AddCondition(Func<IServiceProvider, IApplicationCondition> func) | ||
| => _conditionBuilders.Add(func); | ||
|
|
||
| internal void ExecuteCommand(DiscordInteraction interaction, DiscordClient client) |
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.
this method will need large-scale future refactoring, just to note it here.
| /// <summary> | ||
| /// Enum representing the possibilities for conversion failure. | ||
| /// </summary> | ||
| public enum InvalidMessageConversionType |
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.
on second thoughts yeah this should be changed to something allowing users to define their own failure types
| public class MessageOptionAttribute : Attribute | ||
| { | ||
| public string Option { get; set; } | ||
| public string? ShorthandOption { get; set; } |
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.
there might be merit in turning this into a char
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.
Some shorthands might be more than one character. But usually that isn't the case. But I think we should keep shorthand option as a string for the people that needs it to be more than one character.
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.
this does lock us out of combining shorthands à la git commit -aS
|
|
||
| ## Decisions | ||
| - Should we continue using DSharpPlus.Interactivity or should it be reimplemented into CommandModule? | ||
| - Should we call classes as `xCommand` (for example `MessageCommand`) or should we call it `x` (for example `Message`) No newline at end of file |
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.
personally i think it should be Interaction and MessageCommand/TextCommand/whatever, our priority isn't with making names short
| Services.AddSingleton(configuration); | ||
| if (!Services.Any(s => s.ServiceType == typeof(IErrorHandler))) | ||
| { | ||
| client.Logger.LogTrace("Didn't find a error handler, using the default error handler"); |
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.
probably LogDebug here
Why didn't I build test before pushing these times? I have no single clue.
…to a more dependency like system.
This now makes so converters aren't hard coded anymore.
Fixed a bug with shorthand options and also tree traversal
|
Does the new condition API allow dependency injection? I currently have a check that requires access to a |
|
Closed by #1680. |
Summary
Introduces a new command framework called UnifiedCommands.
Details
UnifiedCommands is a command framework that does not depend on old, code that is hard to maintain and features that should be removed. Meaning a fresh slate for everything.
UnifiedCommands also tries to simplify the process of bot development by going for a ASP.NET like approach making it faster to write methods for commands.
It also has pre execution in form of conditions which is run before a command gets executed to make sure that the event follows the condition needed for execution to proceed. This can also modify values given through Dependency Injection if it is a singleton or scope.
Note
This is still in WIP. If you have any questions. Please ask me in the discord server or comment in this pr.
I will mark this as ready when it's ready for production usage, all features are completed, and if this command framework will get accepted.