Class: SpikeyBot

SpikeyBot

Main class that manages the bot.

new SpikeyBot()

Source:
Listens to Events:
  • Discord~Client#event:ready
  • Discord~Client#event:message
  • Command#event:updateGame
  • Command#event:reboot
  • Command#event:mainreload

Members


<constant> fqdn :string

The fully qualified domain name of this host.
Type:
  • string
Source:

<constant> startTimestamp :number

Timestamp at which this process was started.
Type:
  • number
Source:

<constant> version :string

The current bot version parsed from package.json.
Type:
  • string
Source:

<private, inner> botName :string

The name of the client secret to use. Defaults to release either release or dev depending on the --dev flag.
Type:
  • string
Source:

<private, inner> command :Command

The current instance of Command.
Type:
Source:

<private, inner, constant> commandFilename :string

The filename of the Command mainModule.
Type:
  • string
Default Value:
  • ./commands.js
Source:

<private, inner> delayBoot :number

Number of milliseconds to delay the call to client.login in order to prevent race conditions of multiple bots in the same directory. This is set with the `--delay` flag. `--delay` with no value will default to 5000 milliseconds.
Type:
  • number
Source:

<private, inner, nullable> disconnectReason :string

Reason the bot was disconnected from Discord's servers.
Type:
  • string
Source:

<private, inner> enableSharding :boolean

Whether or not to spawn the bot as multiple shards. Enabled with `--shards` cli argument.
Type:
  • boolean
Source:

<private, inner, constant> fullRebootFilename :string

The full filename where information about the bot rebooting is stored.
Type:
  • string
Source:
See:
  • rebootFilename

<private, inner, constant> guildCustomPrefixFile :string

The path in the guild's subdirectory where we store custom prefixes for bots with custom names.
Type:
  • string
Default Value:
  • /prefixes.json
Source:

<private, inner, constant> guildPrefixes :object.<string>

Cache of all loaded guild's command prefixes. Populated asyncronously after client ready event.
Type:
  • object.<string>
Source:

<private, inner, constant> guildPrefixFile :string

The path in the guild's subdirectory where we store custom prefixes.
Type:
  • string
Default Value:
  • /prefix.txt
Source:

<private, inner> initialized :boolean

Has the bot been initialized already.
Type:
  • boolean
Source:

<private, inner> inspectShard :number

Enable inspecting/profiling for a shard to launch. Set via cli flags, -1 to disable. Currently only supports enabling. The `--inspect` flag will be sent to all shards that are started. This is due to limitations of Discord~ShardingManager.
Type:
  • number
Source:

<private, inner> isBackup :boolean

Is this bot managing backup status monitoring.
Type:
  • boolean
Source:

<private, inner, constant> mainModuleListFile :string

Filename of which to load additional MainModule names. The file must be a valid JSON array of strings.
Type:
  • string
Default Value:
  • ./mainModules.json
Source:

<private, inner> mainModuleNames :Array.<string>

The list of all mainModules to load. Always includes SpikeyBot~commandFilename and SpikeyBot~smListFilename. Additional mainModules can be loaded from SpikeyBot~mainModuleListFile.
Type:
  • Array.<string>
Source:

<private, inner, constant> mainModules :Array.<MainModule>

Instances of MainModules currently loaded.
Type:
Source:

<private, inner> minimal :boolean

Should this bot only load minimal features as to not overlap with multiple instances.
Type:
  • boolean
Source:

<private, inner> noLogin :boolean

Do everything normally, except don't ever actually attempt to login to Discord.
Type:
  • boolean
Source:

<private, inner> numShards :number

The number of shards to use if sharding is enabled. 0 to let Discord decide. Set from `--shards=#` cli argument, or `SHARD_COUNT` environment variable.
Type:
  • number
Source:

<private, inner, constant> rebootFilename :string

Filename without file extension where information about the bot rebooting is stored.
Type:
  • string
Default Value:
  • ./save/reboot
Source:
See:
  • fullRebootFilename

<private, inner> rebooting :boolean

Is the bot currently rebooting.
Type:
  • boolean
Source:

<private, inner, constant> saveFrequency :number

The frequency at which saveInterval will run.
Type:
  • number
Default Value:
  • 2 Minutes
Source:
See:

<private, inner> saveInterval :Interval

The Interval in which we will save and purge data on all mainmodules. Begins after onReady.
Type:
  • Interval
Source:
See:

<private, inner> setDev :boolean

Is this bot running in development mode.
Type:
  • boolean
Source:

<private, inner, constant, nullable> shardId :number

The shard ID of this process. This is passed from the Shard Master using the `SHARDS` environment variable.
Type:
  • number
Source:

<private, inner, nullable> shardMem :number

Number of bytes to allocate for each shard memory. Passed as `--max-old-space-size=` to the spawned node process. Null for default.
Type:
  • number
Source:

<private, inner> smLoader :SMLoader

The current instance of SMLoader.
Type:
Source:

<private, inner, constant> smLoaderFilename :string

The filename of the SMLoader mainModule.
Type:
  • string
Default Value:
  • ./smLoader.js
Source:

<private, inner> testInstance :boolean

Is the bot started with the intent of solely running a unit test. Reduces messages sent that are unnecessary.
Type:
  • boolean
Source:

<private, inner> testMode :boolean

Is the bot currently responding as a unit test.
Type:
  • boolean
Source:

Methods


changePrefix(gId, newPrefix)

Change the command prefix for the given guild.
Parameters:
Name Type Description
gId string The guild id of which to change the command prefix.
newPrefix string The new prefix to set.
Source:

getBotName()

