add per-vm mods, fix typo

This commit is contained in:
Elijah R 2024-04-02 23:20:13 -04:00
parent 07e15ff0fb
commit 6f0def0d03
2 changed files with 26 additions and 20 deletions

View file

@ -72,10 +72,6 @@ public class DiscordCommands : ApplicationCommandModule
[SlashCommand("ban", "Ban a user")] [SlashCommand("ban", "Ban a user")]
public async Task Ban(InteractionContext ctx, [Option("username", "User to ban")] string username, public async Task Ban(InteractionContext ctx, [Option("username", "User to ban")] string username,
[Option("vm", "VM to ban from")] [ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) { [Option("vm", "VM to ban from")] [ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) {
if (ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (Program.VMs.All(v => v.Config.Name != VM)) { if (Program.VMs.All(v => v.Config.Name != VM)) {
await ctx.CreateResponseAsync("No VM by that name found."); await ctx.CreateResponseAsync("No VM by that name found.");
return; return;
@ -84,6 +80,10 @@ public class DiscordCommands : ApplicationCommandModule
if (!vm.cvm.ConnectedToVM) { if (!vm.cvm.ConnectedToVM) {
await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}"); await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}");
} }
if (vm.Config.DiscordMods?.Contains(ctx.Member.Id) != true && ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (vm.cvm.Users.All(u => u.Username != username)) { if (vm.cvm.Users.All(u => u.Username != username)) {
await ctx.CreateResponseAsync($"User {username} not found on {VM}"); await ctx.CreateResponseAsync($"User {username} not found on {VM}");
return; return;
@ -95,10 +95,6 @@ public class DiscordCommands : ApplicationCommandModule
[SlashCommand("kick", "Kick a user")] [SlashCommand("kick", "Kick a user")]
public async Task Kick(InteractionContext ctx, [Option("username", "User to kick")] string username, public async Task Kick(InteractionContext ctx, [Option("username", "User to kick")] string username,
[Option("vm", "VM to kick from"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) { [Option("vm", "VM to kick from"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) {
if (ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (Program.VMs.All(v => v.Config.Name != VM)) { if (Program.VMs.All(v => v.Config.Name != VM)) {
await ctx.CreateResponseAsync("No VM by that name found."); await ctx.CreateResponseAsync("No VM by that name found.");
return; return;
@ -107,6 +103,10 @@ public class DiscordCommands : ApplicationCommandModule
if (!vm.cvm.ConnectedToVM) { if (!vm.cvm.ConnectedToVM) {
await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}"); await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}");
} }
if (vm.Config.DiscordMods?.Contains(ctx.Member.Id) != true && ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (vm.cvm.Users.All(u => u.Username != username)) { if (vm.cvm.Users.All(u => u.Username != username)) {
await ctx.CreateResponseAsync($"User {username} not found on {VM}"); await ctx.CreateResponseAsync($"User {username} not found on {VM}");
return; return;
@ -119,10 +119,6 @@ public class DiscordCommands : ApplicationCommandModule
public async Task Reboot(InteractionContext ctx, public async Task Reboot(InteractionContext ctx,
[Option("vm", "VM to reboot"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) [Option("vm", "VM to reboot"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM)
{ {
if (ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (Program.VMs.All(v => v.Config.Name != VM)) { if (Program.VMs.All(v => v.Config.Name != VM)) {
await ctx.CreateResponseAsync("No VM by that name found."); await ctx.CreateResponseAsync("No VM by that name found.");
return; return;
@ -131,18 +127,18 @@ public class DiscordCommands : ApplicationCommandModule
if (!vm.cvm.ConnectedToVM) { if (!vm.cvm.ConnectedToVM) {
await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}"); await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}");
} }
if (vm.Config.DiscordMods?.Contains(ctx.Member.Id) != true && ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
await vm.cvm.Reboot(); await vm.cvm.Reboot();
await ctx.CreateResponseAsync($"Successfully rebooted {vm}"); await ctx.CreateResponseAsync($"Successfully rebooted {vm}");
} }
[SlashCommand("restpre", "Restore a VM")] [SlashCommand("restore", "Restore a VM")]
public async Task Restore(InteractionContext ctx, public async Task Restore(InteractionContext ctx,
[Option("vm", "VM to restore"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM) [Option("vm", "VM to restore"), ChoiceProvider(typeof(VMAutocompleteProvider))] string VM)
{ {
if (ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
if (Program.VMs.All(v => v.Config.Name != VM)) { if (Program.VMs.All(v => v.Config.Name != VM)) {
await ctx.CreateResponseAsync("No VM by that name found."); await ctx.CreateResponseAsync("No VM by that name found.");
return; return;
@ -151,6 +147,10 @@ public class DiscordCommands : ApplicationCommandModule
if (!vm.cvm.ConnectedToVM) { if (!vm.cvm.ConnectedToVM) {
await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}"); await ctx.CreateResponseAsync($"Not currently connected to {vm.Config.Name}");
} }
if (vm.Config.DiscordMods?.Contains(ctx.Member.Id) != true && ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) {
await ctx.CreateResponseAsync("You do not have permission to use that command.");
return;
}
await vm.cvm.Restore(); await vm.cvm.Restore();
await ctx.CreateResponseAsync($"Successfully restored {vm}"); await ctx.CreateResponseAsync($"Successfully restored {vm}");
} }
@ -158,17 +158,22 @@ public class DiscordCommands : ApplicationCommandModule
[SlashCommand("getip", "Get the IP address of a user")] [SlashCommand("getip", "Get the IP address of a user")]
public async Task GetIP(InteractionContext ctx, [Option("username", "Username to grab IP from")] string username) public async Task GetIP(InteractionContext ctx, [Option("username", "Username to grab IP from")] string username)
{ {
if (ctx.Member.Roles.All(r => !Program.Config.Discord.ModRoles.Contains(r.Id))) { VM[] vms;
if (ctx.Member.Roles.Any(r => Program.Config.Discord.ModRoles.Contains(r.Id)))
vms = Program.VMs;
else
vms = Program.VMs.Where(v => v.Config.DiscordMods?.Contains(ctx.Member.Id) == true).ToArray();
if (vms.Length == 0) {
await ctx.CreateResponseAsync("You do not have permission to use that command."); await ctx.CreateResponseAsync("You do not have permission to use that command.");
return; return;
} }
if (Program.VMs.All(v => v.cvm.Users.All(u => u.Username != username))) { if (vms.All(v => v.cvm.Users.All(u => u.Username != username))) {
await ctx.CreateResponseAsync($"{username} not found on any VM"); await ctx.CreateResponseAsync($"{username} not found on any VM");
return; return;
} }
await ctx.DeferAsync(); await ctx.DeferAsync();
List<DiscordEmbed> IPEmbeds = new(); List<DiscordEmbed> IPEmbeds = new();
foreach (var vm in Program.VMs) foreach (var vm in vms)
{ {
if (!vm.cvm.ConnectedToVM || vm.cvm.Users.All(u => u.Username != username)) continue; if (!vm.cvm.ConnectedToVM || vm.cvm.Users.All(u => u.Username != username)) continue;
var ip = await vm.cvm.GetIP(username); var ip = await vm.cvm.GetIP(username);

View file

@ -32,6 +32,7 @@ public class ConfigVM
public string URL { get; set; } public string URL { get; set; }
public string Node { get; set; } public string Node { get; set; }
public string Password { get; set; } public string Password { get; set; }
public ulong[]? DiscordMods { get; set; }
} }
public class ConfigDatabase public class ConfigDatabase