[MODULE] Озвучка текста в чате

Скрипт на озвучку текста в чате для GM-DONATE

Мой старый скрипт на озвучку думаю будет полезен многим серверам.

Скрипт вставлять в путь: lua/autorun/server/

Скрипт:


local STORE_ITEM = FindMetaTable("IGSItem")

function STORE_ITEM:SetSoundURL(url)
    return self:SetMeta("sound_url", url)
end

function STORE_ITEM:SetTTSPattern(pattern)
    return self:SetMeta("tts_pattern", pattern)
end

function STORE_ITEM:IsScriptPurchased(ply)

    return ply:HasPurchase("ozvuchkateksta")
end

local function SpeakText(text, voice, sender)
    http.Fetch("https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=" .. voice .. "&q=" .. text,
        function(body, size, headers, code)
            if code == 200 then

                if SERVER then
                    net.Start("IGS.PLAYSOUND")
                    net.WriteEntity(sender)
                    net.WriteString(body)
                    net.Broadcast()
                end
            else

            end
        end,
        function(error)

        end
    )
end

local function OpenVoiceMenu(ply)
    if ply:HasPurchase("ozvuchkateksta") or (ply:IsUserGroup("vip") or ply:IsUserGroup("premium") or ply:IsUserGroup("moderator") or ply:IsUserGroup("admin")) then
        local menu = DermaMenu()

        menu:AddOption("Озвучка женским голосом", function()
            ply:SetNWString("voice", "женский")
            ply:ChatPrint("Вы выбрали озвучку женским голосом.")
        end):SetImage("icon16/user_female.png")

        menu:AddOption("Озвучка мужским голосом", function()
            ply:SetNWString("voice", "мужской")
            ply:ChatPrint("Вы выбрали озвучку мужским голосом.")
        end):SetImage("icon16/user.png")

        menu:AddSpacer()

        local speechEnabled = ply:GetNWBool("speechEnabled", false)

        if speechEnabled then
            menu:AddOption("Выключить озвучивание текста", function()
                ply:SetNWBool("speechEnabled", false)
                ply:ChatPrint("Вы выключили озвучку текста в чате.")
            end):SetImage("icon16/sound_mute.png")
        else
            menu:AddOption("Включить озвучивание текста", function()
                ply:SetNWBool("speechEnabled", true)
                ply:ChatPrint("Вы включили озвучку текста в чате.")
            end):SetImage("icon16/sound.png")
        end

        local textLabel = vgui.Create("DLabel")
        textLabel:SetText("Озвучка текста сделана для сервера ViperRolePlay (2022) от Faulin'a")
        textLabel:SetContentAlignment(5)
        textLabel:SetTextColor(Color(255, 255, 255))
        textLabel:SetFont("DermaDefault")
        textLabel:SizeToContents()
        menu:AddPanel(textLabel)


        menu:AddOption("Закрыть", function() end):SetImage("icon16/cross.png")


        menu:SetSize(200, 320)
        menu:Center()
        menu:MakePopup()
        menu:DoModal()
    else
        ply:ChatPrint("Доступ к озвучке текста необходимо приобрести в донат магазине.")
    end
end

if SERVER then
    util.AddNetworkString("IGS.PLAYSOUND")

    local function phraseToTTSUrl(phrase)
        local GOOGLE = "https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=%s&tl=ru"
        return GOOGLE:format(phrase:URLEncode())
    end

    hook.Add("IGS.PlayerPurchasedItem", "IGS.PLAYSOUND", function(pl, ITEM)
        if not ITEM:IsScriptPurchased(pl) then
            return
        end

        local url

        local tts_pattern = ITEM:GetMeta("tts_pattern")
        if tts_pattern then
            local txt = tts_pattern:gsub("{nick}", pl:Nick())
            url = phraseToTTSUrl(txt)
        else
            url = ITEM:GetMeta("sound_url")
        end

        if url then
            net.Start("IGS.PLAYSOUND")
            net.WriteEntity(pl)
            net.WriteString(url)
            net.Broadcast()
        end
    end)

else
    local function PlayURL(url, sender)
        if sender:GetPos():Distance(LocalPlayer():GetPos()) <= 400 then
            sound.PlayURL(url, "3d", function(chan)
                if chan and chan:IsValid() then
                    chan:Play()
                end
            end)
        end
    end

    net.Receive("IGS.PLAYSOUND", function()
        local sender = net.ReadEntity()
        local url = net.ReadString()
        PlayURL(url, sender)
    end)

    hook.Add("PlayerSay", "TextToSpeechCommand", function(ply, text)
        local command = string.lower(text)
        if command == "/ozvuchka" then

            if ply:HasPurchase("ozvuchkateksta") and (ply:IsUserGroup("vip") or ply:IsUserGroup("premium") or ply:IsUserGroup("moderator") or ply:IsUserGroup("admin")) then
                OpenVoiceMenu(ply)
            else
                ply:ChatPrint("Доступ к озвучке текста необходимо приобрести в донат магазине.")
            end
        end
    end)

    hook.Add("PlayerSay", "TextToSpeech", function(ply, text)
        local speechEnabled = ply:GetNWBool("speechEnabled", false)

        if speechEnabled then
            local voice = ply:GetNWString("voice", "женский")

            SpeakText(text, voice, ply)
        end
    end)
end

Услуга в sh_additems.lua

IGS("Говорилка на 1 месяц", "ozvuchkateksta")
    :SetPrice(200)
    :SetTerm(30)
    :SetDescription("Ваш текст будет озвучиваться")
    :SetCategory("Дополнительное")

скрипт нужно в shared закидывать

это в геймоде?