Discord Bot connects to FiveM Server — Whitelist, Logs and Status
Discord Bot connects to FiveM Server.
Discord is the main platform for the FiveM community. Integrating the bot with the server makes admin work much easier.
Discord Webhook — The easiest way.
Webhook is the URL provided by Discord for sending messages to the channel.
-- server.lua (must be on server-side only!)
local function SendDiscordLog(title, description, color)
local webhook = GetConvar('discord_webhook_logs', '')
if webhook == '' then return end
local embed = {
{
title = title,
description = description,
color = color or 3447003, -- blue
timestamp = os.date('!%Y-%m-%dT%H:%M:%SZ'),
footer = { text = 'M2 Roleplay Server' }
}
}
PerformHttpRequest(webhook, function(code, text, headers)
-- Sent
end, 'POST', json.encode({ embeds = embed }), {
['Content-Type'] = 'application/json'
})
end
-- Log when players join
AddEventHandler('playerConnecting', function(name, setKickReason, deferrals)
local src = source
local steam = GetPlayerIdentifierByType(tostring(src), 'steam') or 'Unknown'
SendDiscordLog(
'🟢 Players join',
string.format('**name:** %s\n**Steam:** %s', name, steam),
5763719 -- green
)
end)
-- Log when a player leaves
AddEventHandler('playerDropped', function(reason)
local src = source
local name = GetPlayerName(tostring(src)) or 'Unknown'
SendDiscordLog(
'🔴 Player leaves Server',
string.format('**name:** %s\n**reason:** %s', name, reason),
15158332 -- red
)
end)
Log for Admin Actions
-- Log when admin uses commands
local function LogAdminAction(adminName, action, targetName)
SendDiscordLog(
'⚡ Admin Action',
string.format(
'**Admin:** %s\n**Action:** %s\n**Target:** %s',
adminName, action, targetName
),
16776960 -- yellow
)
end
-- Example: ban command
RegisterCommand('ban', function(source, args, rawCommand)
local src = source
local xPlayer = ESX.GetPlayerFromId(src)
if not IsPlayerAceAllowed(tostring(src), 'command.ban') then return end
local targetId = tonumber(args[1])
local reason = table.concat(args, ' ', 2)
local xTarget = ESX.GetPlayerFromId(targetId)
if xTarget then
-- ban logic
DropPlayer(targetId, 'Banned: ' .. reason)
-- log to discord
LogAdminAction(
GetPlayerName(tostring(src)),
'BAN — reason: ' .. reason,
xTarget.getName()
)
end
end, false)
Whitelist via Discord Role
-- server.lua
local whitelistAPI = 'https://your-bot.com/api/whitelist/check'
AddEventHandler('playerConnecting', function(name, setKickReason, deferrals)
local src = source
deferrals.defer()
deferrals.update('Checking whitelist...')
local discord = GetPlayerIdentifierByType(tostring(src), 'discord')
if not discord then
deferrals.done('Discord must be opened to join this server')
return
end
local discordId = discord:gsub('discord:', '')
PerformHttpRequest(
whitelistAPI .. '?id=' .. discordId,
function(code, text)
local data = json.decode(text)
if data and data.whitelisted then
deferrals.done() -- allow
else
deferrals.done('You have not been whitelisted yet. Sign up at discord.gg/yourserver')
end
end,
'GET', '', {}
)
end)
server.cfg Setup
# Convars for webhooks
set discord_webhook_logs "https://discord.com/api/webhooks/YOUR_WEBHOOK"
set discord_webhook_admin "https://discord.com/api/webhooks/YOUR_ADMIN_WEBHOOK"
set discord_webhook_economy "https://discord.com/api/webhooks/YOUR_ECON_WEBHOOK"
Summary
Discord integrations range from a simple webhook to a full bot API. The webhook is great for logging and the bot API is great for whitelisting and interactive commands.
Related Articles
Breaking: GTA Online อัพเดท "Money Fronts" — FiveM Server Owners ต้องทำอะไร?
Rockstar ปล่อย GTA Online อัพเดท Money Fronts มีผลกระทบต่อ FiveM servers บางส่วน นี่คือสิ่งที่ต้องทำทันทีหลังอัพเดท
Community Spotlight: Script และ Projects ที่น่าสนใจจาก FiveM Community
รวม scripts, tools และ projects ที่โดดเด่นจาก FiveM community ในช่วงที่ผ่านมา ตั้งแต่ free resources ถึง open-source projects
txAdmin อัพเดทใหม่ — Dashboard, Diagnostics และ Ban System ที่ดีขึ้น
txAdmin ซึ่งตอนนี้เป็นส่วนหนึ่งของ Cfx.re อย่างเป็นทางการ ได้รับการอัพเดทครั้งใหญ่ มี features ใหม่ที่ทำให้ Server Management ง่ายขึ้นมาก