Skip to content

Members & roles ​

Players ​

Every online player with a linked Discord account is tracked. On join np_discord reads the player's member object in every tracked guild and builds a profile:

lua
{ linked, id, member, username, displayName, avatar, roles, roleNames, roleInfo, guilds, boosting }
  • member, the name, the avatar and boosting refer to the main guild.
  • roles holds the role ids of every guild in one list; guilds[name] = { id, member, roles } splits them up.
  • roleNames holds every name a held role answers to (config name, lower-case Discord name, 'guild/name').
  • roleInfo holds role badges for a UI: { id, name, colour, guild, position, icon, emoji }, main guild first, highest role first.

np_discord:playerReady (source, profile) fires once every tracked guild answered. After that, np_discord:playerRolesChanged (source, roles, added, removed, guildId) fires whenever a role changes on Discord, through the gateway or the Config.Sync poll. The player's client gets the same profile (see Client).

lua
AddEventHandler('np_discord:playerReady', function(source, profile)
  if not profile.linked then return end
  print(('%s is %s on Discord'):format(GetPlayerName(source), profile.displayName))
end)

The player exports make no request unless noted:

Export Returns
GetDiscordId(user) id nil, err (no_discord) when the player has no Discord linked
GetPlayerByDiscordId(user) source | false The online player that belongs to a Discord account
GetPlayerProfile(src) profile As synced on join
GetPlayersWithRole(role, guild?) sources Online players holding the role, in whichever guild it lives
SyncPlayer(src) profile Re-reads the player's roles from Discord now and fires the change events
CheckWhitelist(src) boolean, message Runs the Config.Connect checks by hand

Use GetPlayersWithRole for online players. GetMembersWithRole walks the whole member list of the guild.

Role checks ​

lua
if exports.np_discord:HasRole(source, 'police') then … end
if exports.np_discord:HasAnyRole(source, { 'mod', 'lspd-officer' }) then … end   -- roles may live in different guilds
if exports.np_discord:HasAllRoles(source, { 'police', 'swat' }) then … end
  • "No" is not an error. A player who is not on the guild, or has no such role, gets false without an err. With an err the answer is "could not find out". Either way the first value is falsy, so if HasRole(…) then is safe.
  • Members are cached (Config.Cache.member, 120 s; 1800 s while the gateway reports member changes). RefreshMember skips the cache.
  • HasPermission(user, 'BAN_MEMBERS') computes the member's permissions from their roles. With a channel, the channel's overwrites are applied.

Changing roles and nicknames ​

lua
exports.np_discord:AddRole(source, 'on-duty', 'Went on duty ingame')
exports.np_discord:RemoveRole(source, 'on-duty', 'Went off duty')
exports.np_discord:SetRoles(source, { 'member', 'police' }, 'Reset roles')   -- one guild, one request
exports.np_discord:SetNickname(source, '[LSPD] ' .. GetPlayerName(source))  -- nil or '' resets, cut to 32 characters

The bot needs Manage Roles and its role must sit above the roles it gives (otherwise 50013). Nicknames need Manage Nicknames; Discord never lets a bot rename the guild owner.

Users ​

GetUser, GetUsername, GetDisplayName and GetAvatar work for anyone, not only members. GetCreatedAt and GetAccountAge are read from the id, without a request, which makes a throwaway-account check cheap:

lua
if exports.np_discord:GetAccountAge(source) < 7 * 86400 then
  DropPlayer(source, 'Your Discord account is too new.')
end

SendDM fails with code 50007 when the user blocks DMs from server members.

Roles in the guild ​

Roles are kept in memory (see Guilds & discovery). GetRoleByName is case-insensitive and searches main and Config.Guilds without a guild argument. CreateRole takes { name, colour, hoist, mentionable, permissions = { 'KICK_MEMBERS' }, icon, unicode_emoji }.

Exports ​

Full tables: Exports → Members, Users, Roles, Players.