Advertisement
CatDev125

Roblox Chatbot

Apr 10th, 2024 (edited)
4,555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | Gaming | 0 0
  1. local Players = game:GetService("Players")
  2. local StarterGui = game:GetService("StarterGui")
  3. local HttpService = game:GetService("HttpService")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local url = "http://localhost:5000/chat?text="
  6.  
  7. local newChat = not ReplicatedStorage:FindFirstChild("DefaultChatSystemChatEvents")
  8.  
  9. local function notify(title, text)
  10.     StarterGui:SetCore("SendNotification", {
  11.         Title = title;
  12.         Text = text;
  13.         Duration = 5;
  14.     })
  15.     print(title..": "..text)
  16. end
  17.  
  18. notify("AI Chatbot", "Loading...")
  19.  
  20.  
  21. -- GUI
  22.  
  23. local gui = loadstring(game:HttpGet('https://raw.githubusercontent.com/violin-suzutsuki/LinoriaLib/main/Library.lua'))()
  24.  
  25. -- Window
  26.  
  27. local Window = gui:CreateWindow({
  28.     Title = 'AI chatbot',
  29.     Center = true,
  30.     AutoShow = false,
  31.     TabPadding = 8,
  32.     MenuFadeTime = 0.2
  33. })
  34.  
  35. -- Main tab
  36.  
  37. local MainTab = Window:AddTab('Main')
  38.  
  39. -- Group
  40.  
  41. local Group = MainTab:AddLeftGroupBox('Chatbot')
  42.  
  43. ---
  44.  
  45. LeftGroupBox:AddToggle('Enabled', {
  46.     Text = 'Enable chatbot',
  47.     Default = true,
  48.     Tooltip = ''
  49. })
  50.  
  51. ---
  52.  
  53. LeftGroupBox:AddSlider('Distance', {
  54.     Text = 'Distance',
  55.     Default = 15,
  56.     Min = 0,
  57.     Max = 100,
  58.     Rounding = 1,
  59.     Compact = false
  60. })
  61.  
  62. -- End of GUI
  63.  
  64. -- Get the text chat services
  65. local TextChatService = game:GetService("TextChatService")
  66.  
  67. local generalChannel = nil
  68. if newChat then
  69.     generalChannel = TextChatService.TextChannels.RBXGeneral
  70. end
  71.  
  72. if newChat then
  73.     if not generalChannel then
  74.         notify("Error", "Unknown chat system (detected as new)")
  75.         return
  76.     end
  77.     notify("Chat system", "New")
  78. else
  79.     if not ReplicatedStorage:FindFirstChild("DefaultChatSystemChatEvents") then
  80.         notify("Error", "Unknown chat system (detected as old)")
  81.         return
  82.     end
  83.     notify("Chat system", "Old chat system is not supported yet")
  84.     return
  85. end
  86.  
  87. local function splitText(text, chunkSize)
  88.     local chunks = {}
  89.     local startPosition = 1
  90.     local textLength = string.len(text)
  91.  
  92.     while startPosition <= textLength do
  93.         local endPosition = startPosition + chunkSize - 1
  94.         -- Не разрезаем слово, если это возможно
  95.         if endPosition < textLength then
  96.             while endPosition > startPosition and string.sub(text, endPosition, endPosition) ~= " " do
  97.                 endPosition = endPosition - 1
  98.             end
  99.         end
  100.         -- Если нет пробелов, принудительно разрезаем текст
  101.         if endPosition == startPosition then
  102.             endPosition = startPosition + chunkSize - 1
  103.         end
  104.         table.insert(chunks, string.sub(text, startPosition, endPosition))
  105.         startPosition = endPosition + 1
  106.     end
  107.  
  108.     return chunks
  109. end
  110.  
  111. local function chat(text)
  112.     local chunks = splitText(text, 200)
  113.     for i, chunk in ipairs(chunks) do
  114.         --if newChat then
  115.         generalChannel:SendAsync(chunk)
  116.         --else
  117.             --ReplicatedStorage.DefaultChatSystemChatEvents.SayMessageRequest:FireServer(chunk)
  118.         --end
  119.     end
  120. end
  121.  
  122. local function getPlayerByUserId(userId)
  123.     for _, player in pairs(Players:GetPlayers()) do
  124.         if player.UserId == userId then
  125.             return player
  126.         end
  127.     end
  128.     return nil
  129. end
  130.  
  131. local function onChatMessageReceived(player, text)
  132.     if (player.Character.HumanoidRootPart.Position - Players.LocalPlayer.Character.HumanoidRootPart.Position).Magnitude > Options.Distance.Value then return end
  133.    
  134.     local author = player.Name
  135.  
  136.     gui:Notify("Chat: " .. text)
  137.  
  138.     local resp = HttpService:JSONDecode(syn.request({
  139.         Url=url..HttpService:UrlEncode(author.." said: "..text)
  140.     }).Body)
  141.  
  142.     gui:Notify("Reply: " .. resp.reply or resp.error)
  143.  
  144.     if resp.reply then
  145.         chat(resp.reply)
  146.     end
  147. end
  148.  
  149. local GameName = game:GetService("MarketplaceService"):GetProductInfo(game.PlaceId).Name
  150. gui:Notify('Game name: ' .. GameName)
  151.  
  152.  
  153. -- Clear chat history
  154. syn.request({
  155.     Url = "http://localhost:5000/clear"
  156. })
  157.  
  158. 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!"
  159.  
  160. local res = HttpService:JSONDecode(syn.request({
  161.     Url=url .. HttpService:UrlEncode(g)
  162. }).Body)
  163.  
  164. if res.reply then
  165.     gui:Notify("Successfully initialized!")
  166.     chat("AI chatbot enabled")
  167.     wait(1)
  168.     chat(res.reply)
  169.  
  170.     if newChat then
  171.         -- Connect the onChatMessageReceived function to the  Chat service's MessageReceived event
  172.         TextChatService.MessageReceived:Connect(function(message)
  173.             if not Toggles.Enabled.Value then return end
  174.            
  175.             if message.TextSource.UserId == Players.LocalPlayer.UserId then
  176.                 return
  177.             end
  178.             local player = getPlayerByUserId(message.TextSource.UserId)
  179.             if not player then
  180.                 notify("Error", "Unable to get player from UserId")
  181.             end
  182.             onChatMessageReceived(player, message.Text)
  183.         end)
  184.     else
  185.         Players.PlayerChatted:Connect(function(_type, player, message)
  186.             if player.UserId == Players.LocalPlayer.UserId then
  187.                 return
  188.             end
  189.             onChatMessageReceived(player, message)
  190.         end)
  191.     end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement