Search in sources :

Example 1 with RoleBuilder

use of sx.blah.discord.util.RoleBuilder in project Discord4J by Discord4J.

the class RoleBot method handle.

/**
 * Client is ready to interact with Discord.
 * @see ReadyBot
 */
@Override
public void handle(ReadyEvent event) {
    try {
        // Gets the first guild the bot is a member of. (NOTE: This is only for demonstration. Getting guilds in this way is NOT recommended. Use IDs or events instead.)
        IGuild guild = event.getClient().getGuilds().get(0);
        // Instantiate a RoleBuilder which will aide in the creation of the role.
        RoleBuilder roleBuilder = new RoleBuilder(guild);
        // Set the new role's name
        roleBuilder.withName("Awesome Role");
        // Set the new role's color
        roleBuilder.withColor(Color.GREEN);
        // Make the new role display separately from others in Discord.
        roleBuilder.setHoist(true);
        // Allow this role to be mentionable in chat.
        roleBuilder.setMentionable(true);
        // Assign the Administrator permission to this role.
        roleBuilder.withPermissions(EnumSet.of(Permissions.ADMINISTRATOR));
        // Add the role to the guild in Discord.
        IRole role = roleBuilder.build();
        // Gets the user of the bot
        IUser ourUser = event.getClient().getOurUser();
        // Assigns our new role to the bot. NOTE: This will make the bot's ONLY role our role.
        guild.editUserRoles(ourUser, new IRole[] { role });
    } catch (MissingPermissionsException | RateLimitException | DiscordException e) {
        // Error occurred
        e.printStackTrace();
    }
}
Also used : IRole(sx.blah.discord.handle.obj.IRole) RateLimitException(sx.blah.discord.util.RateLimitException) DiscordException(sx.blah.discord.util.DiscordException) IUser(sx.blah.discord.handle.obj.IUser) MissingPermissionsException(sx.blah.discord.util.MissingPermissionsException) IGuild(sx.blah.discord.handle.obj.IGuild) RoleBuilder(sx.blah.discord.util.RoleBuilder)

Aggregations

IGuild (sx.blah.discord.handle.obj.IGuild)1 IRole (sx.blah.discord.handle.obj.IRole)1 IUser (sx.blah.discord.handle.obj.IUser)1 DiscordException (sx.blah.discord.util.DiscordException)1 MissingPermissionsException (sx.blah.discord.util.MissingPermissionsException)1 RateLimitException (sx.blah.discord.util.RateLimitException)1 RoleBuilder (sx.blah.discord.util.RoleBuilder)1