Class: Common

Common


new Common()

Commonly required things. Helper functions and constants.
Source:

Members


<static, constant> avatarURL :string

The website base URL for pointing to for avatar files.
Type:
  • string
Source:

<static, constant> encryptAvatars :boolean

Whether to use the encryption specified in auth.js to encrypt users' avatars. Could make it more difficult to serve the images if enabled.
Type:
  • boolean
Source:

<static> fileFetchDelay :number

Cooldown after fetching a file from other shards before requesting the new version again.
Type:
  • number
Default Value:
  • 30 seconds
Source:

<static> fileFetchHist :object.<number>

Filenames and the timestamp they were last fetched from or sent to other shards.
Type:
  • object.<number>
Default Value:
  • {}
Source:

<static> fmtDAPIErr

Format a Discord API error.
Source:

<static, constant> guildSaveDir :string

The root file directory for finding saved data related to individual guilds.
Type:
  • string
Source:

<static, constant> logChannel :string

The channel id for the channel to send general log messages to.
Type:
  • string
Source:

<static> mention

Creates formatted string for mentioning the author of msg.
Source:

<static, constant> shardConfigRegex :RegExp

Regular expression the name of the config file will match for a shard configuration.
Type:
  • RegExp
Source:

<static, constant> spikeyId :string

SpikeyRobot's Discord ID.
Type:
  • string
Source:

<static, constant> testChannel :string

The channel id for the channel to reserve for only unit testing in.
Type:
  • string
Default Value:
  • 439642818084995074
Source:

<static, constant> trustedIds :Array.<string>

Trusted IDs.
Type:
  • Array.<string>
Source:
See:
  • Common.prototype.trustedIds

<static, constant> ua :string

The User-Agent to send in http request headers.
Type:
  • string
Source:

<static, constant> userSaveDir :string

The root file directory for finding saved data related to individual users.
Type:
  • string
Source:

<static, constant> webHelp :string

The website full URL for commands help page.
Type:
  • string
Source:

<static, constant> webPath :string

The website path for more help and documentation.
Type:
  • string
Source:

<static, constant> webURL :string

The website base URL for pointing to for more help and documentation.
Type:
  • string
Source:

<constant> avatarURL :string

The website base URL for pointing to for avatar files.
Type:
  • string
Default Value:
  • https://kamino.spikeybot.com/
Source:

<constant> encryptAvatars :boolean

Whether to use the encryption specified in auth.js to encrypt users' avatars. Could make it more difficult to serve the images if enabled.
Type:
  • boolean
Source:

<constant> guildSaveDir :string

The root file directory for finding saved data related to individual guilds.
Type:
  • string
Default Value:
  • ./save/guilds/
Source:

isMaster :boolean

Is this a shard that is being managed by our sharding system, and is the master that all children (siblings) will be connecting to. If this is true, webservers should attempt to become the master, and not fallback to a client sibling. This is specified using the `SHARDING_MASTER` environment variable.
Type:
  • boolean
Default Value:
  • false
Source:

isRelease :boolean

Whether this should be shown as a release version, or a debug version in the log.
Type:
  • boolean
Source:

isSlave :boolean

Is this a shard that is being managed by our sharding system. If this is true, webservers should not attempt to become the master, and instead attempt to connect to the sharding master server. This is specified using the `SHARDING_SLAVE` environment variable.
Type:
  • boolean
Default Value:
  • false
Source:

isTest :boolean

Whether this current instance is running as a unit test.
Type:
  • boolean
Source:

<constant> logChannel :string

The channel id for the channel to send general log messages to.
Type:
  • string
Default Value:
  • 473935520821673991
Source:

<private, nullable> masterHost :object

The network host information where the master node is located for connecting sibling sockets to. This will be populated if `isSlave` is true.
Type:
  • object
Source:

<constant> shardConfigRegex :RegExp

Regular expression the name of the config file will match for a shard configuration.
Type:
  • RegExp
Default Value:
  • /^shard_[a-z]+_config\.json$/
Source:

<constant> spikeyId :string

SpikeyRobot's Discord ID. If you are self-hosting SpikeyBot, change this to your account ID to be able to give yourself full access to all features of the bot.
Type:
  • string
