Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Parameter precondition attribute for simplifying performing hierarchical operations within a guild #2906

Merged

Conversation

zobweyt
Copy link
Contributor

@zobweyt zobweyt commented Apr 13, 2024

Description

The attribute like this one was really needed by the community. I often saw discussions about how to handle hierarchical operations correctly, but it was all too cumbersome and everyone had to create it themselves.

This is very useful when you need to perform hierarchical operations within a guild, such as managing roles or users. Here's a brief example of how you can check the hierarchy now:

[RequireContext(ContextType.Guild)]
[RequireBotPermission(GuildPermission.ManageRoles)]
[RequireUserPermission(GuildPermission.ManageRoles)]
[SlashCommand("role", "Adds a role to a user within a guild.")]
public async Task RoleAsync(IGuildUser user, [DoHierarchyCheck] IRole role)
{
    if (user.RoleIds.Any(id => id == role.Id))
    {
        await RespondAsync($"{user.Mention} already has {role.Mention}!");
        return;
    }

    await user.AddRoleAsync(role);
    await RespondAsync($"Added {role.Mention} to {user.Mention}.");
}

Changes

  • Add DoHierarchyCheckAttribute to the Discord.Interactions. (9c3cc4b)
  • Update bundled preconditions docs for the Discord.Interactions. (9c3cc4b)
  • Add DoHierarchyCheckAttribute to the Discord.Commands. (0173b20)
  • Update bundled preconditions docs for the Discord.Commands. (0173b20)

Related Issues

Now, there will be less issues like:

@zobweyt zobweyt changed the title Feature/do hierarchy check attribute [Feature] Parameter precondition attribute for simplifying performing hierarchical operations within a guild Apr 13, 2024
@Misha-133 Misha-133 self-requested a review April 15, 2024 18:11
@Misha-133 Misha-133 merged commit 1a5cba8 into discord-net:dev May 11, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants