#How Do I get a SocketRole by an exsisting role's id

1 messages · Page 1 of 1 (latest)

molten cairn
#

I am trying to make a bot for my friend. I want to add mod commands to it that can only be used with a certian role. is there a way to do that?

mellow lichen
#

You could create a custom precondition that checks for that role, such as:

public class RequireSupportRole : PreconditionAttribute
{

    public override async Task<PreconditionResult> CheckRequirementsAsync(IInteractionContext context, ICommandInfo commandInfo, IServiceProvider services)
    {
        
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();

        var moderatorRoleId = config.GetValue<ulong>("Roles:Moderator");
        
        var guildUser = context.User as IGuildUser;
        if (guildUser == null)
            return PreconditionResult.FromError("This command cannot be executed outside of a guild.");

        var guild = guildUser.Guild;
        if (guild.Roles.All(r => r.Id != moderatorRoleId))
            return PreconditionResult.FromError(
                $"The guild does not have the role required to access this command.");

        return guildUser.RoleIds.Any(rId => rId == moderatorRoleId)
            ? PreconditionResult.FromSuccess()
            : PreconditionResult.FromError("You do not have the sufficient role required to access this command.");
    }
}
#

And then in your appsettings.json define a moderator role