boppescripting / b-discordbooster

A beyond simple free script for server owners to give a reward to their discord boosters!
GNU General Public License v3.0
11 stars 1 forks source link

[BUG] Im getting an error #2

Closed BallisticOK closed 5 months ago

BallisticOK commented 9 months ago

i have made some changes to how it work a little but no mater what i do i cant seem to fix this error im getting

[   1410859] [b2944_GTAProce]             MainThrd/ ^1SCRIPT ERROR: @sec-utils/client/func-booster.lua:4: attempt to call a nil value (field 'GetIdentifier')^7
[   1410859] [b2944_GTAProce]             MainThrd/ ^3> ObtainDiscordIdentifier^7 (^5@sec-utils/client/func-booster.lua^7:4)
[   1410859] [b2944_GTAProce]             MainThrd/ ^3> ref^7 (^5@sec-utils/client/main-booster.lua^7:18)

This is all the code for the changes i have done

Func:

local QBCore = exports['qb-core']:GetCoreObject()

function ObtainDiscordIdentifier(src)
    local discordIdentifier = QBCore.Functions.GetIdentifier(src, 'discord')
    return discordIdentifier and string.sub(discordIdentifier, 9) or nil
end

function Notify(src, text, notifType)
    QBCore.Functions.Notify(src, text, notifType)
end

function ProduceRewards(src, rewards)
    local Player = QBCore.Functions.GetPlayer(src)
    if not Player then return false end

    -- Randomly select a reward
    local selectedReward = rewards[math.random(1, #rewards)]

    if selectedReward.type == 'bank' then
        Player.Functions.AddMoney('bank', selectedReward.amount, 'Discord Booster Reward')
    elseif selectedReward.type == 'cash' then
        Player.Functions.AddMoney('cash', selectedReward.amount, 'Discord Booster Reward')
    elseif selectedReward.type == 'item' then
        Player.Functions.AddItem(selectedReward.item, selectedReward.amount, nil, {})
    elseif selectedReward.type == 'car' then
        GiveCarReward(src, selectedReward.vehicle)
    else
        print('sec-booster | INVALID REWARD TYPE')
        return false
    end

    return true
end

function GiveCarReward(src, vehicleData)
    local vehicle = vehicleData[2]
    local pData = QBCore.Functions.GetPlayer(src)
    local cid = pData.PlayerData.citizenid
    local plate = PlateQBGen()

    MySQL.Async.execute('INSERT INTO player_vehicles (license, citizenid, vehicle, hash, mods, plate, state, garage) VALUES (@license, @citizenid, @vehicle, @hash, @mods, @plate, @state, @garage)', {
        ['@license'] = pData.PlayerData.license,
        ['@citizenid'] = cid,
        ['@vehicle'] = vehicle[2],
        ['@hash'] = vehicle[1],
        ['@mods'] = '{}',
        ['@plate'] = plate,
        ['@state'] = 1,
        ['@garage'] = "legionsquare"
    }, function(rowsChanged)
        if rowsChanged > 0 then
            Notify(src, 'You received a new vehicle as a reward!', 'success')
        else
            Notify(src, 'Failed to give you a vehicle. Please contact a server admin.', 'error')
        end
    end)
end

Main:

-- Functions
function CollectedThisMonth(discord)
    local row = MySQL.query.await('SELECT * FROM sec-booster WHERE discord=?', { discord })
    if not row[1] then return false end
    local currentMonth = os.date('*t').month
    if row[1].last_month_collected == currentMonth then return true end
    return false
end

function UpdateLastCollectedMonth(discord)
    local currentMonth = os.date('*t').month
    MySQL.query('INSERT INTO sec-booster (discord, last_month_collected) VALUES (?, ?) ON DUPLICATE KEY UPDATE last_month_collected=?', { discord, currentMonth, currentMonth })
end

-- Commands
RegisterCommand('checkboost', function(source)
    local src = source
    local discord = ObtainDiscordIdentifier(src)
    local url = string.format('https://discord.com/api/v9/guilds/%s/members/%s', Config.DiscordGuildID, tostring(discord))
    print(discord)

    PerformHttpRequest(url, function (errorCode, rdata, resultHeaders)
        local res = json.decode(rdata)
        if errorCode == 200 then
            if res.premium_since then
                if not CollectedThisMonth(discord) then
                    if ProduceRewards(src, Config.Rewards) then
                        Notify(src, 'Thank you for supporting us!', 'success')
                        return UpdateLastCollectedMonth(discord)
                    else
                        return Notify(src, 'Please advise a server admin to check the console.', 'error')
                    end
                else
                    return Notify(src, 'You\'ve already collected your reward this month.', 'error')
                end
            end
        else
            print(errorCode)
        end
    end, "GET", "", {["Content-type"] = "application/json", ["Authorization"] = string.format('Bot %s', Config.DiscordBotToken)})
end, false)
boppescripting commented 9 months ago

Is your qb-core a recent version? By the error, seems that you don't have the GetIdentifier function in your core.

BallisticOK commented 9 months ago

Hey, sorry for the late response. I understand now. Thanks! Yes, that was the problem.

Also, I don't know how to do a push or anything, so feel free to update the code you have with the updates I made. It allows you to give cars to users as one of the rewards. If I did it right, it should also make it so every month it randomly picks a reward and doesn't always give the same one.