I’ve smashed together a few things.
I’m not sure if it all works yet, but it’s a template you could look at.
This setup ensures that only authorized players can manage bans and unbans, and allows for silent execution of commands when needed.
Code
local Players = game:GetService("Players")
local HttpService = game:GetService("HttpService")
local RunService = game:GetService("RunService")
local prefix = ";" -- Change this prefix as per your command setup
local silentPrefix = "/" -- Prefix for silent commands
-- Configuration: Allowed UserIDs, Groups, and Specific Ranks for Ban/Unban
local allowedUserIds = {4309939, -1} -- UserIds of the admins
local allowedGroups = {
{3057028, 255, 254, 250}, -- Group ID and ranks of the allowed groups
{3057028, 255, 252, 248}
-- Add more groups with their respective ranks in the format {groupId, rank1, rank2, ...}
}
local banRank = {3057028, 255, 254, 250} -- Group ID and ranks allowed to ban
local unbanRank = {3057028, 254, 253, 252} -- Group ID and ranks allowed to unban
-- Function to check if a player is in a specific group with a required rank
local function isInGroupWithRank(player, groupId, ranks)
if player:IsInGroup(groupId) then
local playerRank = player:GetRankInGroup(groupId)
for _, rank in ipairs(ranks) do
if playerRank == rank then
return true
end
end
end
return false
end
-- Function to check if a player can ban
local function canBan(player)
-- Check if player is in allowed UserIDs
for _, userId in ipairs(allowedUserIds) do
if player.UserId == userId then
return true
end
end
-- Check if player is in allowed groups with sufficient rank for banning
for _, groupInfo in ipairs(banRank) do
local groupId = groupInfo[1]
if isInGroupWithRank(player, groupId, groupInfo) then
return true
end
end
return false
end
-- Function to check if a player can unban
local function canUnban(player)
-- Check if player is in allowed UserIDs
for _, userId in ipairs(allowedUserIds) do
if player.UserId == userId then
return true
end
end
-- Check if player is in allowed groups with sufficient rank for unbanning
for _, groupInfo in ipairs(unbanRank) do
local groupId = groupInfo[1]
if isInGroupWithRank(player, groupId, groupInfo) then
return true
end
end
return false
end
-- Function to parse duration strings into seconds
local function parseDuration(durationStr)
local duration = tonumber(durationStr:match("(%d+)"))
local unit = durationStr:match("(%a+)")
if not duration or not unit then
return nil
end
if unit == "s" then
return duration
elseif unit == "m" then
return duration * 60
elseif unit == "h" then
return duration * 60 * 60
elseif unit == "d" then
return duration * 60 * 60 * 24
else
return nil
end
end
-- Function to ban a player with optional duration and reason
local function banPlayer(userId, durationStr, reason)
local duration = parseDuration(durationStr) or -1 -- Default to permanent ban if duration is not valid
local success, err = pcall(function()
local banHistoryPages = Players:GetBanHistoryAsync(userId)
local config = {
UserIds = {userId},
Duration = duration,
DisplayReason = "You violated community guideline #5",
PrivateReason = reason or "No reason provided",
ExcludeAltAccounts = false,
ApplyToUniverse = true
}
Players:BanAsync(config)
end)
if not success then
warn("Failed to ban player:", err)
end
end
-- Function to unban a player by UserID
local function unbanPlayer(userId, reason)
local success, err = pcall(function()
Players:UnbanUserAsync(userId)
end)
if success then
print("Successfully unbanned UserID:", userId, "Reason:", reason)
else
warn("Failed to unban UserID:", userId, "-", err)
end
end
-- Function to get ban history for a player by UserID
local function getBanHistory(userId)
local success, banHistoryPages = pcall(function()
return Players:GetBanHistoryAsync(userId)
end)
if success then
local historyMessage = ""
for _, entry in ipairs(banHistoryPages:GetCurrentPage()) do
historyMessage = historyMessage .. "\nBan Details:"
historyMessage = historyMessage .. "\n- Display Reason: " .. entry.DisplayReason
historyMessage = historyMessage .. "\n- Private Reason: " .. entry.PrivateReason
historyMessage = historyMessage .. "\n- Start Time: " .. entry.StartTime
historyMessage = historyMessage .. "\n- Duration: " .. entry.Duration .. " seconds"
historyMessage = historyMessage .. "\n- Ban in PlaceId: " .. (entry.PlaceId == -1 and "Universe" or tostring(entry.PlaceId))
historyMessage = historyMessage .. "\n"
end
return historyMessage
else
return "Failed to retrieve ban history for UserID " .. userId
end
end
-- Command handling for place owner or group owner to manage bans and view ban history
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
if message:sub(1, #prefix) == prefix or message:sub(1, #silentPrefix) == silentPrefix then
local command = message:sub(#prefix + 1)
local isSilent = message:sub(1, #silentPrefix) == silentPrefix
if isSilent then
command = message:sub(#silentPrefix + 1)
end
local function sendMessageToPlayer(player, message)
player:SendNotification({Title = "Command Result", Text = message, Duration = 5})
end
if command:sub(1, 9) == "banhistory" then
local userId = tonumber(command:sub(11)) -- Extract UserID from command
if userId then
-- Check if the player can view ban history
if canBan(player) then
local history = getBanHistory(userId)
sendMessageToPlayer(player, "Ban History for UserID " .. userId .. ":" .. history)
else
sendMessageToPlayer(player, "You are not authorized to view ban history.")
end
else
sendMessageToPlayer(player, "Invalid command. Use /banhistory <UserID> to view ban history.")
end
elseif command:sub(1, 3) == "ban" then
local args = command:sub(5):split(" ") -- Extract UserID, Duration, and Reason from command
local userId = tonumber(args[1])
local durationStr = args[2]
local reason = table.concat(args, " ", 3) or "No reason provided"
if userId then
-- Check if the player can ban
if canBan(player) then
if userId < 0 and not RunService:IsStudio() then
sendMessageToPlayer(player, "You cannot ban special UserIDs in production.")
else
banPlayer(userId, durationStr, reason)
sendMessageToPlayer(player, "Successfully banned UserID " .. userId .. " for reason: " .. reason)
end
else
sendMessageToPlayer(player, "You are not authorized to ban players.")
end
else
sendMessageToPlayer(player, "Invalid command. Use /ban <UserID> <Duration> <Reason> to ban a player.")
end
elseif command:sub(1, 5) == "unban" then
local args = command:sub(7):split(" ") -- Extract UserID and Reason from command
local userId = tonumber(args[1])
local reason = table.concat(args, " ", 2) or "No reason provided"
if userId then
-- Check if the player can unban
if canUnban(player) then
unbanPlayer(userId, reason)
sendMessageToPlayer(player, "Successfully unbanned UserID " .. userId .. " for reason: " .. reason)
else
sendMessageToPlayer(player, "You are not authorized to unban players.")
end
else
sendMessageToPlayer(player, "Invalid command. Use /unban <UserID> <Reason> to unban a player.")
end
end
end
end)
end)