np_discord v1.0.0 ​
Discord for your server scripts. One resource holds the bot token, and every other resource asks it through exports: is this player a cop on Discord, give them a role, log this to a channel, ban that account, post a status message, answer a slash command. Around 150 exports with the same conventions, plus the things every server ends up building on top: a whitelist, batched logs, a chat bridge, a status message, job roles and rich presence.
It is standalone: no framework, no database, no other resource needed. es_extended, qb-core and qbx_core are only
used for job roles, the chat resource for the chat bridge and the chat log, and baseevents
for the death log. np_discord has no UI.
if exports.np_discord:HasRole(source, 'police') then
exports.np_discord:Log('duty', { description = 'Went on duty', colour = 'blue', player = source })
end
At a glance ​
- Players are Discord users. Every function takes a player
source, a Discord id ordiscord:<id>. Roles, channels and guilds can be named in the config ('police') or passed by id or by their name on Discord. See Exports → Conventions. - Finds everything by itself. Every guild the bot is in, with its roles and channels, is loaded at start and
available by name (
HasRole(source, 'Admin'),'lsmd/Doctor'for other guilds) without listing a single id. See Guilds & discovery. - Several guilds. A main server plus team and faction servers (LSPD, LSMD, FIB …). Roles and channels know which
guild they live in, so
HasRole(source, 'lspd-officer')asks the LSPD guild without being told. - Members and roles:
GetMember,HasRole/HasAnyRole/HasAllRoles,AddRole,RemoveRole,SetRoles,SetNickname, display name, avatar, account age, boosting, computed permissions. See Members & roles. - Moderation and voice: timeout, kick, ban, unban, bulk ban, ban lookup, audit log; move, disconnect, mute and deafen members. See Moderation & voice.
- Messages: send, edit, delete, pin, react, reply, embeds, buttons, text files, DMs, threads and forum posts. Text is cut to Discord's limits and nothing pings anyone unless you ask for it. See Messages & channels.
- Webhooks and logs:
Log('name', entry)returns at once; entries are queued and sent ten embeds per message. Webhook urls live in convars, not in files. See Webhooks & logs. - Slash commands, buttons, select menus and modals with handlers that get the in-game
sourceof the Discord user. See Slash commands. - Live updates through the gateway: a role given on Discord reaches the game within a second. Without the gateway a poll does the same, slower. See Gateway.
- Ready-made: connection whitelist, in-game chat ↔ Discord channel, a self-updating status message, the bot's activity line, job → role sync, client rich presence. See Ready-made features.
- Few requests, rate limits handled: guilds, roles, channels and emojis live in memory; members and users are cached; identical lookups share one request; one queue per route, retries on 429 and 5xx.
- Client side: each client gets its own profile (avatar, display name, roles) for UIs. See Client.
Not possible with a bot token, so not included: a user's e-mail address (that needs an OAuth2 login) and binary file uploads (FiveM's HTTP client is text only).
Who uses it ​
| Resource | What it uses np_discord for |
|---|---|
| np_admin | Discord role → group sync (two-way if wanted), ban sync, audit log categories routed to log channels |
| np_prison | Admins and guards by Discord role, prison records in the log channel prison |
| np_shops | Admins by Discord role, the shops log channel |
| np_flightradar | Ground radar by Discord role, emergencies in a log channel |
| np_fuel | Staff by Discord role (Config.AdminDiscordRoles) |
| np_mechanic | Staff by Discord role (Config.AdminDiscordRoles) |
| np_banking | Staff by Discord role while np_admin is not running |
| np_gangs | Staff by Discord role while np_admin is not running |
| np_drugs | Admins by Discord role while np_admin is not running |
| np_environmental | Staff by Discord role, a staff log channel for rolls, spawns and clears |
| np_clothing | Staff by Discord role (Config.AdminDiscordRoles) |
All of them work without np_discord. np_discord itself registers its settings with np_admin when it runs (see Configuration → Editable in np_admin).
Quick example ​
-- fxmanifest.lua of your resource (optional shortcut): server_script '@np_discord/init.lua'
-- then Discord.X(...) is exports.np_discord:X(...)
RegisterCommand('duty', function(source)
if not Discord.HasAnyRole(source, { 'police', 'ems' }) then return end
Discord.AddRole(source, 'on-duty', 'Went on duty ingame')
Discord.SetNickname(source, '[LSPD] ' .. GetPlayerName(source))
end)
-- result, err: with an err Discord could not be asked, without one `false` simply means "no"
local member, err = Discord.GetMember(source)
if err then print(err.message) elseif not member then print('not on our Discord') end
-- throwaway accounts
if Discord.GetAccountAge(source) < 7 * 86400 then DropPlayer(source, 'Your Discord account is too new.') end
-- roles changed on Discord while the player is online
AddEventHandler('np_discord:playerRolesChanged', function(source, roles, added, removed)
print(('%s gained %d and lost %d roles'):format(GetPlayerName(source), #added, #removed))
end)
-- anything there is no function for
local onboarding = Discord.Request('GET', ('guilds/%s/onboarding'):format(Discord.GetGuildId()))
Functions that talk to Discord wait for the answer, so call them from an event handler, a command or a thread.