-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerModule.cs
More file actions
119 lines (107 loc) · 4.32 KB
/
Copy pathServerModule.cs
File metadata and controls
119 lines (107 loc) · 4.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Globalization;
using System.Text;
using Discord;
using Discord.Interactions;
using Microsoft.Extensions.DependencyInjection;
using RustPlusBot.Persistence.Servers;
namespace RustPlusBot.Discord.Modules;
/// <summary>Guild-scoped management of Rust+ servers via slash commands.</summary>
/// <param name="scopeFactory">Creates a short-lived DI scope per command.</param>
[Group("server", "Manage this guild's Rust+ servers")]
public sealed class ServerModule(IServiceScopeFactory scopeFactory)
: InteractionModuleBase<SocketInteractionContext>
{
/// <summary>Adds a Rust+ server to this guild.</summary>
/// <param name="name">Display name.</param>
/// <param name="ip">Server host or ip.</param>
/// <param name="port">Rust+ app port.</param>
[SlashCommand("add", "Add a Rust+ server to this guild")]
public async Task AddAsync(
[Summary("name", "Display name")] string name,
[Summary("ip", "Server host or ip")] string ip,
[Summary("port", "Rust+ app port")] int port)
{
if (Context.Guild is null)
{
await RespondAsync("This command must be used in a server.", ephemeral: true).ConfigureAwait(false);
return;
}
if (port is < 1 or > 65535)
{
await RespondAsync("Port must be between 1 and 65535.", ephemeral: true).ConfigureAwait(false);
return;
}
var scope = scopeFactory.CreateAsyncScope();
try
{
var service = scope.ServiceProvider.GetRequiredService<IServerService>();
var server = await service.AddAsync(Context.Guild.Id, Context.User.Id, name, ip, port)
.ConfigureAwait(false);
await RespondAsync($"Added **{server.Name}** (`{server.Id}`).", ephemeral: true).ConfigureAwait(false);
}
finally
{
await scope.DisposeAsync().ConfigureAwait(false);
}
}
/// <summary>Lists this guild's Rust+ servers.</summary>
[SlashCommand("list", "List this guild's Rust+ servers")]
public async Task ListAsync()
{
if (Context.Guild is null)
{
await RespondAsync("This command must be used in a server.", ephemeral: true).ConfigureAwait(false);
return;
}
var scope = scopeFactory.CreateAsyncScope();
try
{
var service = scope.ServiceProvider.GetRequiredService<IServerService>();
var servers = await service.ListAsync(Context.Guild.Id).ConfigureAwait(false);
if (servers.Count == 0)
{
await RespondAsync("No servers configured.", ephemeral: true).ConfigureAwait(false);
return;
}
var builder = new StringBuilder();
foreach (var server in servers)
{
builder.AppendLine(CultureInfo.InvariantCulture,
$"• **{server.Name}** — `{server.Ip}:{server.Port}` (`{server.Id}`)");
}
await RespondAsync(builder.ToString(), ephemeral: true).ConfigureAwait(false);
}
finally
{
await scope.DisposeAsync().ConfigureAwait(false);
}
}
/// <summary>Removes a Rust+ server by id.</summary>
/// <param name="id">The server id from <c>/server list</c>.</param>
[SlashCommand("remove", "Remove a Rust+ server by id")]
public async Task RemoveAsync([Summary("id", "The server id from /server list")] string id)
{
if (Context.Guild is null)
{
await RespondAsync("This command must be used in a server.", ephemeral: true).ConfigureAwait(false);
return;
}
if (!Guid.TryParse(id, out var serverId))
{
await RespondAsync("That is not a valid server id.", ephemeral: true).ConfigureAwait(false);
return;
}
var scope = scopeFactory.CreateAsyncScope();
try
{
var service = scope.ServiceProvider.GetRequiredService<IServerService>();
var removed = await service.RemoveAsync(Context.Guild.Id, serverId).ConfigureAwait(false);
await RespondAsync(removed ? "Removed." : "No matching server found.", ephemeral: true)
.ConfigureAwait(false);
}
finally
{
await scope.DisposeAsync().ConfigureAwait(false);
}
}
}