Skip to content

Commit 4198235

Browse files
committed
Update to components v2 and improve UX
1 parent 31ff815 commit 4198235

File tree

8 files changed

+62
-80
lines changed

8 files changed

+62
-80
lines changed

NetCordBuddy/Docs/DocsService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ void LoadMember(ISymbol symbol, string? parentId)
166166
if (symbol is INamespaceOrTypeSymbol namespaceOrType)
167167
{
168168
if (namespaceOrType is ITypeSymbol)
169-
symbols.Add(new(id, null, docsUrl));
169+
symbols.Add(new(id, null, SymbolFormatter.GetQualifiedName(symbol), docsUrl));
170170

171171
foreach (var child in namespaceOrType.GetMembers())
172172
LoadMember(child, id);
173173
}
174174
else
175-
symbols.Add(new(id, parentId, docsUrl));
175+
symbols.Add(new(id, parentId, SymbolFormatter.GetQualifiedName(symbol), docsUrl));
176176
}
177177
}
178178

NetCordBuddy/Docs/DocsSymbolInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace NetCordBuddy.Docs;
44

5-
public partial class DocsSymbolInfo(string id, string? parentId, string docsUrl)
5+
public partial class DocsSymbolInfo(string id, string? parentId, string displayName, string docsUrl)
66
{
77
[GeneratedRegex(@"\W")]
88
private static partial Regex GetFragmentRegex();
99

10-
public string Name { get; } = id;
10+
public string Name => displayName;
1111

1212
public string DocsUrl { get; } = parentId is null ? $"{docsUrl}/{id.Replace('`', '-')}.html" : $"{docsUrl}/{parentId.Replace('`', '-')}.html#{GetFragmentRegex().Replace(id, "_")}";
1313
}

NetCordBuddy/Docs/SymbolFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private static ImmutableArray<SymbolDisplayPart> GetDisplayParts(
109109
}
110110
catch (InvalidOperationException)
111111
{
112-
return ImmutableArray<SymbolDisplayPart>.Empty;
112+
return [];
113113
}
114114

115115
static ImmutableArray<SymbolDisplayPart> GetCastOperatorOverloadDisplayParts(ImmutableArray<SymbolDisplayPart> parts)
@@ -122,7 +122,7 @@ static ImmutableArray<SymbolDisplayPart> GetCastOperatorOverloadDisplayParts(Imm
122122
if (part.Kind is SymbolDisplayPartKind.Keyword && part.ToString() is "operator" or "checked")
123123
break;
124124
}
125-
return parts.Take(endIndex + 1).ToImmutableArray();
125+
return [.. parts.Take(endIndex + 1)];
126126
}
127127
}
128128

NetCordBuddy/Modules/SlashCommands/DocsCommand.cs renamed to NetCordBuddy/Modules/ApplicationCommands/DocsCommand.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
using Microsoft.Extensions.Options;
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.Extensions.Options;
23

34
using NetCord;
45
using NetCord.Rest;
56
using NetCord.Services.ApplicationCommands;
67

78
using NetCordBuddy.Docs;
89

9-
namespace NetCordBuddy.Modules.SlashCommands;
10+
namespace NetCordBuddy.Modules.ApplicationCommands;
1011

