Advertisement
ShadyX-SAMP

autogrammar.lua

Nov 7th, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | Gaming | 0 0
  1. script_name("autogrammar")
  2. script_author("Rockwell / Shady (Discord: shadyxo)")
  3.  
  4. local vkeys = require "vkeys"
  5. local autoGrammarEnabled = false
  6. local configFolder = getWorkingDirectory() .. '\\config\\'
  7. local configFile = configFolder .. 'autogrammar.ini'
  8.  
  9. function main()
  10.     if not doesDirectoryExist(configFolder) then
  11.         createDirectory(configFolder)
  12.     end
  13.  
  14.     if doesFileExist(configFile) then
  15.         loadGrammarState()
  16.     else
  17.         blankIni()
  18.     end
  19.  
  20.     while not isSampAvailable() do wait(100) end
  21.  
  22.     sampRegisterChatCommand("autogrammar", toggleAutoGrammar)
  23.  
  24.     while true do
  25.         wait(0)
  26.  
  27.         if autoGrammarEnabled and sampIsChatInputActive() then
  28.             if wasKeyPressed(vkeys.VK_RETURN) then
  29.                 local chatInput = sampGetChatInputText()
  30.  
  31.                 if chatInput ~= "" and chatInput:match("%S") then
  32.                     if not chatInput:match("^/") then
  33.                         local correctedMessage = autoCorrectSentence(chatInput)
  34.                         sampSetChatInputText(correctedMessage)
  35.                     end
  36.                 end
  37.             end
  38.         end
  39.     end
  40. end
  41.  
  42. function toggleAutoGrammar()
  43.     if autoGrammarEnabled then
  44.         autoGrammarEnabled = false
  45.         sampAddChatMessage("{FFA500}Info: {FFFFFF}You have {FF0000}disabled{FFFFFF} Automatic Grammar-Checking. ", -1)
  46.     else
  47.         autoGrammarEnabled = true
  48.         sampAddChatMessage("{FFA500}Info: {FFFFFF}You have {00FF00}enabled{FFFFFF} Automatic Grammar-Checking.", -1)
  49.     end
  50.  
  51.     saveGrammarState()
  52. end
  53.  
  54. function autoCorrectSentence(msg)
  55.     msg = msg:match("^%s*(.-)%s*$")
  56.  
  57.     msg = msg:sub(1, 1):upper() .. msg:sub(2)
  58.  
  59.     if not msg:find("[%.!%?]$") then
  60.         msg = msg .. "."
  61.     end
  62.  
  63.     msg = msg:gsub("([%.!?]%s*)(%l)", function(punct, letter)
  64.         return punct .. letter:upper()
  65.     end)
  66.  
  67.     return msg
  68. end
  69.  
  70. function saveGrammarState()
  71.     local f = io.open(configFile, "w")
  72.     if f then
  73.         f:write("[Settings]\n")
  74.         f:write("autoGrammarEnabled=" .. tostring(autoGrammarEnabled) .. "\n")
  75.         f:close()
  76.     end
  77. end
  78.  
  79. function loadGrammarState()
  80.     local f = io.open(configFile, "r")
  81.     if f then
  82.         local content = f:read("*all")
  83.         f:close()
  84.  
  85.         local enabled = content:match("autoGrammarEnabled=(%w+)")
  86.         if enabled == "false" then
  87.             autoGrammarEnabled = false
  88.         else
  89.             autoGrammarEnabled = true
  90.         end
  91.     end
  92. end
  93.  
  94. function doesDirectoryExist(path)
  95.     local f = io.open(path, "r")
  96.     if f then
  97.         f:close()
  98.         return true
  99.     else
  100.         return false
  101.     end
  102. end
  103.  
  104. function createDirectory(path)
  105.     os.execute("mkdir " .. path)
  106. end
  107.  
  108. function doesFileExist(path)
  109.     local f = io.open(path, "r")
  110.     return f ~= nil
  111. end
  112.  
  113. function blankIni()
  114.     autoGrammarEnabled = false
  115.     saveGrammarState()
  116. end
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement