0.1.1-alpha.1 • Published 3 years ago

@dawalters1/wolf.js.stage v0.1.1-alpha.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Example I guess? - Highly recommend using redis with this setup, that way you can be more accurate with data being sent, of if you wish to use a queue you can handle it all from here

const StageClient = require('@dawalters1/wolf.js.stage');

module.exports = class ClientManager {
  constructor (api) {
    this._api = api;
    this.clients = [];
  }

  async getClient (targetGroupId) {
    const requestedClient = this.clients.find((cliData) => cliData.targetGroupId === targetGroupId);

    if (requestedClient) {
      return requestedClient.client;
    }

    const client = new StageClient(this._api, targetGroupId);

    //Requested audio has finished broadcasting
    client.onBroadcastEnded(async () => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        'Broadcast has ended'
      );
    });

    // Client was disconnected from stage
    client.onDisconnected(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        data.sourceSubscriberId ? `I was disconnected from the stage by ${(await this._api.subscriber().getById(data.sourceSubscriberId)).nickname} (${data.sourceSubscriberId})` : 'I have dropped from stage'
      );
    });

    //Bot was muted on stage
    client.onMuted(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        data.sourceSubscriberId ? `I was muted on stage by ${(await this._api.subscriber().getById(data.sourceSubscriberId)).nickname} (${data.sourceSubscriberId})` : 'I have muted myself'
      );
    });

    //Broadcast was muted (Contains current duration of broadcast)
    client.onPaused(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        '(Y) Song has been paused'
      );
    });

    //An error occurred while broadcasting
    client.onError(async (error) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        `(N) Error occurred while broadcasting - ${error.message} :(`
      );
    });

    //Client joined a slot and is ready to broadcast
    client.onReady(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        '(Y) Client is ready to broadcast'
      );
    });

    //Broadcast has been stopped and reset
    client.onStopped(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        '(Y) Broadcast has been paused'
      );
    });

    //Broadcast has been unmuted
    client.onUnmuted(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        data.sourceSubscriberId ? `I was unmuted on stage by ${(await this._api.subscriber().getById(data.sourceSubscriberId)).nickname} (${data.sourceSubscriberId})` : 'I have unmuted myself'
      );
    });

    //Broadcast was unpaused
    client.onUnpaused(async (data) => {
      return await this._api.messaging().sendGroupMessage(
        targetGroupId,
        '(Y) Broadcast has been unpaused'
      );
    });

    this.clients.push({
      targetGroupId,
      client
    });

    return client;
  }

  async deleteClient (targetGroupId) {
    const requestedClient = this.clients.find((cliData) => cliData.targetGroupId === targetGroupId);

    if (requestedClient) {
      const client = requestedClient.client;

      await client.stop();

      if (client.ready) {
        await client.leaveSlot();
      }

      this.clients = this.clients.filter((cliData) => cliData.targetGroupId !== targetGroupId);
    }

    return Promise.resolve();
  }
};