Default Value:
  • 124733888177111041
Source:

<constant> trustedIds :Array.<string>

Discord IDs that are allowed to reboot the bot, and are overall trusted individuals/accounts.
Type:
  • Array.<string>
Source:

<constant> userSaveDir :string

The root file directory for finding saved data related to individual users.
Type:
  • string
Default Value:
  • ./save/users/
Source:

<constant> webHelp :string

The website full URL for commands help page.
Type:
  • string
Source:

<constant> webPath :string

The website path for more help and documentation.
Type:
  • string
Default Value:
  • help/
Source:

<constant> webURL :string

The website base URL for pointing to for more help and documentation.
Type:
  • string
Default Value:
  • https://www.spikeybot.com/
Source:

<private, inner, constant> app :string

The script's filename to show in the log.
Type:
  • string
Source:

<private, inner> mycolor :number

The color code to prefix log messages with for this script.
Type:
  • number
Source:

<private, inner, constant> prefixLength :number

The number of characters reserved for the filename of the script.
Type:
  • number
Default Value:
  • 14
Source:

<private, inner> title :string

The final formatted filename for logging.
Type:
  • string
Source:

Methods


<private, static> camelToSpaces(str)

Convert a string in camelcase to a human readable spaces format. (helloWorld --> Hello World).
Parameters:
Name Type Description
str string The input.
Source:
Returns:
The output.
Type
string

<static> connectSQL( [force])

Create initial connection with sql server. The connection is injected into the global scope as sqlCon. If a connection still exists, calling this function just returns the current reference.
Parameters:
Name Type Argument Default Description
force boolean <optional>
false Force a new connection to be established.
Source:
Returns:
Current sql connection object.
Type
sql.ConnectionConfig

<static> deepFreeze(object)

Recursively freeze all elements of an object.
Parameters:
Name Type Description
object object The object to deep freeze.
Source:
Returns:
The frozen object.
Type
object

<static> mkAndWrite(filename, dir, data [, cb], encrypt)

Write data to a file and make sure the directory exists or create it if it doesn't. Async.
Parameters:
Name Type Argument Description
filename string The name of the file including the directory.
dir string The directory path without the file's name.
data string | object The data to write to the file.
cb function <optional>
Callback to fire on completion. Only parameter is optional error.
encrypt boolean Encrypt and append ".crypt" to filename if true
Source:
See:
  • Common~mkAndWriteSync

<static> mkAndWriteSync(filename, dir, data)

Write data to a file and make sure the directory exists or create it if it doesn't. Synchronous.
Parameters:
Name Type Description
filename string The name of the file including the directory.
dir string The directory path without the file's name.
data string The data to write to the file.
Source:
See:
  • Common~mkAndWrite

<static> parse(str [, cb])

Parse a JSON string.
Parameters:
Name Type Argument Description
str string String to parse.
cb function <optional>
Callback once completed, with optional error parameter, and parameter of parsed data.
Source:

<static> readAndParse(filename [, cb])

Read a file's contents, checks other shards for newer version first, and parse the file's contents info from JSON to objects. This does not have a synchronous version as the whole point of this is to potentially fetch a newer file from another server.
Parameters:
Name Type Argument Description
filename string The name of the file to read and parse.
cb function <optional>
Callback once completed, with optional error parameter, and parameter of parsed contents.
Source:

<static> readFile(filename [, cb] [, encrypt])

Read a file's contents, checks other shards for newer version first. This does not have a synchronous version as the whole point of this is to potentially fetch a newer file from another server.
Parameters:
Name Type Argument Default Description
filename string The name of the file to read.
cb function <optional>
Callback once completed, with optional error parameter, and parameter of file contents from `fs.readFile`.
encrypt boolean <optional>
true Enable encryption.
Source:

Delete data from file, and mark it for deletion from other shards.
Parameters:
Name Type Argument Description
filename string The name of the file to remove.
cb function <optional>
Callback once completed, with optional error parameter.
Source:

<static> unlinkSync(filename)

Delete data from file, and mark it for deletion from other shards. Synchronous.
Parameters:
Name Type Description
filename string The name of the file to remove.
Source:

begin(isTest, isRelease)

