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_CREATEfor 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 everyConfig.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_roleinstead of a guess. ResolveRole(role, guild?)andResolveChannel(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:
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 everyConfig.Guildsguild. An explicitguildargument 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 quietfalse.SetRolesis the exception: it replaces the roles of one guild, so mixing fails withmixed_guilds. - Channel permissions (
HasPermissionwith a channel,SetChannelPermission,EditChannel) use the roles and@everyoneof the channel's guild. - Online players are tracked in every configured guild. The profile has
roles(all guilds in one list) andguilds[name] = { id, member, roles }.np_discord:playerReadywaits until every guild answered;np_discord:playerRolesChangedcarries the guild where the change happened. - Whitelist and job roles may use roles from any guild;
Config.Connect.requireMembertakes a list of guilds. - Slash commands are registered in
guild, inguilds = { … }, in every configured guild (guilds = 'all') or globally. One handler serves all of them;interaction.guild_idsays 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_MEMBERSintent) forConfig.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, soGetUser,GetUsernameandGetAvatarrarely need a request. - Identical lookups that run at the same time share one request.
GetStats().cacheshows hits, misses and entries;GetStats().registrythe 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.