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.
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.
final int code = 491350;
Optional<GroupChatChannel> optionalChannel = Social.get().getChatManager().groupChannelByCode(code);
You can also retrieve the group channel associated with a specific 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.
ChatFormatBuilder format = ChatFormatBuilder.empty()
.append(text("$(clickable_channel_icon) "))
.append(text("$(formatted_nickname) "))
.append(text(">"));
Rendered example:
Registry
Once a format has been defined, the channel can be created and registered in the channel registry:
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:
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.
channel.addMember(user);
// Alternatively, using a UUID:
channel.addMember(user.getUuid());
It is also possible to remove members with a similar method:
channel.removeMember(user);
// Alternatively, using a UUID:
channel.removeMember(user.getUuid());
You can check whether a SocialUser is a member of a channel: