Skip to content

Commit

Permalink
Add prefill for tag modals
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Feb 27, 2024
1 parent b7fc324 commit 754b815
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
10 changes: 8 additions & 2 deletions ModCore/Commands/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public class Tags : ApplicationCommandModule
exists? "Update global tag contents" : "Create new global tag", new Dictionary<string, string>()
{
{ "n", name }
});
}, exists ? new Dictionary<string, string>()
{
{ nameof(SetTagModal.Content), tag.Contents }
} : null);
}

[SlashCommand("override", "Creates a channel-specific override for a tag.")]
Expand All @@ -75,7 +78,10 @@ public class Tags : ApplicationCommandModule
exists ? "Update channel tag contents" : "Create new channel tag", new Dictionary<string, string>()
{
{ "n", name }
});
}, exists? new Dictionary<string, string>()
{
{ nameof(OverrideTagModal.Content), tag.Contents }
} : null);
}

[SlashCommand("remove", "Removes a tag.")]
Expand Down
9 changes: 7 additions & 2 deletions ModCore/Extensions/Handlers/ModalHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ModalHandler(Type type, ModalAttribute info)
fields.TryAdd(property.GetCustomAttribute<ModalFieldAttribute>(), property);
}

public async Task CreateAsync(DiscordInteraction interaction, string title, IDictionary<string, string> hiddenValues)
public async Task CreateAsync(DiscordInteraction interaction, string title, IDictionary<string, string> hiddenValues, IDictionary<string, string> prefill = null)
{
var modalString = ExtensionStatics.GenerateIdString(Info.ModalId, hiddenValues);

Expand All @@ -51,9 +51,14 @@ public async Task CreateAsync(DiscordInteraction interaction, string title, IDic
foreach (var field in fields)
{
var attr = field.Key;
string prefillValue = null;
if(prefill != null && prefill.Keys.Contains(field.Value.Name))
{
prefillValue = prefill[field.Value.Name];
}
interactionResp.AddComponents(
new TextInputComponent(attr.DisplayText, attr.FieldName,
attr.Placeholder, attr.Prefill, attr.Required, attr.InputStyle, attr.MinLength, attr.MaxLength));
attr.Placeholder, attr.Prefill ?? prefillValue ?? null, attr.Required, attr.InputStyle, attr.MinLength, attr.MaxLength));
}

await interaction.CreateResponseAsync(InteractionResponseType.Modal, interactionResp);
Expand Down
4 changes: 2 additions & 2 deletions ModCore/Extensions/InteractionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public InteractionExtension(IServiceProvider services)
}

public async Task RespondWithModalAsync<T>(DiscordInteraction interaction, string title,
IDictionary<string, string> hiddenValues = null) where T : IModal
IDictionary<string, string> hiddenValues = null, IDictionary<string, string> prefill = null) where T : IModal
{
// Cache for next call :^)
var handler = modalHandlers.FirstOrDefault(x => x.Type == typeof(T)) ?? registerModalHandler(typeof(T));

await handler.CreateAsync(interaction, title, hiddenValues);
await handler.CreateAsync(interaction, title, hiddenValues, prefill);
}

public string GenerateId(string id, params (string Key, string Value)[] values)
Expand Down

0 comments on commit 754b815

Please sign in to comment.