Skip to content

Latest commit

 

History

History
407 lines (282 loc) · 7.07 KB

README.md

File metadata and controls

407 lines (282 loc) · 7.07 KB

Hack Club Arcade JS

This is a library to access the Hack Club Arcade API in TypeScript/JavaScript.

To get started, simply install the package, make a js/ts file, and create a client:

const hackclub = require('hc-arcade-js');
// OR
import * as hackclub from 'hc-arcade-js';

const client = new hackclub.Client({
  slackID: 'Slack User ID',
  APIKey: 'Hack Club API Key',
});

And you're set!

Usage

Types/Classes

ResData

{
    ok: false;
    error: string;
} | {
    ok: true;
    // More Data
}

SessionData

{
  time: {
    totalMins: number;
    elapsedMins: number;
    remainingMins: number;
    createdAt: Date;
    endAt: Date;
  }
  paused: boolean;
  completed: boolean;
  goal: string | null;
  work: string;
}

Time

export default class Time {
  constructor(ms: number);
  getMS(): number;
  getSeconds(): number;
  getMinutes(): number;
  getHours(): number;
  getDays(): number;
  getWeeks(): number;
  getMonths(): number;
  getTrueMonths(): number;
  getYears(): number;
}

Methods

ping()

Returns a Promise which resolves to the string "pong".

Example
const res = await client.ping();
console.log(res);
// pong

getStatus()

Fetches the status of the hack club arcade.

Returns a Promise which resolves with the status.

Data
{
  activeSessions: number;
  airtableConnected: boolean;
  slackConnected: boolean;
}
Example
const status = await client.getStatus();
console.log(`There are ${status.activeSessions} sessions active!`);
// There are (x) sessions active!

getRemainingSessionTime(slackId?: string) (DEPRECATED)

Fetches the amount of milliseconds left on the active session of a user, and also indicates if there is a session active. By default fetches the active user's session.

Returns a Promise which resolves to a ResData.

The data will either have active as false, or active as true and remainingMS as the amount of milliseconds left.

Data
{
    active: false;
} | {
    active: true;
    remainingMS: number;
}
Example
const status = await client.getRemainingSessionTime();

if (!status.ok) throw new Error(status.error);

if (status.active) console.log(`I have ${status.remainingMS / 1000 / 60} minutes left!`);
else console.log('I am not in an active session!');

getSessionData()

Fetches active session data of the current user.

Returns a Promise which resolves to a ResData.

The data will either have found to false and active to false if there is no active or recent session, or found will be true and the data will fill in as below.

Data
{
    found: true;
    active: boolean;
    userID: string;
    messageTs: string;
} & SessionData
Example
const session = await client.getSessionData();

if (!session.ok) throw new Error(session.error);

if (session.active) console.log(`I have ${session.time.remainingMinsq} minutes left!`);
else console.log('I am not in an active session!');

getUserStats()

Fetches statistics of the current user.

Returns a Promise which resolves to a ResData.

Data
{
  sessions: number;
  time: Time;
}
Example
const stats = await client.getUserStats();

if (!stats.ok) throw new Error(stats.error);

console.log(`I have done ${stats.sessions} sessions and spent ${stats.time.getMinutes()} minutes on the Hack Club Arcade!`);

getUserGoals()

Fetches all goals and their data of the current user.

Returns a Promise which resolves to a ResData.

Data
{
  goals: Array<{
    name: string;
    time: Time;
  }>;
}
Example
const goals = await client.getUserGoals();

if (!goals.ok) throw new Error(goals.error);

console.log(`I have ${goals.length} total goals!`);

getSessionHistory()

Fetches the session history of the user.

Returns a Promise which resolves to a ResData.

| NOTE: This may not retrieve all sessions

Data
{
  sessions: Array<SessionData>;
}
Example
const history = await client.getSessionHistory();

if (!history.ok) throw new Error(history.error);

console.log(`I have ${history.length} total sessions!`);

start(work: string)

Start a new session.

Returns a Promise which resolves to a ResData.

The data will either have created set to false in the case that there is an already active session, or created will be set to true and the data will fill in as below.

Data
{
  created: true;
  session: {
    id: string;
    userID: string;
    createdAt: Date;
  }
}
Example
const res = await client.start();

if (!res.ok) throw new Error(res.error);

if (res.created) console.log(`Created session ${res.session.id}`);
else console.log('Session already active!');

cancel()

Stops the current session.

Returns a Promise which resolves to a ResData.

The data will either have cancelled set to false in the case that there is no active session, or cancelled will be set to true and the data will fill in as below.

Data
{
  cancelled: true;
  session: {
    id: string;
    userID: string;
    createdAt: Date;
  }
}
Example
const res = await client.cancel();

if (!res.ok) throw new Error(res.error);

if (res.cancelled) console.log(`Cancelled session ${res.session.id}`);
else console.log('No active session!');

togglePaused()

Returns a Promise which resolves to a ResData.

The data will either have toggled set to false in the case that there is no active session, or toggled will be set to true and the data will fill in as below.

Data
{
  toggled: true;
  session: {
    id: string;
    userID: string;
    createdAt: Date;
    paused: boolean;
  }
}
Example
const res = await client.togglePaused();

if (!res.ok) throw new Error(res.error);

if (res.toggled) console.log(`${res.session.paused ? 'Paused' : 'Unpaused'} the active session!`);
else console.log('No active session!');

Events

ratelimit

ratelimit(type: 'READ' | 'WRITE', info: RateLimitData): void;

Emitted whenever new ratelimit data is received, irrespective of if the ratelimit was hit or not.