处理经验文本聊天的服务,包括managing channels、装饰消息、过滤文本、creating commands、开发自定义聊天界面。
了解更多信息,请参阅TextChatService 概览。
为进一步自定义,TextChatService有以下单独孩子:
概要
属性
决定是否启用聊天翻译。
决定是否完全启用 TextChatService 或恢复到遗产聊天系统。
决定是否 TextChatService 应该创建默认 TextChatCommands。
决定是否 TextChatService 应该创建默认 TextChannels。
方法
在提供的部分或玩家角色上方显示聊天泡泡。
决定用户是否有权在体验中聊天。
决定是否两个用户之间会收到消息。
决定用户是否有权直接与其他用户聊天在基于父母控制设置的体验中。
活动
当 TextChatService:DisplayBubble() 被调用时发生火灾。
当在客户端上调用 TextChannel:DisplaySystemMessage() 时,或当客户端从服务器收到有效的 TextChannel:SendAsync() 响应时,发生火灾。
当发送客户端调用 TextChannel:SendAsync() 时发生火灾
回调
当泡泡聊天即将显示时调用。
当新消息即将显示在聊天窗口时调用。这只能在客户端实现。
当 TextChatService 收到来自外部的消信息时调用。
属性
ChatVersion
决定是否完全启用 TextChatService 或恢复到遗产聊天系统。将此属性设置为 Enum.ChatVersion.LegacyChatService 有效禁用 TextChatService。
CreateDefaultCommands
决定是否 TextChatService 应该创建默认 TextChatCommands。
如果真实,以下 TextChatCommands 将被创建并放入 Folder 命名为 TextChatCommands 的 TextChatService 内:
<th>主要别名</th><th>次要别名</th><th>描述</th><th>使用示例</th></tr></thead><tbody><tr><td><b>RBX清除命令</b></td><td>清除</td><td>cls</td><td>清除本地用户的聊天日志。</td><td><code>/cls</code></td></tr><tr><td><b>RBXConsoleCommand</b></td> <td>控制台</td><td /><td>打开开发者控制台。</td> <td><code>/console</code> ></td></tr><tr><td><b>RBX表情命令</b></td><td>动作</td><td>e</td><td>播放虚拟形象动作。</td><td><code>/e 跳舞</code></td></tr><tr><td><b>RBXHelp命令</b></td><td>帮助</td><td>?</td><td>显示聊天命令列表。</td><td><code>/帮助</code></td></tr><tr><td><b>RBXMute命令</b></td><td>静音</td><td>m</td><td>通过用户的 <code>Class.Player.Name|Name</code> 或 <code>Class.Player.DisplayName|DisplayName</code> 在所有 <code>Class.TextChannel|TextChannels</code> 中静音用户。</td><td><code>/m 用户名</code></td></tr><tr><td><b>RBX团队命令</b></td><td>团队</td><td>t</td><td>进入团队聊天模式,只有队友能看到消息。</td><td><code>/t</code></td></tr><tr><td><b>RBXUnmute命令</b></td><td>取消静音</td><td>um</td><td>通过用户的 <code>Class.Player.Name|Name</code> 或 <code>Class.Player.DisplayName|DisplayName</code> 在所有 <code>Class.TextChannel|TextChannels</code> 中取消静音用户。</td><td><code>/um 用户名</code></td></tr><tr><td><b>RBX版本命令</b></td><td>版本</td><td>v</td><td>显示聊天版本。</td><td><code>/版本</code></td></tr><tr><td><b>RBXWhisper命令</b></td><td>悄悄话</td><td>w</td><td>使用另一个 <code>Class.Player</code> 进入悄悄模式。</td><td><code>/w DisplayName</code> 或 <code>/w @Username</code></td></tr></tbody>
名称 |
---|
请注意,即使 TextChatCommands 是真的,您仍然可以编辑、创建 or 创作和删除 CreateDefaultCommands 。请注意,静音和解除静音命令适用于所有 TextChannels 。
CreateDefaultTextChannels
决定是否 TextChatService 应该创建默认 TextChannels。如果真实,一个名为 Folder 的 文本通道 在运行时内 TextChatService 创建,以包含下表中描述的 TextChannels 。每个默认通道都有描述的特殊行为应用于使用其内部绑定的 TextChannel.OnIncomingMessage 回调函数发送的消息,改变消息在通过通频道发送时的显示方式。回调在运行时自动分配(如果 存在)或在 创建时自动分配(如果 存在)。
<th>描述</th></tr></thead><tbody><tr><td><b>RBX一般</b></td><td><code>Class.TextChannel</code> 用于玩家消息。在聊天窗口中,消息被修改为 <code>Class.TextChatMessage.PrefixText|PrefixText</code> 收到一个 <a href="/ui/rich-text">富文本字体颜色标签</a> 以给聊天玩家他们的独特名称颜色。如果 <code>Class.Player.Team</code> 存在,那么 <code>Class.Team.TeamColor</code> 将被用作默认名称颜色的替代。</td></tr><tr><td><b>RBX系统</b></td><td><code>Class.TextChannel</code>用于系统消息。在聊天窗口中,消息被修改为默认情况下 <code>Class.TextChatMessage.Text</code> 获得浅灰色标签,或如果 <code>Class.TextChatMessage.Metadata</code> 包含单词 <code>“错误”</code> ,则为红色标签。</td></tr><tr><td><b>RBX团队[BrickColor]</b></td><td><code>Class.TextChannel</code> 用于团队特定玩家消息,当 <code>Class.Team.TeamColor|TeamColor</code> 由任何 <code>Class.Team</code> 服务中的任何 <code>Class.Teams</code> 使用时创建。创建的通道名称为 <b>RBXTeam</b> 后跟随 <code>Class.Team.TeamColor|TeamColor</code> 名称。例如,<b>RBXTeamCrimson</b>是一个<code>Class.TextChannel</code>,创建时有一个活跃的团队,其<code>Class.Team.TeamColor|TeamColor</code>属性是“深红色”的<code>Datatype.BrickColor</code>。在聊天窗口中,消息被修改,使<code>Class.TextChatMessage.PrefixText|PrefixText</code>颜色与<code>Class.Player.TeamColor|TeamColor</code>匹配,并以<b>[团队]</b>的形式添加。团队通道创建了<code>Class.TextSource|TextSources</code>,用于所有非<code>Class.Player.Neutral|Neutral</code>玩家与匹配的<code>Class.Team.TeamColor|TeamColor</code>一起使用团队聊天。如果没有与之相关的 <code>Class.Team.TeamColor|TeamColor</code> 的剩余团队,通道将被移除。</td></tr><tr><td><b>RBXWhisper:[userId1][userId2]</b></td><td><code>Class.TextChannel</code> 用于两名玩家之间的低语消息,创建时玩家使用另一名玩家的 <code>/whisper</code> 命令。例如 <b>RBXWhisper:2276836_505306092</b> 是为玩家 <code>Class.TextChannel</code> 的 <code>Class.Player.UserId|UserIds</code> 和 <b>2276836</b> 的 <b>505306092</b> 的。内部 Whisper 通道中有两个 <code>Class.TextSource|TextSources</code> 与各自的 <code>Class.Player.UserId|UserIds</code> 相关。在聊天窗口中,消息的颜色与在 <b>RBXGeneral</b> 和 <code>Class.TextChatMessage.PrefixText</code> 中的消息相同。Whisper 通道将被移除,当玩家离开体验时。</td></tr></tbody>
通道 |
---|
请注意,默认 TextChannel.OnIncomingMessage 回调可以覆盖。还注意到,即使 TextChannels 是真的,你也可以编辑、创创建 or 创作和删除 CreateDefaultTextChannels 。
使用 ChannelTabsConfiguration 将不同文本通道的消息分开到聊天窗口的不同选项卡中
方法
DisplayBubble
在提供的部分或玩家角色上方显示聊天泡泡,并发射参数在此方法中指定的 BubbleDisplayed 事件。如果您在角色内指定一个部分,例如头部,则可以显示非玩家角色(NPC)的泡泡。
请注意,此方法仅适用于在 LocalScript 中使用,或在 Script 中使用 RunContext 的 Enum.RunContext.Client 。
参数
返回
CanUserChatAsync
决定用户是否有权在体验中聊天。父母控制设置等因素可能会阻止用户发送消息。如果用户 ID 不在当前服务器中,则出错。请注意,该方法可以与当前所有玩家 UserIds 在一个 Script 中使用 RunContext 的 Enum.RunContext.Server 或 Enum.RunContext.Legacy 。该方法也可以在 LocalScript 中使用,但仅限本地玩家的 UserId 。
参数
返回
CanUsersChatAsync
决定是否两个用户之间会收到消息。不兼容的家长控制设置或被阻止的状态可能会阻止用户之间的消息传递 TextChannels 内部使用此结果来确定是否向用户发送消息。请注意,此方法仅适用于在 Script 中使用,其中 RunContext 为 Enum.RunContext.Server 或 Enum.RunContext.Legacy 。
参数
返回
CanUsersDirectChatAsync
决定用户是否有权直接与其他用户聊天在基于其家长控制设置的体验中。在使用时:
- 通信线由用户启动(不是由开发人员或游戏开发启动)
- 通信的访问被关闭并受限
直接聊天的例子是两个用户之间的低语频道。
您可以根据结果使用此方法来启用体验中的特定功能。
当创建一个直接聊天的 TextChannel 时,使用 TextChannel:SetDirectChatRequester() 来设置要求者用户ID,以便通道可以确定是否向用户发送消息。当 TextChannel.DirectChatRequester 不为 null 时,TextChannels 内部使用此结果来确定是否向用户发送消息。
local whoCanDirectChat = TextChatService:CanUsersDirectChatAsync(requesterUserId, { userId1, userId2 })if #whoCanDirectChat > 0 then-- 需求者的用户ID可以直接与 iPad1、 iPad2 或两者都聊天else-- 需求者的用户 ID 无法直接与 iPad1 或 iPad2 聊天end
请注意,此方法仅适用于在 Script 中使用,其中 RunContext 为 Enum.RunContext.Server 或 Enum.RunContext.Legacy 。
参数
将启动直接聊天请求的用户。如果要求者的用户ID不在当前服务器中,该方法将发生错误。
一个列表用户,要求者UserId想要直接聊天的用户。当前服务器中没有用户被忽略。
返回
一列可以参加直接聊天请求的用户列表。如果没有用户可以直接聊天与要求者的 userId,结果是一个空数组。
代码示例
This example checks if two users can chat, creates a new TextChannel, and adds them to it.
local TextChatService = game:GetService("TextChatService")
local directChatParticipants = TextChatService:CanUsersDirectChatAsync(userId1, { userId2 })
-- Check for eligible participants
if #directChatParticipants > 0 then
local directChannel = Instance.new("TextChannel")
directChannel.Parent = TextChatService
for _, participant in directChatParticipants do
directChannel:AddUserAsync(participant)
end
return directChannel
end
warn("Could not create TextChannel. Not enough eligible users.")
return nil
活动
MessageReceived
像 TextChannel.MessageReceived 一样,在客户端调用 TextChannel:DisplaySystemMessage() 时或从服务器收到有效的 TextChannel:SendAsync() 响应时发生。该事件仅在客户端发射。
如果服务器的 TextChannel.ShouldDeliverCallback 属性已绑定并返回 false , 客户端不会发射 TextChatService.MessageReceived .
使用 TextChatMessage 参数获取消息的 TextSource 和文本 (带 TextChatMessage.Text )。
TextChatMessage 参数是任何绑定到 TextChatService.OnIncomingMessage 或 TextChannel.OnIncomingMessage 的函数的最终结果。
参数
收到 TextChatMessage 。
SendingMessage
当发送客户端调用 TextChannel:SendAsync() 时发生火灾使用此来允许将占位符消息显示给用户,等待服务器回应到 TextChannel:SendAsync() 。
参数
来自 TextChatMessage 调用的 TextChannel:SendAsync() 。
回调
OnBubbleAdded
当泡泡聊天即将显示时调用。这只能在客户端实现。
使用此来自定义个人泡泡聊天消息。如果此回调返回 BubbleChatMessageProperties,这些属性将应用于相关泡泡,覆盖 BubbleChatConfiguration 属性。如果 UICorner 、 UIGradient 或 ImageLabel 被父辈在 BubbleChatMessageProperties 下覆盖,它们也会覆盖各自在 BubbleChatConfiguration 中定义的对应方。
如果聊天消息由玩家发送,message.TextSource 将对应该玩家,而 adornee 将是 nil 。
如果聊天消息通过 发送,那么 将提供,而 将是 。
当与客户端发送消信息绑定时,此回调会两次运行:首先当消息初始发送并在本地接收时,然后当客户端从服务器收到过滤后的消息的结果时再次运行。
参数
收到的 TextChatMessage 。
泡泡聊天消息附属的部分或角色。
返回
如果返回了 BubbleChatMessageProperties,其属性将覆盖 BubbleChatConfiguration 属性。
OnChatWindowAdded
当新消息即将显示在聊天窗口时调用。这只能在客户端实现。
使用此来自定义聊天窗口中显示的个人消息。如果此回调返回 ChatWindowMessageProperties,这些属性将应用于相关信息,覆盖 ChatWindowConfiguration 属性。如果 UIGradient 被父辈在 ChatWindowMessageProperties 下覆盖,它也会覆盖在 TextColor3 中定义的 ChatWindowConfiguration 属性。
当与客户端发送消信息绑定时,此回调会两次运行:首先当消息初始发送并在本地接收时,然后当客户端从服务器收到过滤后的消息的结果时再次运行。
参数
收到的 TextChatMessage 。
返回
如果返回了 ChatWindowMessageProperties,其属性将覆盖 ChatWindowConfiguration 属性。
OnIncomingMessage
当 TextChatService 收到来自外部的消信息时调用。只能在客户端实现。
使用这个来装饰 TextChatMessages .如果此回调返回一个 TextChatMessageProperties ,那些属性将与 TextChatMessage 参数合并以创建一个新的 TextChatMessage 。
当与客户端发送消信息绑定时,此回调会两次运行;首先当消息初始发送并在本地接收时,然后当客户端从服务器接收过滤后的消息的结果时。
请注意,这个 TextChatService.OnIncomingMessage 回调在任何 **** 回调之前运行 TextChannel.OnIncomingMessage。
这应该仅在源验证码中定义一次。多个绑定将以非决定性方式覆盖 друг друга。
参数
收到的 TextChatMessage 。
返回
如果返回了 TextChatMessageProperties ,那些属性将与 TextChatMessage 参数合并,创建一个新的 TextChatMessage 拥有那些属性,否则,如果返回了 nil ,那么 TextChatMessage 不会被更改。