#Checking when user is banned with api without having to rejoin.

15 messages · Page 1 of 1 (latest)

muted glade
#

I'm trying to make it so my remote ban system picks up as soon as the user is added to the database, because right now they have to rejoin the game in order for it to work

I know some people will say "just use a while loop" however this is bad as it can overwhelm the api.

Here is my api checkifbanned and ban route

app.get('/checkuser', validateAPIKey, async (req, res) => {
  const requestedUserId = req.query.userid;

  try {
    const result = await collection.findOne({ userid: requestedUserId });

    if (result) {
      const { reason } = result;
      res.json({ Banned: true, reason });
    } else {
      res.json({ Banned: false, reason: null });
    }
  } catch (error) {
    console.error('Error while checking value:', error);
    res.status(500).json({ error: 'Internal server error' });
  }
});

app.post('/ban', async (req, res) => {
    const { notapikeylol, userid, usernametoinsert, reason } = req.body;

  if (notapikeylol !== "hotdog123") {
    return res.status(401).json({ error: 'Invalid API key' });
  }

  axios
    .get(`https://api.newstargeted.com/roblox/users/v2/user.php?userId=${userid}`)
    .then(async (response) => {
      console.log(response.data);
      const usernametoinsert = response.data.username;

      try {
        const result = await insertUserData(userid, usernametoinsert, reason);

        res.json({ success: true, insertedCount: result.insertedCount });
      } catch (error) {
        console.error('Error while inserting user:', error);
        res.status(500).json({ error: 'Internal server error' });
      }
    })
    .catch((error) => {
      console.error(`Error: ${error.message}`);
      res.status(500).json({ error: 'Internal server error' });
    });
});

async function insertUserData(userid, usernametoinsert, reason) {
  const result = await collection.insertOne({ userid, usernametoinsert, reason });
  return result;
}
#

and here is the code that sends requests to the api in roblox when a player joins

local HttpService = game:GetService("HttpService")
local tpservice = game:GetService('TeleportService')

local apiurl = script.apiendpoint.Value
local apikey = script.apikey.Value

local rateLimitTable = {}

local function checkRateLimit(userId)
    local currentTime = os.time()
    local lastJoinTime = rateLimitTable[userId] or 0
    local cooldown = 15

    if currentTime - lastJoinTime < cooldown then
        return false
    else
        rateLimitTable[userId] = currentTime
        return true
    end
end

local function fetchUserData(userId)
    local url = apiurl.."/checkuser?userid="..userId.."&api_key="..apikey
    return HttpService:JSONDecode(HttpService:GetAsync(url))
end

game.Players.PlayerAdded:Connect(function(plr)
    if checkRateLimit(plr.UserId) then
        local response = fetchUserData(plr.UserId)
        
        if response["Banned"] == true then
            local tpdata = {
                reason = response['reason']
            }
            plr:Kick('You\'ve been banned! Reason: '..response['reason'])
            tpservice:Teleport(14153110712, plr, tpdata)
        end
    else
        plr:Kick('You are rejoining too quickly')
    end
end)

how could I impliment something that checks twhen the user is banned withoutt having to rejoin (not a while loop that constantly requests)

https://cdn.discordapp.com/attachments/850057247253069835/1137070079859769384/2023-08-04_20-09-30.mov

river sorrel
#

How do you actually go about banning someone? Do you send that info to your database directly? Is it from Roblox or some outside source?

#

If its from an outside source you might have to poll the database for these changes.

#

To my knowledge roblox doesn't allow you to listen to events for this kind of stuff

muted glade
river sorrel
muted glade
#

ah ok sorry

#

i can ever ban them from discrd or command prompt from a cli

river sorrel
#

Alright, yeah your going to have to use polling (aka a while loop). 🤷‍♂️

muted glade
#

Nooo

#

is there any other way

river sorrel
#

Roblox doesn't support recieving signals from outside sources so no

muted glade
#

rip ok

#

thanks for the help thoug