Initialize variables and settings for logging properly.
Parameters:
Name Type Description
isTest boolean Is this running as a test.
isRelease boolean Is this a release version, or a development version of the app running.
Source:

error(message, ip [, traceIncrease])

Format an error message to be logged.
Parameters:
Name Type Argument Default Description
message string The message to display.
ip string The IP address or unique identifier of the client that caused this event to happen.
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source:

fmtDAPIErr(e)

Format a Discord API error.
Parameters:
Name Type Description
e Discord~DiscordAPIError DiscordAPIError to format into a string.
Source:
Returns:
Error formatted as single line string.
Type
string

getFile(filename, cb)

Request that the ShardingSlave fetches a file from the ShardingMaster.
Parameters:
Name Type Description
filename string The name of the file to fetch with path relative to project directory.
cb function Callback with optional error.
Source:

getIPName(ip)

Formats a given IP address by padding with zeroes, or completely replacing with a human readable alias if the address is a known location.
Parameters:
Name Type Description
ip string The ip address to format.
Source:
Returns:
The formmatted address.
Type
string

log(message, ip [, traceIncrease])

Format a log message to be logged.
Parameters:
Name Type Argument Default Description
message string The message to display.
ip string The IP address or unique identifier of the client that caused this event to happen.
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source:

logDebug(message, ip [, traceIncrease])

Format a log message to be logged. Prefixed with DBG.
Parameters:
Name Type Argument Default Description
message string The message to display.
ip string The IP address or unique identifier of the client that caused this event to happen.
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source:

logWarning(message, ip [, traceIncrease])

Format a log message to be logged. Prefixed with WRN.
Parameters:
Name Type Argument Default Description
message string The message to display.
ip string The IP address or unique identifier of the client that caused this event to happen.
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source:

mention(msg)

Creates formatted string for mentioning the author of msg.
Parameters:
Name Type Description
msg Discord~Message | Discord~UserResolvable Message to format a mention for the author of.
Source:
Returns:
Formatted mention string.
Type
string

padIp(str)

Pad an IP address with zeroes.
Parameters:
Name Type Description
str number The ipv4 address as a string to format.
Source:
Returns:
The padded address.
Type
string

reply(msg, text [, post])

Replies to the author and channel of msg with the given message.
Parameters:
Name Type Argument Description
msg Discord~Message Message to reply to.
text string The main body of the message.
post string <optional>
The footer of the message.
Source:
Returns:
Promise of Discord~Message that we attempted to send, or null if error occurred before attempting to send.
Type
Promise

sendFile(filename, cb)

Request that the ShardingSlave sends a file to the ShardingMaster.
Parameters:
Name Type Description
filename string The name of the file to send with path relative to project directory.
cb function Callback with optional error.
Source:

sendSQL(query, cb)

Send an SQL query to the master database through our open websocket.
Parameters:
Name Type Description
query string SQL query to send directly to the database.
cb function Callback with optional error, otherwise returned rows.
Source:

updatePrefix(ip)

Format a prefix for a log message or error. Includes the ip before the message.
Parameters:
Name Type Description
ip string The ip to include in the prefix.
Source:
Returns:
The formatted prefix for a log message.
Type
string

<private, inner> __filename( [inc])

Gets the name of the file that called a log function.
Parameters:
Name Type Argument Default Description
inc number <optional>
0 Increase distance up the stack to returns.
Source:
Returns:
Filename in call stack.
Type
string

<private, inner> __line( [inc])

Gets the line number of the function that called a log function.
Parameters:
Name Type Argument Default Description
inc number <optional>
0 Increase distance up the stack to returns.
Source:
Returns:
Line number of call in stack.
Type
number

<private, inner> getTrace( [traceIncrease])

Gets the name and line number of the current function stack.
Parameters:
Name Type Argument Default Description
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source:
Returns:
Formatted string with length 24.
Type
string

<private, inner> write(prefix, message, ip [, traceIncrease])

Write the final portion of the log message.
Parameters:
Name Type Argument Default Description
prefix string The first characters on the line.
message string The message to display.
ip string The IP address or unique identifier of the client that caused this event to happen.
traceIncrease number <optional>
0 Increase the distance up the stack to show the in the log.
Source: