Skip to content

Guilds & discovery ​

You don't have to list anything to get started. np_discord finds the guilds the bot is in, their roles and their channels, and lets you use them by name.

What is loaded at start ​

At start np_discord reads every guild the bot is in, and of each tracked guild its guild object, roles and channels (three requests per guild, once). When that is done, IsReady() is true and np_discord:ready fires with { { id, name, label } } (error instead of label for a guild that failed).

  • With the gateway the data comes with Discord's GUILD_CREATE for free and stays current through Discord's events (role created / renamed / deleted, channels, emojis, the bot joining or leaving a guild). Nothing is asked for again.
  • Without the gateway the copy in memory is served at once and refreshed in the background when it is older than Config.Cache.*. Callers never wait for a refresh. The guild list is re-read every Config.Discovery.interval (600 s).
  • RefreshGuilds(guild?) re-reads the guild list and roles / channels now.

Names ​

A role, channel or guild can be passed in several ways:

Kind Accepted
role A name from Config.Roles or a guild's roles, a role id, the role's name on Discord (any case, or as a slug: 'LSPD Officer' = 'lspd-officer'), or 'guild/name' ('lsmd/Doctor')
channel A name from Config.Channels or a guild's channels, a channel id, the channel's name on Discord, or 'guild/name'
guild 'main', a name from Config.Guilds, the name of a discovered guild, its name on Discord, or a guild id
  • Discovered guilds are named after their Discord name: "LSMD | Los Santos Medical" → 'lsmd-los-santos-medical'. Two guilds with the same name get the end of their id appended ('fib-0003'). Config names always win. GetBotGuilds() lists every guild with the name to pass (name) and its Discord name (label).
  • Bare names search only the guilds you configured (main and Config.Guilds). Role names are what staff checks are written with, and a partner server the bot was invited to must never be able to grant "Admin" on yours. Roles and channels of other guilds are one step away: 'lsmd-los-santos-medical/Doctor', the guild argument, or the id (GetAllRoles() lists them). The error for a bare name says where it exists.
  • When a name exists in several configured guilds, the main guild's wins. If only other guilds have it, you get ambiguous_role instead of a guess.
  • ResolveRole(role, guild?) and ResolveChannel(channel, guild?) turn a name into the id and the guild it belongs to.

Several guilds ​

The guild from discord_guild_id is always called main. More guilds (team server, LSPD, LSMD, FIB …) go into Config.Guilds, each with its own roles and channels:

lua
Config.Guilds = {
  staff = '123456789012345678',
  lspd = { id = '…', roles = { ['lspd-officer'] = '…', ['lspd-chief'] = '…' }, channels = { ['lspd-radio'] = '…' } },
}
  • A role or channel knows its guild. HasRole(source, 'lspd-officer') asks the LSPD guild, AddRole(source, 'lspd-chief') gives the role there, SendMessage('lspd-radio', …) posts there. A bare id or a Discord name is looked for in main and every Config.Guilds guild. An explicit guild argument always wins.
  • Lists of roles may mix guilds. HasAnyRole(source, { 'mod', 'lspd-officer' }) checks each role where it lives. If one of those guilds cannot be asked, the answer is an error, never a quiet false. SetRoles is the exception: it replaces the roles of one guild, so mixing fails with mixed_guilds.
  • Channel permissions (HasPermission with a channel, SetChannelPermission, EditChannel) use the roles and @everyone of the channel's guild.
  • Online players are tracked in every configured guild. The profile has roles (all guilds in one list) and guilds[name] = { id, member, roles }. np_discord:playerReady waits until every guild answered; np_discord:playerRolesChanged carries the guild where the change happened.
  • Whitelist and job roles may use roles from any guild; Config.Connect.requireMember takes a list of guilds.
  • Slash commands are registered in guild, in guilds = { … }, in every configured guild (guilds = 'all') or globally. One handler serves all of them; interaction.guild_id says where it was used.

Tracking ​

Config.Discovery.track = 'all' (the default) tracks every guild the bot is in like a configured one: online players' roles are read there too. With more than maxTracked (10) discovered guilds besides main and Config.Guilds, none of them are tracked and the console says so. 'configured' tracks only main and Config.Guilds; other guilds still work when you name them.

When a guild becomes tracked (found at start, or the bot joined it) np_discord:guildAdded fires and every online player is read there. When the bot leaves a guild, or it is no longer tracked, np_discord:guildRemoved fires.

Caching ​

  • Members are cached for Config.Cache.member (120 s); while the gateway reports member changes (GUILD_MEMBERS intent) for Config.Cache.memberLive (1800 s). All cached members are dropped if the gateway disconnects.
  • Every member that passes through (lookups, ListMembers, GetAllMembers, SearchMembers, gateway events) also fills the member and user cache, so GetUser, GetUsername and GetAvatar rarely need a request.
  • Identical lookups that run at the same time share one request.
  • GetStats().cache shows hits, misses and entries; GetStats().registry the guild counts. ClearCache(prefix?) drops 'member:', 'user:', 'roles:', 'channels:', 'guild:', 'emojis:' or everything.

Exports ​

Export Returns
GetBotGuilds() { { id, name, label, icon, owner, tracked, available } } Every guild the bot is in. No request
GetAllRoles() roles Every role of every tracked guild, each with guild_id, highest first. From memory
RefreshGuilds(guild?) true Re-read the guild list and roles / channels now
IsReady() boolean The start-up load is done
GetGuilds() { { id, name, label } } Every tracked guild, main first. No request
GetGuildName(guildId) name | false false for a guild the bot is not in
GetGuildId(guild?) id Resolves a name. No request
GetMemberRolesByGuild(user) { [guild name] = role ids | false } false = not a member there
GetMemberGuilds(user) names Tracked guilds the user is a member of
GetPlayersInGuild(guild?) sources Online players that are members of the guild. No request

The guild management exports (GetGuild, GetMemberCount, ModifyGuild …) are in Exports → Guild.