Allow for the creation of fake MessageCreateEventArgs#2042
Conversation
…age create event args to be created for manual usage of the text commands extension
akiraveliara
left a comment
There was a problem hiding this comment.
few nitpicks in addition to the discussion above
| /// <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 |
There was a problem hiding this comment.
nitpick: this is technically inaccurate, only one of the two is ever required
There was a problem hiding this comment.
Which one isn't required
There was a problem hiding this comment.
neither is required individually, it's just that one of them must be present, no matter which one
| [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); |
There was a problem hiding this comment.
naming convention; pascal case (either that, or exactly match the name of the extern, as we do for library imports)
There was a problem hiding this comment.
These are private members, why are we using pascal case
There was a problem hiding this comment.
methods are always pascal case by convention, private or not
There was a problem hiding this comment.
Ah, yes. These are methods. Not properties. I have eyes. Sometimes I just can't use them.
|
|
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. |
Summary
In order to use any functionality of
TextCommandProcessoryou must have aMessageCreateEventArgsavailable. 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.