This commit is contained in:
Noi 2024-11-20 19:30:22 +00:00 committed by GitHub
commit 5b7b4d13a6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 5 deletions

View file

@ -20,7 +20,7 @@ public class BirthdayModule : BotModuleBase {
public class SubCmdsBirthdaySet : BotModuleBase {
[SlashCommand("date", HelpCmdSetDate)]
public async Task CmdSetBday([Summary(description: HelpOptDate)] string date,
[Summary(description: HelpOptZone)] string? zone = null) {
[Summary(description: HelpOptZone), Autocomplete<TzAutocompleteHandler>] string? zone = null) {
int inmonth, inday;
try {
(inmonth, inday) = ParseDate(date);
@ -63,7 +63,7 @@ public class BirthdayModule : BotModuleBase {
}
[SlashCommand("timezone", HelpCmdSetZone)]
public async Task CmdSetZone([Summary(description: HelpOptZone)] string zone) {
public async Task CmdSetZone([Summary(description: HelpOptZone), Autocomplete<TzAutocompleteHandler>] string zone) {
using var db = new BotDatabaseContext();
var user = ((SocketGuildUser)Context.User).GetUserEntryOrNew(db);

View file

@ -44,7 +44,7 @@ public class BirthdayOverrideModule : BotModuleBase {
[SlashCommand("set-timezone", "Set a user's time zone on their behalf.")]
public async Task OvSetTimezone([Summary(description: HelpOptOvTarget)] SocketGuildUser target,
[Summary(description: HelpOptZone)] string zone) {
[Summary(description: HelpOptZone), Autocomplete<TzAutocompleteHandler>] string zone) {
using var db = new BotDatabaseContext();
var user = target.GetUserEntryOrNew(db);

View file

@ -193,7 +193,7 @@ public class ConfigModule : BotModuleBase {
}
[SlashCommand("set-timezone", "Configure the time zone to use by default in the server." + HelpPofxBlankUnset)]
public async Task CmdSetTimezone([Summary(description: HelpOptZone)] string? zone = null) {
public async Task CmdSetTimezone([Summary(description: HelpOptZone), Autocomplete<TzAutocompleteHandler>] string? zone = null) {
const string Response = ":white_check_mark: The server's time zone has been ";
if (zone == null) {

View file

@ -0,0 +1,34 @@
using BirthdayBot.Data;
using Discord.Interactions;
using Microsoft.EntityFrameworkCore;
namespace BirthdayBot.ApplicationCommands;
public class TzAutocompleteHandler : AutocompleteHandler {
public override Task<AutocompletionResult> GenerateSuggestionsAsync(IInteractionContext cx,
IAutocompleteInteraction ia, IParameterInfo pm,
IServiceProvider sv) {
var input = ((SocketAutocompleteInteraction)ia).Data.Current.Value.ToString()!;
var inparts = input.Split('/', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var db = new BotDatabaseContext();
var query = db.UserEntries.AsNoTracking();
if (inparts.Length == 2) {
query = query.Where(u => EF.Functions.ILike(u.TimeZone!, $"%{inparts[0]}%/%{inparts[1]}%"));
} else {
// No '/' in query - search for string within each side of zone name (tested to not give conflicting results)
query = query.Where(u =>
EF.Functions.ILike(u.TimeZone!, $"%{input}%/%") || EF.Functions.ILike(u.TimeZone!, $"%/%{input}%"));
}
// TODO Could find a way to include all remaining zones at the bottom of results for full completion
// TODO Filter out undesirable zone names, aliases, etc from autocompletion
var result = query.GroupBy(u => u.TimeZone)
.Select(g => new { ZoneName = g.Key, Count = g.Count() })
.OrderByDescending(x => x.Count)
.Take(25)
.Select(x => new AutocompleteResult(x.ZoneName, x.ZoneName))
.ToList();
return Task.FromResult(AutocompletionResult.FromSuccess(result));
}
}

View file

@ -24,7 +24,7 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Discord.Net" Version="3.14.1" />
<PackageReference Include="Discord.Net" Version="3.15.2" />
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
<PrivateAssets>all</PrivateAssets>