11-
public class DocsCommand(DocsService docsService, IOptions<Configuration> options) : ApplicationCommandModule<SlashCommandContext>
12+
public class DocsCommand(DocsService docsService, IOptions<Configuration> options) : ApplicationCommandModule<ApplicationCommandContext>
1213
{
1314
[SlashCommand("docs", "Allows you to search the documentation via Discord", IntegrationTypes = [ApplicationIntegrationType.GuildInstall, ApplicationIntegrationType.UserInstall])]
1415
public InteractionMessageProperties Docs([SlashCommandParameter(Description = "Search query", MaxLength = 90, AutocompleteProviderType = typeof(QueryAutocompleteProvider))] string query)
1516
{
1617
var config = options.Value;
17-
return new InteractionMessageProperties().AddEmbeds(DocsHelper.CreateDocsEmbed(query, 0, docsService, config, Context.Interaction.Id, Context.User, out var more))
18-
.WithComponents(DocsHelper.CreateDocsComponents(query, 0, more, config));
18+
return new InteractionMessageProperties().AddComponents(DocsHelper.CreateDocsComponents(query, 0, docsService, config))
19+
.WithFlags(MessageFlags.IsComponentsV2);
1920
}
2021

2122
private class QueryAutocompleteProvider(DocsService docsService) : IAutocompleteProvider<AutocompleteInteractionContext>

NetCordBuddy/Modules/ButtonInteractions/DocsInteraction.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88

99
namespace NetCordBuddy.Modules.ButtonInteractions;
1010

11-
public class DocsInteraction(DocsService docsService, IOptions<Configuration> options) : ComponentInteractionModule<ButtonInteractionContext>
11+
public class DocsInteraction(DocsService docsService, IOptions<Configuration> options) : ComponentInteractionModule<ComponentInteractionContext>
1212
{
1313
[ComponentInteraction("docs")]
1414
public InteractionCallback Docs(int page, string query)
1515
{
16-
var interactionMetadata = Context.Message.InteractionMetadata!;
1716
var config = options.Value;
18-
return InteractionCallback.ModifyMessage(m => m.AddEmbeds(DocsHelper.CreateDocsEmbed(query, page, docsService, config, interactionMetadata.Id, interactionMetadata.User, out var more))
19-
.WithComponents(DocsHelper.CreateDocsComponents(query, page, more, config)));
17+
return InteractionCallback.ModifyMessage(m => m.AddComponents(DocsHelper.CreateDocsComponents(query, page, docsService, config)));
2018
}
2119
}

NetCordBuddy/Modules/DocsHelper.cs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,42 @@ namespace NetCordBuddy.Modules;
77

88
internal static class DocsHelper
99
{
10-
public static EmbedProperties CreateDocsEmbed(string query, int page, DocsService docsService, Configuration config, ulong interactionId, User interactionUser, out bool more)
10+
public static IEnumerable<IComponentProperties> CreateDocsComponents(string query, int page, DocsService docsService, Configuration config)
1111
{
12-
var results = docsService.FindSymbols(query, page * 5, 5, out more);
13-
14-
if (results.Count == 0)
15-
throw new("No results found!");
16-
17-
var embedTitle = "Results";
18-
var footerText = interactionUser.Username;
19-
20-
int length = embedTitle.Length + footerText.Length;
21-
22-
var embedFields = results
23-
.Select(s => new EmbedFieldProperties()
24-
{
25-
Value = $"```cs\n{s.Name}```[Docs]({s.DocsUrl})",
26-
})
27-
.Where(f => f.Value!.Length <= 1024)
28-
.TakeWhile(f => (length += f.Value!.Length) <= 6000);
29-
30-
return new()
31-
{
32-
Title = embedTitle,
33-
Footer = new()
34-
{
35-
Text = footerText,
36-
IconUrl = (interactionUser.HasAvatar ? interactionUser.GetAvatarUrl() : interactionUser.DefaultAvatarUrl).ToString(),
37-
},
38-
Fields = embedFields,
39-
Timestamp = Snowflake.CreatedAt(interactionId),
40-
Color = new(config.PrimaryColor),
41-
};
42-
}
12+
var results = docsService.FindSymbols(query, page * 5, 5, out var more);
13+
14+
var container = new ComponentContainerProperties()
15+
.WithAccentColor(new(config.PrimaryColor));
16+
17+
if (results.Count is 0)
18+
return
19+
[
20+
container
21+
.AddComponents(new TextDisplayProperties("# No symbols found!"))
22+
];
23+
24+
var title = "# Symbols";
25+
26+
int length = title.Length;
27+
28+
var sections = results
29+
.Select(s => new ComponentSectionProperties(new LinkButtonProperties(s.DocsUrl, "Docs"))
30+
.AddComponents(new TextDisplayProperties($"```cs\n{s.Name}```")))
31+
.TakeWhile(s => (length += s.Components.First().Content.Length) <= 4000);
4332

44-
public static IEnumerable<MessageComponentProperties> CreateDocsComponents(string query, int page, bool more, Configuration config)
45-
{
4633
return
4734
[
48-
new ActionRowProperties(
49-
[
50-
new ButtonProperties($"docs:{page - 1}:{query}", new EmojiProperties(config.Emojis.Left), ButtonStyle.Primary)
51-
{
52-
Disabled = page < 1,
53-
},
54-
new ButtonProperties($"docs:{page + 1}:{query}", new EmojiProperties(config.Emojis.Right), ButtonStyle.Primary)
55-
{
56-
Disabled = !more,
57-
},
58-
]),
35+
container
36+
.AddComponents(new TextDisplayProperties(title))
37+
.AddComponents(sections)
38+
.AddComponents(new ActionRowProperties()
39+
.AddButtons(
40+
new ButtonProperties($"docs:{page - 1}:{query}", new EmojiProperties(config.Emojis.Left), ButtonStyle.Primary)
41+
.WithDisabled(page < 1),
42+
new ButtonProperties($"docs:{page + 1}:{query}", new EmojiProperties(config.Emojis.Right), ButtonStyle.Primary)
43+
.WithDisabled(!more)
44+
)
45+
)
5946
];
6047
}
6148
}

NetCordBuddy/NetCordBuddy.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net9.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
12-
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
13-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
14-
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
15-
<PackageReference Include="NetCord" Version="1.0.0-alpha.292" />
16-
<PackageReference Include="NetCord.Hosting" Version="1.0.0-alpha.62" />
17-
<PackageReference Include="NetCord.Hosting.Services" Version="1.0.0-alpha.71" />
18-
<PackageReference Include="NetCord.Services" Version="1.0.0-alpha.193" />
11+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.5" />
14+
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.5" />
15+
<PackageReference Include="NetCord" Version="1.0.0-alpha.371" />
16+
<PackageReference Include="NetCord.Hosting" Version="1.0.0-alpha.371" />
17+
<PackageReference Include="NetCord.Hosting.Services" Version="1.0.0-alpha.371" />
18+
<PackageReference Include="NetCord.Services" Version="1.0.0-alpha.371" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

NetCordBuddy/Program.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33

4-
using NetCord;
54
using NetCord.Hosting.Gateway;
65
using NetCord.Hosting.Services;
76
using NetCord.Hosting.Services.ApplicationCommands;
87
using NetCord.Hosting.Services.ComponentInteractions;
9-
using NetCord.Services.ApplicationCommands;
10-
using NetCord.Services.ComponentInteractions;
118

129
using NetCordBuddy;
1310
using NetCordBuddy.Docs;
1411

1512
var builder = Host.CreateApplicationBuilder(args);
1613

17-
builder.Services
14+
var services = builder.Services;
15+
16+
services
1817
.AddOptions<Configuration>()
1918
.BindConfiguration(string.Empty)
2019
.ValidateOnStart()
2120
.ValidateDataAnnotations();
2221

23-
builder.Services
22+
services
2423
.ConfigureHttpClientDefaults(b => b.RemoveAllLoggers())
2524
.AddSingleton<DocsService>()
2625
.AddHostedService(services => services.GetRequiredService<DocsService>())
27-
.AddApplicationCommands<SlashCommandInteraction, SlashCommandContext, AutocompleteInteractionContext>()
28-
.AddComponentInteractions<ButtonInteraction, ButtonInteractionContext>()
29-
.AddDiscordGateway(options => options.Configuration = new()
30-
{
31-
Intents = 0,
32-
});
26+
.AddApplicationCommands()
27+
.AddComponentInteractions()
28+
.AddDiscordGateway(options => options.Intents = 0);
3329

3430
var host = builder.Build()
3531
.AddModules(typeof(Program).Assembly)

0 commit comments

Comments
 (0)