-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeaderService.cs
More file actions
73 lines (66 loc) · 3.44 KB
/
Copy pathLeaderService.cs
File metadata and controls
73 lines (66 loc) · 3.44 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using RustPlusBot.Features.Commands.Localization;
using RustPlusBot.Features.Connections.Listening;
namespace RustPlusBot.Features.Commands.Leader;
/// <summary>One selectable team member for the <c>/leader</c> promote select.</summary>
/// <param name="SteamId">The member's Steam64 id.</param>
/// <param name="Name">The member's in-game name.</param>
/// <param name="IsLeader">True if this member is the current team leader.</param>
internal sealed record LeaderMemberOption(ulong SteamId, string Name, bool IsLeader);
/// <summary>The members to offer, or an error message to show instead.</summary>
/// <param name="Members">The selectable members (empty when <paramref name="ErrorMessage"/> is set).</param>
/// <param name="ErrorMessage">A localized message to show instead of a select, or null to proceed.</param>
internal sealed record LeaderTeamResult(IReadOnlyList<LeaderMemberOption> Members, string? ErrorMessage);
/// <summary>The testable logic behind <c>/leader</c>: fetch live members, then promote one.</summary>
/// <param name="query">The live-server query seam.</param>
/// <param name="localizer">Resolves the result/error messages.</param>
internal sealed class LeaderService(IRustServerQuery query, ICommandLocalizer localizer)
{
/// <summary>Fetches the current team for the promote select, or an error message.</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverId">The target server id.</param>
/// <param name="culture">The guild culture.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The members to offer, or an error message.</returns>
public async Task<LeaderTeamResult> GetMembersAsync(
ulong guildId,
Guid serverId,
string culture,
CancellationToken cancellationToken)
{
var team = await query.GetTeamInfoAsync(guildId, serverId, cancellationToken).ConfigureAwait(false);
if (team is null)
{
return new LeaderTeamResult([], localizer.Get("leader.notconnected", culture));
}
if (team.Members.Count == 0)
{
return new LeaderTeamResult([], localizer.Get("leader.noteam", culture));
}
var members = team.Members
.Select(m => new LeaderMemberOption(m.SteamId, m.Name, m.SteamId == team.LeaderSteamId))
.ToList();
return new LeaderTeamResult(members, null);
}
/// <summary>Promotes a member and returns the localized result message.</summary>
/// <param name="guildId">The owning guild snowflake.</param>
/// <param name="serverId">The target server id.</param>
/// <param name="steamId">The Steam64 id to promote.</param>
/// <param name="memberName">The member's name (for the success message).</param>
/// <param name="culture">The guild culture.</param>
/// <param name="cancellationToken">A cancellation token.</param>
/// <returns>The localized result message.</returns>
public async Task<string> PromoteAsync(
ulong guildId,
Guid serverId,
ulong steamId,
string memberName,
string culture,
CancellationToken cancellationToken)
{
var promoted = await query.PromoteToLeaderAsync(guildId, serverId, steamId, cancellationToken)
.ConfigureAwait(false);
return promoted
? localizer.Get("leader.success", culture, memberName)
: localizer.Get("leader.failure", culture);
}
}