Skip to content

Conversation

@OoLunar
Copy link
Contributor

@OoLunar OoLunar commented Jul 26, 2024

Summary

In order to use any functionality of TextCommandProcessor you must have a MessageCreateEventArgs available. Currently, users cannot create any entities or event args. If the dev wants to run a command as another user or parse event arguments, they must create event args through reflection. This PR does that for them.

Notes

Tested.

OoLunar added 2 commits July 26, 2024 11:39
…age create event args to be created for manual usage of the text commands extension
@OoLunar OoLunar added enhancement new-features commands For issues related to DSharpPlus.Commands labels Jul 26, 2024
@OoLunar OoLunar requested a review from akiraveliara July 26, 2024 17:21
@OoLunar OoLunar self-assigned this Jul 26, 2024
@akiraveliara akiraveliara added this to the v5.0 milestone Jul 26, 2024
Copy link
Member

@akiraveliara akiraveliara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few nitpicks in addition to the discussion above

Comment on lines +25 to 29
/// <summary>
/// The intents required for this processor to function.
/// </summary>
public const DiscordIntents RequiredIntents = DiscordIntents.DirectMessages // Required for commands executed in DMs
| DiscordIntents.GuildMessages; // Required for commands that are executed via bot ping
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this is technically inaccurate, only one of the two is ever required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which one isn't required

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neither is required individually, it's just that one of them must be present, no matter which one

Comment on lines +15 to +67
[GeneratedRegex(@"<@&(\d+)>", RegexOptions.Compiled)]
private static partial Regex roleMentionRegex();

[GeneratedRegex(@"<@!?(\d+)>", RegexOptions.Compiled)]
private static partial Regex userMentionRegex();

[GeneratedRegex(@"<#(\d+)>", RegexOptions.Compiled)]
private static partial Regex channelMentionRegex();

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
private static extern MessageCreatedEventArgs messageCreateEventArgsConstructor();

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Message")]
private static extern void messageCreateEventArgsMessageSetter(MessageCreatedEventArgs messageCreateEventArgs, DiscordMessage message);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_MentionedUsers")]
private static extern void messageCreateEventArgsMentionedUsersSetter(MessageCreatedEventArgs messageCreateEventArgs, IReadOnlyList<DiscordUser> mentionedUsers);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_MentionedRoles")]
private static extern void messageCreateEventArgsMentionedRolesSetter(MessageCreatedEventArgs messageCreateEventArgs, IReadOnlyList<DiscordRole> mentionedRoles);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_MentionedChannels")]
private static extern void messageCreateEventArgsMentionedChannelsSetter(MessageCreatedEventArgs messageCreateEventArgs, IReadOnlyList<DiscordChannel> mentionedChannels);

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
private static extern DiscordMessage messageConstructor();

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
private static extern DiscordMessage messageConstructor(DiscordMessage message);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Content")]
private static extern void messageContentSetter(DiscordMessage message, string content);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Channel")]
private static extern void messageChannelSetter(DiscordMessage message, DiscordChannel channel);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_ChannelId")]
private static extern void messageChannelIdSetter(DiscordMessage message, ulong channelId);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_guildId")]
private static extern void messageGuildIdSetter(DiscordMessage message, ulong? guildId);

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Author")]
private static extern void messageAuthorSetter(DiscordMessage message, DiscordUser author);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "mentionedUsers")]
private static extern ref List<DiscordUser> messageMentionedUsersSetter(DiscordMessage message);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "mentionedRoles")]
private static extern ref List<DiscordRole> messageMentionedRolesSetter(DiscordMessage message);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "mentionedChannels")]
private static extern ref List<DiscordChannel> messageMentionedChannelsSetter(DiscordMessage message);
Copy link
Member

@akiraveliara akiraveliara Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming convention; pascal case (either that, or exactly match the name of the extern, as we do for library imports)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are private members, why are we using pascal case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

methods are always pascal case by convention, private or not

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes. These are methods. Not properties. I have eyes. Sometimes I just can't use them.

@OoLunar
Copy link
Contributor Author

OoLunar commented Jul 31, 2024

Remove event args from argument converters and instead make use of ProcessorConverterContext similar to ProcessorCommandContext

See voice recording

@OoLunar OoLunar closed this Jul 31, 2024
@OoLunar
Copy link
Contributor Author

OoLunar commented Jul 31, 2024

The audio didn't record. Here's the TL;DR:

Drop event args from being passed to the converter and instead add the data specific to the event args into the processor specific converter context. Here's an example:

public class TextConverterContext : ConverterContext
{
    public required DiscordMessage Message { get; init; }
}

// ...

TextConverterContext converterContext = new()
{
    Message = eventArgs.Message,
    Channel = eventArgs.Channel,
    Command = command,
    Extension = this.extension,
    RawArguments = commandText[index..],
    ServiceScope = scope,
    Splicer = this.Configuration.TextArgumentSplicer,
    User = eventArgs.Author
};

By dropping the event args required by the argument converters, we may be able to greatly simplify the base command processor and processor implementation in general. Much appreciation to @quinchs for pointing this out.

@OoLunar OoLunar deleted the oolunar/feature/commands/fake-message-create-event-args branch July 31, 2024 03:30
@OoLunar OoLunar mentioned this pull request Aug 18, 2024
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands For issues related to DSharpPlus.Commands enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants