Skip to content

Channel management

Channels are one of the core components of social. In the API, channels are represented by the ChatChannel class. Channels can be retrieved and managed through the channel registry.

Getting a ChatChannel by its name
String channelName = "global"; // Channel name
IdentifiedRegistryKey channelKey = RegistryKey.identified(channelName); // We wrap the channel name in a registry key
Optional<ChatChannel> optionalChannel = Social.registries().channels().value(channelKey);

The built-in ChatManager provides additional helper methods for common channel operations.

Group channels

Group channels can be retrieved using their unique numeric code.

Getting a GroupChatChannel from its code
final int code = 491350;
Optional<GroupChatChannel> optionalChannel = Social.get().getChatManager().groupChannelByCode(code);

You can also retrieve the group channel associated with a specific SocialUser.

Getting a GroupChatChannel from a SocialUser
Optional<GroupChatChannel> optionalChannel = Social.get().getChatManager().groupChannelByUser(user);

Registering a custom channel

Although channels are typically configured by administrators, plugins may register channels programmatically when required.

Formatter

To create a channel, you must first define a channel format using ChatFormatBuilder. The format defines how messages from the channel will be rendered.

Building the channel format
ChatFormatBuilder format = ChatFormatBuilder.empty()
    .append(text("$(clickable_channel_icon) "))
    .append(text("$(formatted_nickname) "))
    .append(text(">"));

Rendered example:

(channel icon) (user nickname) > (message)

Registry

Once a format has been defined, the channel can be created and registered in the channel registry:

Registering a ChatChannel (basic)
String name = "example";
ChatChannel channel = ChatChannel.builder(name, format).build();
IdentifiedRegistryKey key = RegistryKey.identified(name);

Social.registries().channels().register(key, channel);

Advanced channel configuration

The ChatChannel builder supports additional configuration options:

Registering a ChatChannel (advanced)
String name = "example";
IdentifiedRegistryKey key = RegistryKey.identified(name);
ChatChannel channel = ChatChannel.builder(name, format)
    .alias("example-alias") // Alternative name for the channel
    .commands("example") // Commands associated with the channel
    .icon(text("[E]")) // Channel icon
    .description(text("A custom channel!")) // Channel description
    .color(NamedTextColor.YELLOW) // Channel color used in UI components
    .permission("channel.example") // Permission required to access the channel
    .textColor(NamedTextColor.WHITE) // Default message text color
    .joinByDefault(true) // Whether users automatically join the channel
    .supportedFeature(ChatRendererFeature.replies(2)) // Enables the replies feature
    .build();

Social.registries().channels().register(key, channel);

Managing channel members

After creating a channel, users can be added or removed as members.

Adding members to a channel
channel.addMember(user);

// Alternatively, using a UUID:
channel.addMember(user.getUuid());

It is also possible to remove members with a similar method:

Removing members from a channel
channel.removeMember(user);

// Alternatively, using a UUID:
channel.removeMember(user.getUuid());

You can check whether a SocialUser is a member of a channel:

Checking whether a SocialUser is a member of a channel
boolean isMember = channel.isMember(user);