Getter for the bot's name. If name is null, it is most likely because there is no custom name and common.isRelease should be used instead.
Source:
See:
Returns:
The bot's name or null if it has not been defined yet or there is no custom name.
Type
string

getFullBotName()

Getter for the bot's name. If botName is null, this will give either `release` or `dev`.
Source:
See:
Returns:
The bot's name.
Type
string

getPrefix(id)

Get this guild's custom prefix. Returns the default prefix otherwise.
Parameters:
Name Type Description
id Discord~Guild | string | number The guild id or guild to lookup.
Source:
Returns:
The prefix for all commands in the given guild.
Type
string

reloadCommon()

Delete cache and re-require common.js and auth.js.
Source:

<private, inner> commandChangePrefix(msg)

Change the custom prefix for the given guild.
Parameters:
Name Type Description
msg Discord~Message Message that triggered command.
Source:
Listens to Events:
  • Command#event:changePrefix

<private, inner> commandReboot(msg [, silent] [, onlySelf])

Trigger a reboot of the bot. Actually just gracefully shuts down, and expects to be immediately restarted.
Parameters:
Name Type Argument Default Description
msg Discord~Message Message that triggered command.
silent boolean <optional>
false Suppress reboot scheduling messages.
onlySelf boolean <optional>
false Prevent sending reboot command to other shards if possible.
Source:
To Do:
  • Support scheduled reload across multiple shards. Currently the bot waits for the shard at which the command was sent to be ready for reboot instead of all shard deciding on their own when they're ready to reboot. This will also need to check that we are obeying Discord's rebooting rate limits to help reduce downtime.
Listens to Events:
  • Command#event:reboot

<private, inner> commandReload(msg)

Reload all mainmodules by unloading then re-requiring.
Parameters:
Name Type Description
msg Discord~Message Message that triggered command.
Source:
Listens to Events:
  • Command#event:mainreload

<private, inner> commandSaveAll(msg)

Trigger all mainModules to save their data.
Parameters:
Name Type Description
msg Discord~Message Message that triggered command.
Source:
See:
  • SpikeyBot~saveAll()
Listens to Events:
  • Command#event:saveAll

<private, inner> commandUpdateGame(msg)

Change current status message.
Parameters:
Name Type Description
msg Discord~Message Message that triggered command.
Source:
Listens to Events:
  • Command#event:updateGame

<private, inner> createShards()

Create a ShardingManager and spawn shards. This shall only be called at most once, and `login()` shall not be called after this.
Source:

<private, inner> exit(info)

Trigger a graceful shutdown with process signals. Does not trigger shutdown if exit is -1.
Parameters:
Name Type Argument Description
info * <repeatable>
Information about the signal.
Source:
Listens to Events:
  • event:SIGINT
  • event:SIGHUP
  • event:SIGTERM
  • process#event:exit

<private, inner> isCmd(msg, cmd)

Checks if given message is the given command.
Parameters:
Name Type Description
msg Discord~Message Message from Discord to check if it is the given command.
cmd string Command to check if the message is this command.
Source:
Returns:
True if msg is the given command.
Type
boolean

<private, inner> loadGuildPrefixes(guilds)

Load prefixes from file for the given guilds asynchronously.
Parameters:
Name Type Description
guilds Array.<Discord~Guild> Array of guilds to fetch the custom prefixes of.
Source:

<private, inner> login()

Login to Discord. This shall only be called at most once.
Source:

<private, inner> onDisconnect(event)

The bot has disconnected from Discord and will not be attempting to reconnect.
Parameters:
Name Type Description
event CloseEvent The websocket close event.
Source:
Listens to Events:
  • Discord~Client#event:disconnect

<private, inner> onInteraction(interaction)

Handle a command or interaction received.
Parameters:
Name Type Description
interaction Discord~BaseInteraction The interaction that was created.
Source:
Fires:
  • event:Command
Listens to Events:
  • Discord~Client#event:interactionCreate

<private, inner> onMessage(msg)

Handle a message sent.
Parameters:
Name Type Description
msg Discord~Message Message that was sent in Discord.
Source:
Fires:
  • event:Command
Listens to Events:
  • Discord~Client#event:messageCreate

<private, inner> onPresenceUpdate(oldMem, newMem)

Attempt to detect when the main bot goes offline by the presence changing.
Parameters:
Name Type Description
oldMem Discord~GuildMember Member before presence update.
newMem Discord~GuildMember Member after presence update.
Source:

<private, inner> onReady()

The bot has become ready.
Source:
Listens to Events:
  • Discord~Client#event:ready

<private, inner> onReconnecting()

The bot has disconnected from Discord, and is reconnecting.
Source:
Listens to Events:
  • Discord~Client#event:reconnecting

<private, inner> reloadMainModules( [toReload] [, reloaded] [, schedule])

Reloads mainmodules from file. Reloads all modules if `toReload` is not specified. `reloaded` will contain the list of messages describing which mainmodules were reloaded, or not.
Parameters:
Name Type Argument Default Description
toReload string | Array.<string> <optional>
Specify mainmodules to reload, or null to reload all mainmodules.
reloaded Array.<string> <optional>
Reference to a variable to store output status information about outcomes of attempting to reload mainmodules.
schedule boolean <optional>
true Automatically re-schedule reload for mainmodules if they are in an unloadable state.
Source:
Returns:
True if something failed and not all mainmodules were reloaded.
Type
boolean

<private, inner> saveAll()

Trigger all mainmodules to save their data.
Source:

<private, inner> updateGame(game [, type])

Changes the bot's status message.
Parameters:
Name Type Argument Default Description
game string New message to set game to.
type string <optional>
'WATCHING' The type of activity.
Source: