-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerResolver.cs
More file actions
50 lines (44 loc) · 2.01 KB
/
Copy pathServerResolver.cs
File metadata and controls
50 lines (44 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using RustPlusBot.Features.Commands.Localization;
using RustPlusBot.Persistence.Servers;
namespace RustPlusBot.Features.Commands.Servers;
/// <summary>Picks the target server for a slash command from an optional (autocompleted) server argument.</summary>
/// <param name="servers">The server service used to list the guild's servers.</param>
/// <param name="localizer">Resolves the error messages.</param>
internal sealed class ServerResolver(IServerService servers, ICommandLocalizer localizer)
{
/// <summary>Resolves the target server, or returns a localized error.</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverArg">The raw server argument (a server id string) or null.</param>
/// <param name="culture">The guild culture.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The resolved server, or an error message.</returns>
public async Task<ServerResolution> ResolveAsync(
ulong guildId,
string? serverArg,
string culture,
CancellationToken cancellationToken)
{
var known = await servers.ListAsync(guildId, cancellationToken).ConfigureAwait(false);
if (known.Count == 0)
{
return new ServerResolution(null, null, localizer.Get("command.server.none", culture));
}
if (string.IsNullOrWhiteSpace(serverArg))
{
if (known.Count == 1)
{
return new ServerResolution(known[0].Id, known[0].Name, null);
}
return new ServerResolution(null, null, localizer.Get("command.server.specify", culture));
}
if (Guid.TryParse(serverArg, out var id))
{
var match = known.FirstOrDefault(s => s.Id == id);
if (match is not null)
{
return new ServerResolution(match.Id, match.Name, null);
}
}
return new ServerResolution(null, null, localizer.Get("command.server.unknown", culture));
}
}