Skip to content

Commit

Permalink
Command name autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Oct 31, 2024
1 parent 0aa6d89 commit 05cfd2b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/AutocompleteProviders/CommandAutocompleteProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus.Commands.Processors.SlashCommands;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Entities;
using Humanizer;

namespace OoLunar.Tomoe.AutocompleteProviders
{
public sealed class CommandAutocompleteProvider : IAutoCompleteProvider
{
public ValueTask<IEnumerable<DiscordAutoCompleteChoice>> AutoCompleteAsync(AutoCompleteContext context)
{
if (string.IsNullOrWhiteSpace(context.UserInput))
{
return ValueTask.FromResult<IEnumerable<DiscordAutoCompleteChoice>>(context.Extension.Commands.Values
.Take(25)
.Select(command => new DiscordAutoCompleteChoice(command.FullName.Humanize(LetterCasing.Title), command.FullName))
.OrderBy(command => command.Name));
}

List<DiscordAutoCompleteChoice> choices = [];
foreach (Command command in context.Extension.Commands.Values.SelectMany(command => command.Flatten()))
{
if (command.FullName.Contains(context.UserInput, StringComparison.OrdinalIgnoreCase))
{
choices.Add(new DiscordAutoCompleteChoice(command.FullName.Humanize(LetterCasing.Title), command.FullName));
}
}

return ValueTask.FromResult<IEnumerable<DiscordAutoCompleteChoice>>(choices
.OrderBy(x => x.Name.StartsWith(context.UserInput, StringComparison.OrdinalIgnoreCase) ? 0 : 1)
.ThenBy(x => x.Name));
}
}
}
4 changes: 3 additions & 1 deletion src/Commands/Common/SourceCodeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
using System.Threading.Tasks;
using DSharpPlus.Commands;
using DSharpPlus.Commands.ArgumentModifiers;
using DSharpPlus.Commands.Processors.SlashCommands.ArgumentModifiers;
using DSharpPlus.Commands.Processors.TextCommands;
using DSharpPlus.Commands.Trees;
using DSharpPlus.Commands.Trees.Metadata;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using OoLunar.Tomoe.AutocompleteProviders;

namespace OoLunar.Tomoe.Commands.Common
{
Expand Down Expand Up @@ -222,7 +224,7 @@ private static IEnumerable<T> GetNodes<T>(this IEnumerable<SyntaxNode> nodes) wh
/// Sends a link to the repository which contains the code for the bot.
/// </summary>
[Command("source_code"), TextAlias("repository", "source", "code", "repo")]
public static async ValueTask ExecuteAsync(CommandContext context, [RemainingText] string? commandName = null)
public static async ValueTask ExecuteAsync(CommandContext context, [RemainingText, SlashAutoCompleteProvider<CommandAutocompleteProvider>] string? commandName = null)
{
if (string.IsNullOrWhiteSpace(commandName))
{
Expand Down

0 comments on commit 05cfd2b

Please sign in to comment.