Skip to content

Ready-made features ​

The things every server ends up building on top of a Discord bot. All of them are off by default and switched on in config.lua (see Configuration) or, with np_admin, on its settings page.

Whitelist ​

lua
Config.Connect = {
  enabled = true,
  requireDiscord = true,
  requireMember = true,                 -- or { 'main', 'staff' }: member of all of these
  roles = { 'whitelisted', 'staff' },   -- at least one, from any guild
  blockedRoles = { 'banned' },
  bypass = { 'license:abc…' },
  failOpen = false,
}

While a player connects (playerConnecting, deferred) np_discord checks, in this order:

  1. Bypass: an identifier in bypass lets the player in without asking Discord.
  2. Linked Discord: without one, requireDiscord refuses with messages.noDiscord.
  3. Membership: every guild in requireMember, and every guild a listed role lives in, is asked fresh (a role given a second ago counts). Not a member of a required guild: messages.notMember.
  4. Blocked roles: holding any of blockedRoles refuses with messages.blocked.
  5. Roles: with roles set, the player needs one of them, otherwise messages.missingRole.

When Discord cannot be asked, failOpen decides: false (default) refuses with messages.error, true lets the player in. A role in roles or blockedRoles that cannot be resolved refuses every connection and says so in the console, rather than quietly turning the whitelist off.

CheckWhitelist(src) runs the same checks by hand and returns allowed, message.

np_admin has a simpler requireRole option; this whitelist is the better place for it (see np_admin → Discord).

Chat bridge ​

lua
Config.Chat = { enabled = true, channel = 'chat', toDiscord = true, toGame = true }
  • Game → Discord: chat messages (from the chat resource) are collected and sent once every 1.5 s, as one message of up to 1900 characters, so a busy chat stays inside the rate limit. Each line uses discordFormat ('**{name}** [{id}]: {message}'); names and text are stripped of colour codes and escaped, so players cannot ping or format. Messages starting with / are skipped while ignoreCommands is on.
  • Discord → game: needs the gateway with GUILD_MESSAGES and MESSAGE_CONTENT. Messages in the channel from people (not bots, not webhooks) appear in the in-game chat as [Discord] Name: text in gameColour, cut to 256 characters.

Status message ​

lua
Config.Status = { enabled = true, channel = 'status', interval = 60, connect = 'cfx.re/join/abcdef' }

The bot posts one embed in the channel and edits it every interval seconds (at least 15): title ('{server}'), players online / max, the connect address, and with showPlayers the player names (up to 40). The message id is kept in the resource KVP, so a restart edits the same message. If someone deletes it, a new one is posted.

Bot activity ​

Config.Status.activity (default '{players}/{max} players') is the bot's custom status line. It needs the gateway, not Config.Status.enabled. Joins and leaves are collected for 15 s before the line is updated, because Discord allows only a few presence updates per 20 s. nil leaves the bot's activity alone, so you can set it yourself with SetBotActivity.

Placeholders for the status title and the activity: {players}, {max} (sv_maxclients), {server} (sv_projectName, else sv_hostname, without colour codes).

Job roles ​

lua
Config.JobRoles = {
  enabled = true,
  removeOld = true,
  jobs = {
    police = 'police',
    ambulance = { role = 'ems', grades = { [3] = 'ems-lead' } },
    fib = 'fib-agent',                          -- listed under Config.Guilds.fib: given in that guild
  },
}
  • Detected by their events: es_extended (esx:playerLoaded, esx:setJob) and qb-core / qbx_core (QBCore:Server:PlayerLoaded, QBCore:Server:OnJobUpdate). No framework is required to start np_discord.
  • On load and on every job change the roles of the new job (and grade) are added if missing. With removeOld, the roles of the previous job are taken away.
  • Changes for one player run one after another, so quick job changes cannot leave the wrong roles.
  • Roles are only ever added or removed for mapped jobs; other roles are never touched.

The bot needs Manage Roles and a role above the mapped roles. A map edited in np_admin goes through JSON, so grade keys work as strings too.

Rich presence ​

Config.Presence puts your server on the player's own Discord profile: text, images and up to two buttons. See Client → Rich presence.