public async Task RegisterAsync( [Remainder] [Summary("The role to register to.")] IRole targetRole) { var user = Context.User as IGuildUser; if (user.RoleIds.Any(x => x == targetRole.Id)) { await ReplyAsync("You're already registered to that role."); return; } if (!await _designatedRoleService.RoleHasDesignationAsync(Context.Guild.Id, targetRole.Id, DesignatedRoleType.Pingable)) { await ReplyAsync("Can't register to a role that isn't pingable."); return; } await user.AddRoleAsync(targetRole); await ReplyAsync($"Registered {user.Mention} to {Format.Bold(targetRole.Name)}."); }
public async Task <bool> Handle(MentionCommand request, CancellationToken cancellationToken) { if (request.Role is null) { throw new ArgumentNullException(nameof(request.Role)); } if (request.Channel is null) { throw new ArgumentNullException(nameof(request.Channel)); } var message = request.Message ?? string.Empty; if (request.Role.IsMentionable) { await SendMessage(); return(true); } if (!_authorizationService.HasClaim(AuthorizationClaim.MentionRestrictedRole)) { await request.Channel.SendMessageAsync("You cannot mention roles!"); return(false); } if (!await _designatedRoleService.RoleHasDesignationAsync( request.Role.Guild.Id, request.Role.Id, DesignatedRoleType.RestrictedMentionability, cancellationToken)) { await request.Channel.SendMessageAsync("You cannot mention this role!"); return(false); } try { await request.Role.ModifyAsync(x => x.Mentionable = true); await SendMessage(); } finally { await request.Role.ModifyAsync(x => x.Mentionable = false); } return(true); Task SendMessage() => request.Channel.SendMessageAsync($"{request.Role.Mention} {message}"); }
public async Task <bool> MentionRole(IRole role, IMessageChannel channel, string message = null) { if (role is null) { throw new ArgumentNullException(nameof(role)); } if (channel is null) { throw new ArgumentNullException(nameof(channel)); } if (message is null) { message = string.Empty; } if (role.IsMentionable) { await channel.SendMessageAsync($"{role.Mention} {message}"); return(true); } _authorizationService.RequireClaims(AuthorizationClaim.MentionRestrictedRole); if (await _designatedRoleService.RoleHasDesignationAsync(role.Guild.Id, role.Id, DesignatedRoleType.RestrictedMentionability, CancellationToken.None) == false) { await channel.SendMessageAsync($"Sorry, **{role.Name}** hasn't been designated as mentionable."); return(false); } //Set the role to mentionable, immediately mention it, then set it //to unmentionable again await role.ModifyAsync(x => x.Mentionable = true); //Make sure we set the role to unmentionable again no matter what try { await channel.SendMessageAsync($"{role.Mention} {message}"); } finally { await role.ModifyAsync(x => x.Mentionable = false); } return(true); }