GiveRoleToAllUsers
This commit is contained in:
@@ -12,6 +12,7 @@ using NetCord.Rest;
|
|||||||
using NetCord.Services.ApplicationCommands;
|
using NetCord.Services.ApplicationCommands;
|
||||||
using NetCord.Services.ComponentInteractions;
|
using NetCord.Services.ComponentInteractions;
|
||||||
using System.ComponentModel;
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public static class Program
|
public static class Program
|
||||||
@@ -59,7 +60,7 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule<Appl
|
|||||||
}
|
}
|
||||||
|
|
||||||
[SlashCommand("getroles", "Get all roles on the server")]
|
[SlashCommand("getroles", "Get all roles on the server")]
|
||||||
public async Task<string> GetRolesCommand()
|
public async Task GetRolesCommand()
|
||||||
{
|
{
|
||||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||||
|
|
||||||
@@ -69,42 +70,41 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule<Appl
|
|||||||
|
|
||||||
if (roles.Count == 0)
|
if (roles.Count == 0)
|
||||||
{
|
{
|
||||||
return $"Found no roles :(";
|
await Context.Interaction.ModifyResponseAsync(message => 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")]
|
[SlashCommand("giveroletoallusers", "Give role to all users on the server")]
|
||||||
public async Task<string> GiveRoleToAllUsersCommand(string roleId)
|
public async Task GiveRoleToAllUsersCommand(string roleId)
|
||||||
{
|
{
|
||||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||||
|
|
||||||
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
||||||
if (!parsed)
|
if (!parsed)
|
||||||
{
|
{
|
||||||
return "Invalid roleId";
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId"));
|
||||||
}
|
}
|
||||||
|
|
||||||
await GiveRoleToAllUsers(roleIdParsed);
|
_ = GiveRoleToAllUsers(roleIdParsed);
|
||||||
|
|
||||||
return "Done.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[SlashCommand("removerolefromallusers", "Remove role from all users on the server")]
|
[SlashCommand("removerolefromallusers", "Remove role from all users on the server")]
|
||||||
public async Task<string> RemoveRoleFromAllUsersCommand(string roleId)
|
public async Task RemoveRoleFromAllUsersCommand(string roleId)
|
||||||
{
|
{
|
||||||
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
await Context.Interaction.SendResponseAsync(InteractionCallback.DeferredMessage());
|
||||||
|
|
||||||
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
var parsed = ulong.TryParse(roleId, out ulong roleIdParsed);
|
||||||
if (!parsed)
|
if (!parsed)
|
||||||
{
|
{
|
||||||
return "Invalid roleId";
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Invalid roleId"));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await RemoveRoleFromAllUsers(roleIdParsed);
|
await RemoveRoleFromAllUsers(roleIdParsed);
|
||||||
|
|
||||||
return "Done.";
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent("Done."));
|
||||||
}
|
}
|
||||||
|
|
||||||
[SlashCommand("defer", "Defer")]
|
[SlashCommand("defer", "Defer")]
|
||||||
@@ -195,9 +195,25 @@ public class SlashCommands(GatewayClient client) : ApplicationCommandModule<Appl
|
|||||||
{
|
{
|
||||||
var users = await GetAllUsersFromGuild();
|
var users = await GetAllUsersFromGuild();
|
||||||
|
|
||||||
|
int total = users.Count;
|
||||||
|
await Context.Interaction.ModifyResponseAsync(message => message.WithContent($"Applying role for {users.Count} users."));
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
foreach (var user in users)
|
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<Appl
|
|||||||
|
|
||||||
foreach (var user in users)
|
foreach (var user in users)
|
||||||
{
|
{
|
||||||
if (user.RoleIds.Contains(roleId))
|
if (!user.RoleIds.Contains(roleId))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user