Client ​
client
The client only knows its own Discord profile, and only for display: avatars, names and role badges in a UI.
Whether a player may do something is decided on the server (exports.np_discord:HasRole(source, …)); a client can claim
whatever it likes.
lua
local profile = exports.np_discord:GetProfile() -- { displayName, avatar, roles, roleNames, member, ... }
SendNUIMessage({ avatar = profile.avatar, name = profile.displayName })
exports.np_discord:SetPresence('Robbing the Pacific Standard', 600)
Profile ​
When the session has started, the client asks the server for its profile once (np_discord:requestProfile). The server
answers with np_discord:profile and sends it again whenever the roles change.
lua
{ linked, id, member, username, displayName, avatar, roles, roleNames, roleInfo, guilds, boosting }
The fields are the same as the server's player profile. If a guild did not answer when the
player joined, the profile arrives without roles and with pending = true; the server keeps retrying.
lua
AddEventHandler('np_discord:profile', function(profile)
SendNUIMessage({ action = 'badges', roles = profile.roleInfo })
end)
The exports wait up to five seconds for the first profile.
| Export | Returns | |
|---|---|---|
GetProfile() |
profile | |
IsLoaded() |
boolean | The profile has arrived |
GetDiscordId() |
id | |
GetAvatar() |
url | |
GetDisplayName() |
string | |
IsMember(guild) |
boolean | Without guild the main guild |
GetRoles(guild) |
role ids | Without guild every guild in one list |
GetGuilds() |
names | Tracked guilds the player is a member of |
GetRoleInfo() |
badges | { { id, name, colour, guild, position, icon, emoji } } of the player's roles |
HasRole(role) |
boolean | Config name, Discord name (any case; main + Config.Guilds), 'guild/name' or id |
HasAnyRole(roles) |
boolean | |
SetPresence(text, seconds?) |
Overrides the rich presence text | |
ResetPresence() |
Rich presence ​
lua
Config.Presence = {
enabled = true,
appId = '123456789012345678', -- the application that owns the art assets
largeAsset = 'logo', largeText = '{server}',
smallAsset = nil, smallText = nil,
text = '{players}/{max} players',
buttons = { { label = 'Join', url = 'https://cfx.re/join/abcdef' } }, -- max 2
interval = 30,
}
Config.Presenceis the only part of the config sent to clients. It reaches each player once, on join, so changes (also from np_admin) apply on the next join.- Placeholders:
{players},{max},{server}(from global state the server keeps current),{name}and{id}(the player's own name and server id). - Upload the asset images to your application in the developer portal (Rich Presence → Art Assets) and use their
names as
largeAsset/smallAsset. - Other resources replace the text with
SetPresence(text, seconds?), forsecondsor untilResetPresence():
lua
exports.np_discord:SetPresence('In a police chase', 120)