Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local StarterGui = game:GetService("StarterGui")
- local HttpService = game:GetService("HttpService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local url = "http://localhost:5000/chat?text="
- local newChat = not ReplicatedStorage:FindFirstChild("DefaultChatSystemChatEvents")
- local function notify(title, text)
- StarterGui:SetCore("SendNotification", {
- Title = title;
- Text = text;
- Duration = 5;
- })
- print(title..": "..text)
- end
- notify("AI Chatbot", "Loading...")
- -- GUI
- local gui = loadstring(game:HttpGet('https://raw.githubusercontent.com/violin-suzutsuki/LinoriaLib/main/Library.lua'))()
- -- Window
- local Window = gui:CreateWindow({
- Title = 'AI chatbot',
- Center = true,
- AutoShow = false,
- TabPadding = 8,
- MenuFadeTime = 0.2
- })
- -- Main tab
- local MainTab = Window:AddTab('Main')
- -- Group
- local Group = MainTab:AddLeftGroupBox('Chatbot')
- ---
- LeftGroupBox:AddToggle('Enabled', {
- Text = 'Enable chatbot',
- Default = true,
- Tooltip = ''
- })
- ---
- LeftGroupBox:AddSlider('Distance', {
- Text = 'Distance',
- Default = 15,
- Min = 0,
- Max = 100,
- Rounding = 1,
- Compact = false
- })
- -- End of GUI
- -- Get the text chat services
- local TextChatService = game:GetService("TextChatService")
- local generalChannel = nil
- if newChat then
- generalChannel = TextChatService.TextChannels.RBXGeneral
- end
- if newChat then
- if not generalChannel then
- notify("Error", "Unknown chat system (detected as new)")
- return
- end
- notify("Chat system", "New")
- else
- if not ReplicatedStorage:FindFirstChild("DefaultChatSystemChatEvents") then
- notify("Error", "Unknown chat system (detected as old)")
- return
- end
- notify("Chat system", "Old chat system is not supported yet")
- return
- end
- local function splitText(text, chunkSize)
- local chunks = {}
- local startPosition = 1
- local textLength = string.len(text)
- while startPosition <= textLength do
- local endPosition = startPosition + chunkSize - 1
- -- Не разрезаем слово, если это возможно
- if endPosition < textLength then
- while endPosition > startPosition and string.sub(text, endPosition, endPosition) ~= " " do
- endPosition = endPosition - 1
- end
- end
- -- Если нет пробелов, принудительно разрезаем текст
- if endPosition == startPosition then
- endPosition = startPosition + chunkSize - 1
- end
- table.insert(chunks, string.sub(text, startPosition, endPosition))
- startPosition = endPosition + 1
- end
- return chunks
- end
- local function chat(text)
- local chunks = splitText(text, 200)
- for i, chunk in ipairs(chunks) do
- --if newChat then
- generalChannel:SendAsync(chunk)
- --else
- --ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest:FireServer(chunk)
- --end
- end
- end
- local function getPlayerByUserId(userId)
- for _, player in pairs(Players:GetPlayers()) do
- if player.UserId == userId then
- return player
- end
- end
- return nil
- end
- local function onChatMessageReceived(player, text)
- if (player.Character.HumanoidRootPart.Position - Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude > Options.Distance.Value then return end
- local author = player.Name
- gui:Notify("Chat: " .. text)
- local resp = HttpService:JSONDecode(syn.request({
- Url=url..HttpService:UrlEncode(author.." said: "..text)
- }).Body)
- gui:Notify("Reply: " .. resp.reply or resp.error)
- if resp.reply then
- chat(resp.reply)
- end
- end
- local GameName = game:GetService("MarketplaceService"):GetProductInfo(game.PlaceId).Name
- gui:Notify('Game name: ' .. GameName)
- -- Clear chat history
- syn.request({
- Url = "http://localhost:5000/clear"
- })
- local g = "You are a Roblox player who is in the game called '"..GameName.."' and your in-game name is "..game.Players.LocalPlayer.Name..". Use an informal communication style and try not to make long messages. Don't mention users using @ when replying to them, write the reply text right away. First, say hello to other players!"
- local res = HttpService:JSONDecode(syn.request({
- Url=url .. HttpService:UrlEncode(g)
- }).Body)
- if res.reply then
- gui:Notify("Successfully initialized!")
- chat("AI chatbot enabled")
- wait(1)
- chat(res.reply)
- if newChat then
- -- Connect the onChatMessageReceived function to the Chat service's MessageReceived event
- TextChatService.MessageReceived:Connect(function(message)
- if not Toggles.Enabled.Value then return end
- if message.TextSource.UserId == Players.LocalPlayer.UserId then
- return
- end
- local player = getPlayerByUserId(message.TextSource.UserId)
- if not player then
- notify("Error", "Unable to get player from UserId")
- end
- onChatMessageReceived(player, message.Text)
- end)
- else
- Players.PlayerChatted:Connect(function(_type, player, message)
- if player.UserId == Players.LocalPlayer.UserId then
- return
- end
- onChatMessageReceived(player, message)
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement