diff --git a/Roles/AddRole.cs b/Roles/AddRole.cs new file mode 100644 index 0000000..c1ea4b8 --- /dev/null +++ b/Roles/AddRole.cs @@ -0,0 +1,57 @@ +using System.IO.Compression; +using System.Runtime.CompilerServices; +using Discord; +using Discord.Commands; +using Discord.Rest; +using Discord.WebSocket; +using Microsoft.Win32.SafeHandles; +using PluginManager.Interfaces; +using PluginManager.Others; +using Roles.Internals; + +namespace Roles +{ + public class AddRole : DBCommand + { + public string Command => "addrole"; + + public List Aliases => new() { "ar", "addr", "roleadd" }; + + public string Description => "Role options"; + + public string Usage => "addrole [user1] [user2] ... [role1] [role2] ..."; + + public bool canUseDM => false; + + public bool canUseServer => true; + + public bool requireAdmin => true; + + public async void Execute(SocketCommandContext context, SocketMessage message, DiscordSocketClient client, bool isDM) + { + if (message.MentionedUsers.Count == 0 || message.MentionedRoles.Count == 0) + { + await context.Channel.SendMessageAsync($"Invalid invocation\nUsage:{Usage}"); + return; + } + + try + { + var users = message.MentionedUsers; + var roles = message.MentionedRoles as IEnumerable; + + foreach (var user in users) + { + SocketGuildUser? usr = context.Client.GetUser(user.Username, user.Discriminator) as SocketGuildUser; + if (usr is null) + throw new Exception("User is null"); + await usr.AddRolesAsync(roles); + } + } + catch (Exception ex) + { + await context.Channel.SendMessageAsync(ex.Message); + } + } + } +} diff --git a/Roles/Internals/RoleManagement.cs b/Roles/Internals/RoleManagement.cs new file mode 100644 index 0000000..edcf616 --- /dev/null +++ b/Roles/Internals/RoleManagement.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord; +using Discord.WebSocket; +using PluginManager.Others; + +namespace Roles.Internals +{ + internal static class RoleManagement + { + internal static async void AddRole(this SocketGuildUser user, string roleName) + { + string role = roleName; + IRole? r = user.Guild.Roles.FirstOrDefault(rl => rl.Name == role || rl.Mention == role); + if (r is null) + throw new Exception("The role does not exist"); + + try + { + await user.AddRoleAsync(r); + } + catch (Exception ex) + { + if (ex.Message.Contains("Permission", StringComparison.CurrentCultureIgnoreCase)) + throw new Exception("Insufficient permissions"); + } + } + + internal static async void AddRole(this SocketGuildUser user, IRole role) + { + try + { + await user.AddRoleAsync(role); + } + catch (Exception ex) + { + if (ex.Message.Contains("Permission", StringComparison.CurrentCultureIgnoreCase)) + throw new Exception("Insufficient permissions"); + } + } + + internal static async void AddRoles(this SocketGuildUser user, string[] roleNames) + { + foreach (string rolename in roleNames) + { + string roleName = rolename; + IRole? r = user.Guild.Roles.FirstOrDefault(rl => rl.Name == roleName || rl.Mention == roleName); + if (r is null) + throw new Exception("The role does not exist"); + + try + { + await user.AddRoleAsync(r); + } + catch (Exception ex) + { + if (ex.Message.Contains("Permission", StringComparison.CurrentCultureIgnoreCase)) + throw new Exception("Insufficient permissions"); + } + } + } + + internal static async void AddRoles(this SocketGuildUser user, IEnumerable roles) + { + try + { + await user.AddRolesAsync(roles); + } + catch (Exception ex) + { + if (ex.Message.Contains("Permission", StringComparison.CurrentCultureIgnoreCase)) + throw new Exception("Insufficient permissions"); + } + } + } +} diff --git a/Roles/Roles.csproj b/Roles/Roles.csproj new file mode 100644 index 0000000..b468d70 --- /dev/null +++ b/Roles/Roles.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + +