Class: WebApi

WebApi

Handles receiving webhooks and other API requests from external services.

new WebApi()

Creates SubModule instance.
Source:

Extends

Members


<private, static> _fetchGameHost

Get the host data for requesting game information from Twitch.
Source:

<private, nullable> _app :http.Server

Web server listening for requests, fires WebCommands._handler on requests.
Type:
  • http.Server
Source:

<private, constant> _endpoints :object.<ApiEndpoint>

Data representing available API endpoints, and where to redirect data.
Type:
Default Value:
  • {"undefined":""}
Source:

<private> _proxy

Internal proxy server for forwarding api requests to other endpoints.
Source:

<private, constant> _rateHistory :object

History of requests for rate limiting purposes, mapped by user token.
Type:
  • object
Default Value:
  • {}
Source:

<private, constant> _rateLimitFile :string

File storing website rate limit specifications.
Type:
  • string
Default Value:
  • ./save/webRateLimits.json
Source:

<private> _rateLimits :object

Object storing parsed rate limit info from WebApi~_rateLimitFile.
Type:
  • object
Default Value:
  • {"commands":"","groups":""}
Source:

<nullable> bot :SpikeyBot

The parent SpikeyBot instance. Defaults to required cache value for autocompletion, updates to current reference at init.
Type:
Inherited From:
Overrides:
Source:

client :Discord~Client

The current bot client. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
  • Discord~Client
Inherited From:
Overrides:
Source:

command :Command

The command object for registering command listeners. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
Inherited From:
Overrides:
Source:

<constant> commit :string

The commit at HEAD at the time this module was loaded. Essentially the version of this submodule.
Type:
  • string
Inherited From:
Overrides:
Source:

common :Common

The common object. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
Inherited From:
Overrides:
Source:

Discord :Discord

The current Discord object instance of the bot. Defaults to require cache value for editor autocompletion, updates to current reference at init.
Type:
  • Discord
Inherited From:
Overrides:
Source:

<abstract> helpMessage :undefined|string|Discord~EmbedBuilder

The help message to show the user in the main help message.
Type:
  • undefined | string | Discord~EmbedBuilder
Inherited From:
Overrides:
Source:

<protected, readonly> initialized :boolean

Has this subModule been initialized yet (Has begin() been called).
Type:
  • boolean
Inherited From:
Overrides:
Source:

<constant> loadTime :number

The time at which this module was loaded for use in checking if the module needs to be reloaded because the file has been modified since loading.
Type:
  • number
Inherited From:
Overrides:
Source:

<abstract, protected> myName :string

The name of this submodule. Used for differentiating in the log. Should be defined before begin().
Type:
  • string
Inherited From:
Overrides:
Source:

<abstract> postPrefix :string

The postfix for the global prefix for this subModule. Must be defined before begin(), otherwise it is ignored.
Type:
  • string
Inherited From:
Overrides:
Source:

Methods


<private> _apiReady(userData, reqData, res, ip)

All necessary request data has been collected for an endpoint. Handle finally firing request internally.
Parameters:
Name Type Description
userData WebUserData The user data for the request.
reqData ApiRequestBody Data sent in body of request to send to endpoint.
res http.ServerResponse Our response to the api request.
ip string Client IP for logging.
Source:

<private> _apiRequest(req, res, url, ip)

Handles requests to the API endpoint.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _checkRateLimit(cmd, token)

Check if this current connection or user is being rate limited.
Parameters:
Name Type Description
cmd string The command being run.
token string The user's token used, equivalent of the user ID for purposes of rate limiting.
Source:
Returns:
Current rate limiting information for the given request and user data.
Type
Object

<private> _fetchWebhookMetadata(userId, gameId, content [, data])

Fetch the necessary data for the parsed webhook request, then continue to sending alerts.
Parameters:
Name Type Argument Default Description
userId string The Twitch user ID the request is for.
gameId string The Twitch game ID the user is playing.
content string Full content string from the webhook to re-broadcast to shards.
data object <optional>
{} Parsed content to object. Allows for additional metadata to be stored and to prevent duplicate alerts.
Source:

<private> _handler(req, res)

Handler for all http requests.
Parameters:
Name Type Description
req http.IncomingMessage The client's request.
res http.ServerResponse Our response to the client.
Source:

<private> _parseTwitchGameResponse(content, cb)

Parse the response from Twitch containing information about a game.
Parameters:
Name Type Description
content string Received content from request to Twitch API.
cb function First parameter is optional error string, otherwise the second parameter is the game object.
Source:

<private> _patreonCampaignEndpoint(req, res, url, ip)

Fetches Patreon campaign information.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _publicApiRequest(req, res, url, ip)

Handles requests to the public (unauthenticated) API endpoint.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _shardStatusHistoryEndpoint(req, res, url, ip)

Fetches shard status history information.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _twitchConfirmation(req, res, url, ip)

Handle a request from Twitch to confirm adding a webhook to this endpoint.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _twitchFinal(ids, content)

All data has been received for a Twitch webhook request, and is ready to be sent to our shards for re-broadcast and alerting.
Parameters:
Name Type Description
ids string Encoded array of ids to broadcast to shards.
content string Encoded content string to broadcast to shards.
Source:

<private> _twitchStreamEnd(userId)

Handle a user's stream ending.
Parameters:
Name Type Description
userId string The user's ID of which the stream has ended.
Source:

<private> _twitchWebhook(req, res, url, ip)

Handle a webhook event from Twitch.
Parameters:
Name Type Description
req http.IncomingMessage Client request.
res http.ServerResponse Server response.
url string The requested url. Generally a similar or slightly modified version of `req.url`.
ip string IP for logging purposes.
Source:

<private> _updateRateLimits()

Parse rate limits from file.
Source:

begin(Discord, client, command, common, bot)

Initialize this submodule.
Parameters:
Name Type Description
Discord Discord The Discord object for the API library.
client Discord~Client The client that represents this bot.
command Command The command instance in which to register command listeners.
common Common Class storing common functions.
bot SpikeyBot The parent SpikeyBot instance.
Inherited From:
Overrides:
Source:

<protected> debug(msg)

Log using common.logDebug, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Overrides:
Source:

end()

Trigger subModule to shutdown and get ready for process terminating.
Inherited From:
Overrides:
Source:

<protected> error(msg)

Log using common.error, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Overrides:
Source:

fetchGameData(gameId, cb)

Fetch a Twitch game information from its ID.
Parameters:
Name Type Description
gameId string The ID of the game to fetch.
cb function First parameter is an optional error string, otherwise the second parameter is the fetched data object.
Source:

<protected> initialize()

The function called at the end of begin() for further initialization specific to the subModule. Must be defined before begin() is called.
Inherited From:
Overrides:
Source:

<protected> log(msg)

Log using common.log, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Overrides:
Source:

<abstract> reloadable()

Check if this module is in a state that is ready to be reloaded. If false is returned, this module should not be unloaded and doing such may risk putting the module into an uncontrollable state. This is different from unloadable, which checks if this module can be stopped completely, this checks if the module can be stopped and restarted.
Inherited From:
Overrides:
Source:
See:
  • SubModule~unloadable
Returns:
True if can be reloaded, false if cannot.
Type
boolean

<abstract> save( [opt])

Saves all data to files necessary for saving current state.
Parameters:
Name Type Argument Default Description
opt string <optional>
'sync' Can be 'async', otherwise defaults to synchronous.
Inherited From:
Overrides:
Source:

<protected> shutdown()

Shutdown and disable this submodule. Removes all event listeners.
Inherited From:
Overrides:
Source:

<abstract> unloadable()

Check if this module is in a state that is ready to be unloaded. If false is returned, this module should not be unloaded and doing such may risk putting the module into an uncontrollable state.
Inherited From:
Overrides:
Source:
See:
  • SubModule~reloadable
Returns:
True if can be unloaded, false if cannot.
Type
boolean

<protected> warn(msg)

Log using common.logWarning, but automatically set name.
Parameters:
Name Type Description
msg string The message to log.
Inherited From:
Overrides:
Source: