PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Lua Chat System — Server API — ChatChannel

Lua Chat System — Server API — ChatChannel

Oct 31 2018, 12:13 AM PST 10 min

< Return to Lua Chat System

ChatChannel is an object that stores data about a single channel, which is a means by which messages can be exchanged between ChatSpeakers. It also has access permission properties that determine the visibility of messages along with whether users may join or leave the channel manually (using /join or /leave commands).

By default, each player has a ChatSpeaker that is automatically added to the “All” and “System” chat channels (although, “System” is read only). If the player is on a Team (Player/Team is set), they will also have access to a channel for only that Team. Whisper messages use a channel

Properties

Name

WelcomeMessage

  • Type: string
  • Description: A message to display when a player joins the channel.

Joinable

  • Type: bool
  • Description: Determines whether a player may manually join a channel using the /join command. A player can still be added to a channel using ChatSpeaker:JoinChannel() or other means even if this property is false.

Leavable

  • Type: bool
  • Description: Determines whether a player may manually leave a channel using the /leave command. A player can still be removed from a channel using ChatSpeaker:LeaveChannel() or other means even if this property is false.

AutoJoin

  • Type: bool
  • Description: Determines whether a player’s ChatSpeaker will automatically join the channel upon joining the game. Non-player speakers will not automatically join channels, even when this property is true (ChatSpeaker:JoinChannel() may be used to do this).

Private

  • Type: bool
  • Description: Determines whether the channel will be included in the list of channels returned by ChatService:GetChannelList(). This for whisper chats and team chats.

Methods

KickSpeaker

  • Parameters: string speakerName, string reason = nil
  • Description: Removes the speaker with the given speakerName from the channel, sending a message to both the player and the channel from which the player was kicked. If a reason is provided, the reason will be included in the message.
  • Returns: void

MuteSpeaker

  • Parameters: string speakerName, string reason = nil, int duration = 0
  • Description: Mutes the speaker with the given speakerName in the channel for a duration specified in seconds. If duration is nil or 0, the mute is indefinite. If reason is provided, then a message will be sent to the channel with the reason included.
  • Returns: void

UnmuteSpeaker

  • Parameters: string speakerName
  • Description: Unmutes the speaker with the given speakerName in the channel.
  • Returns: void

IsSpeakerMuted

  • Parameters: string speakerName
  • Description: Describes whether the speaker with the given speakerName is presently muted in the channel.
  • Returns: bool

GetSpeakersList

  • Parameters: None
  • Description: Returns a list containing all if the names of the ChatSpeaker currently in the channel.
  • Returns: array<string>

SendSystemMessage

  • Parameters: string message
  • Description: Sends a message from the “System” ChatSpeaker{ChatSpeaker] to the channel.
  • Returns: void

RegisterFilterMessageFunction

  • Parameters: string functionId, function func
  • Description: Registers a filter function, func, identified by functionId to the channel. The filter function will be called with the ChatSpeaker, the ChatMessage, and the string name of the channel the message originated in. Changes to the message will persist and will be displayed after filtering.
  • Returns: void
  • Example:
-- Paste this example into a ModuleScript within the ChatModules folder.
-- This example filters a keyword, and if successful, sets the chatColor of the message
local functionId = "greenText"
local keyword = "#green"
local chatColor = Color3.new(0, 1, 0) -- green

local function doFilter(speaker, messageObject, channelName)
	-- Check if the message contains the keyword
	local start, finish = string.find(messageObject.Message, keyword)
	if start and finish then
		-- Remove (filter) the keyword from the message, also setting the ChatColor
		messageObject.Message = string.gsub(messageObject.Message, keyword, "")
		messageObject.ExtraData.ChatColor = chatColor
	end
end

local function runChatModule(ChatService)
	-- Create a channel and register the filter function
	local testChannel = ChatService:AddChannel("TestChannel")
	testChannel:RegisterFilterMessageFunction(functionId, doFilter)
end
 
return runChatModule

UnregisterFilterMessageFunction

  • Parameters: string functionId
  • Description: Unregisters a filter function (registered by RegisterFilterMessageFunction) given its identifier, functionId.
  • Returns: void

RegisterProcessCommandsFunction

  • Parameters: string functionId, function func
  • Description: Registers a process command function, func, identified by functionId to the chat. Before a message is filtered, it will pass through func (and other functions registered by this). The function func should check whether the message invokes a command. If so, perform the action of the command and return true. Returning true indicates the message was indeed a command and should not be displayed. The function can be unregistered using UnregisterProcessCommandsFunction.
  • Returns: void
  • Example:
-- Paste this example into a ModuleScript within the ChatModules folder.
local functionId = "getPizza"
local command = "/pizza"
local toolId = 22596452 -- Pepperoni pizza slice gear
local function processCommand(speakerName, message, channelName)
	if string.sub(message, 1, command:len()) == command then
		local model = game:GetService("InsertService"):LoadAsset(toolId)
		local tool = model:GetChildren()[1]
		local speaker = ChatService:GetSpeaker(speakerName)
		local player = speaker:GetPlayer()
		tool.Parent = player.Backpack
		return true
	end
	return false
end

local function runChatModule(ChatService)
	ChatService:RegisterProcessCommandsFunction(functionId, processCommand)
end
 
return runChatModule

UnregisterProcessCommandsFunction

  • Parameters: string functionId
  • Description: Unregisters a command processor (registered by RegisterProcessCommandsFunction) given the identifier, functionId.
  • Returns: void

Events

MessagePosted

  • Parameters: ChatMessage message
  • Description: Fires when a message is posted in the channel.

SpeakerJoined

  • Parameters: string speakerName
  • Description: Fires when a ChatSpeaker joins the channel.

SpeakerLeft

  • Parameters: string speakerName
  • Description: Fires when a ChatSpeaker leaves the channel.

SpeakerMuted

  • Parameters: string speakerName, string reason, int duration = 0
  • Description: Fires when a ChatSpeaker has been muted in the channel.

SpeakerUnmuted