From 5c526004bd1bb6bc6fa4c169958700847bbb4527 Mon Sep 17 00:00:00 2001 From: Marro64 Date: Sat, 13 Dec 2025 19:16:51 +0100 Subject: [PATCH] GiveRoleToAllUsers --- FluxPoseDiscordBot/Program.cs | 42 ++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/FluxPoseDiscordBot/Program.cs b/FluxPoseDiscordBot/Program.cs index 4e97f0f..163df3e 100644 --- a/FluxPoseDiscordBot/Program.cs +++ b/FluxPoseDiscordBot/Program.cs @@ -12,6 +12,7 @@ using NetCord.Rest; using NetCord.Services.ApplicationCommands; using NetCord.Services.ComponentInteractions; using System.ComponentModel; +using System.Diagnostics; using System.Linq; public static class Program @@ -59,7 +60,7 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule GetRolesCommand() + public async Task GetRolesCommand() { await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage()); @@ -69,42 +70,41 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule message.WithContent($"Found no roles :(")); } - return $"Roles in the server:\n{String.Join("\n", roles.Select(role => $"- {role.Id}: {role.Name}"))}"; + await Context.Interaction.ModifyResponseAsync(message => message.WithContent($"Roles in the server:\n{String.Join("\n", roles.Select(role => $"- {role.Id}: {role.Name}"))}")); } [SlashCommand("giveroletoallusers", "Give role to all users on the server")] - public async Task GiveRoleToAllUsersCommand(string roleId) + public async Task GiveRoleToAllUsersCommand(string roleId) { await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage()); var parsed = ulong.TryParse(roleId, out ulong roleIdParsed); if (!parsed) { - return "Invalid roleId"; + await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId")); } - await GiveRoleToAllUsers(roleIdParsed); - - return "Done."; + _ = GiveRoleToAllUsers(roleIdParsed); } [SlashCommand("removerolefromallusers", "Remove role from all users on the server")] - public async Task RemoveRoleFromAllUsersCommand(string roleId) + public async Task RemoveRoleFromAllUsersCommand(string roleId) { await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage()); var parsed = ulong.TryParse(roleId, out ulong roleIdParsed); if (!parsed) { - return "Invalid roleId"; + await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId")); + return; } await RemoveRoleFromAllUsers(roleIdParsed); - return "Done."; + await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Done.")); } [SlashCommand("defer", "Defer")] @@ -195,9 +195,25 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule message.WithContent($"Applying role for {users.Count} users.")); + + int count = 0; foreach (var user in users) { - await client.Rest.AddGuildUserRoleAsync(GuildId, user.Id, roleId); + count++; + try + { + await client.Rest.AddGuildUserRoleAsync(GuildId, user.Id, roleId); + Debug.WriteLine($"{count}/{total} Applied role to user {user.Username} ({user.Id})"); + } + catch (Exception ex) + { + Debug.WriteLine($"{count}/{total} Failed to apply role to user {user.Username} ({user.Id}). Error:"); + Debug.WriteLine(ex); + } + + await Task.Delay(100); } } @@ -207,7 +223,7 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule