Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add way to send system messages to ChatProviderService #520

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chatproviderservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@quenty/playerutils": "file:../playerutils",
"@quenty/preferredparentutils": "file:../preferredparentutils",
"@quenty/promise": "file:../promise",
"@quenty/remoting": "file:../remoting",
"@quenty/richtext": "file:../richtext",
"@quenty/rx": "file:../rx",
"@quenty/rxbinderutils": "file:../rxbinderutils",
Expand Down
45 changes: 44 additions & 1 deletion src/chatproviderservice/src/Client/ChatProviderServiceClient.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

local require = require(script.Parent.loader).load(script)

local TextChatService = game:GetService("TextChatService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local TextChatService = game:GetService("TextChatService")

local Maid = require("Maid")
local Signal = require("Signal")
local String = require("String")
local TextChannelUtils = require("TextChannelUtils")

local ChatProviderServiceClient = {}
ChatProviderServiceClient.ServiceName = "ChatProviderServiceClient"
Expand All @@ -36,6 +38,27 @@ function ChatProviderServiceClient:Start()
TextChatService.OnIncomingMessage = function(textChatMessage)
self.MessageIncoming:Fire(textChatMessage)

local metadata = textChatMessage.Metadata
if metadata then
local success, decodedMessageData = pcall(function()
return HttpService:JSONDecode(metadata)
end)

if success and decodedMessageData then
local color = decodedMessageData.Color or "#ffffff"
local isValidHex = pcall(function()
return Color3.fromHex(color)
end)

if isValidHex then
local overrideProperties = Instance.new("TextChatMessageProperties")
overrideProperties.Text = `<font color="#{color}">{textChatMessage.Text}</font>`

return overrideProperties
end
end
end

local textSource = textChatMessage.TextSource
if not textSource then
return
Expand All @@ -53,6 +76,26 @@ function ChatProviderServiceClient:Start()
end
end

--[=[
Sends a system message to the provided TextChannel.
@param encodedMessageData string
@param channel TextChannel?
]=]
function ChatProviderServiceClient:SendSystemMessage(message: string, encodedMessageData: string?, channel: TextChannel?)
assert(typeof(message) == "string", "[ChatProviderServiceClient.SendSystemMessage] - Bad message")

if not channel then
channel = TextChannelUtils.getDefaultTextChannel()
end

if not channel then
warn("[ChatProviderServiceClient.SendSystemMessage] - Failed to get default channel")
return
end

channel:DisplaySystemMessage(message, encodedMessageData)
end

function ChatProviderServiceClient:_renderTags(textSource)
local player = Players:GetPlayerByUserId(textSource.UserId)
if not player then
Expand Down
29 changes: 29 additions & 0 deletions src/chatproviderservice/src/Shared/TextChannelUtils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--[=[
Utility functions for querying text channels.
@class TextChannelUtils
]=]

local require = require(script.Parent.loader).load(script)

local TextChatService = game:GetService("TextChatService")

local TextChannelUtils = {}

function TextChannelUtils.getDefaultTextChannel()
return TextChannelUtils.getTextChannel("RBXGeneral")
end

function TextChannelUtils.getTextChannel(channelName: string)
local channels = TextChannelUtils.getTextChannels()
if not channels then
return
end

return channels:FindFirstChild(channelName)
end

function TextChannelUtils.getTextChannels()
return TextChatService:FindFirstChild("TextChannels")
end

return TextChannelUtils
Loading