Advertisement
baasbase

OBSModule typescript

Feb 24th, 2024
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import ObsWebSocket from 'obs-websocket-js';
  2. import {BotModule} from '../BotModule';
  3. import {ChatUserstate} from 'tmi.js';
  4. import {SocketMessagePayload} from '../../../shared/socket-message-payload.interface';
  5. import {SocketMessageType} from '../../../shared/socket-message-type.interface';
  6. import {SocketMessage} from '../../../shared/socket-message.interface';
  7. import {twitchModule} from './TwitchModule';
  8.  
  9. class OBSModule extends BotModule
  10. {
  11.     moduleName = 'OBSModule';
  12.  
  13.     private client: ObsWebSocket;
  14.     private obsAddress: string;
  15.     private obsPort: number | string;
  16.     private obsPassword: string;
  17.  
  18.     constructor()
  19.     {
  20.         super();
  21.         this.obsAddress = process.env.OBS_ADDRESS || 'localhost';
  22.         this.obsPort = process.env.OBS_PORT || 4444;
  23.         this.obsPassword = process.env.OBS_PASSWORD;
  24.  
  25.         this.client = new ObsWebSocket();
  26.         this.client.on('Exiting', () => {
  27.             console.log('OBS Exited');
  28.             this.client.disconnect();
  29.             setTimeout(() => this.connect(), 10000);
  30.         });
  31.         this.client.on('SwitchScenes', scene => {
  32.             this.onSceneSwitch(scene);
  33.         });
  34.         this.connect();
  35.     }
  36.  
  37.     private connect(): void
  38.     {
  39.         this.client.connect({address: this.obsAddress + ':' + this.obsPort, password: atob(this.obsPassword)})
  40.             .then(() => console.log('OBS API initialized'))
  41.             .catch(() => setTimeout(() => this.connect(), 30000));
  42.     }
  43.  
  44.     init(): void
  45.     {
  46.         twitchModule.subscribeTwitchEvent('channel.channel_points_custom_reward_redemption.add', event => {
  47.             const username = event.user_name;
  48.             const rewardId = event.reward.id;
  49.             console.log(`${username} redeemed ${rewardId} (${event.reward.title}): ${event.user_input}`);
  50.             if (rewardId === '1f8cea96-ec9a-4518-91a6-4c0029d1a90c')
  51.             {
  52.                 this.toggleSnapCam(true);
  53.                 setTimeout(() => this.toggleSnapCam(false), 60000);
  54.             }
  55.         });
  56.     }
  57.  
  58.     onCommand(channel: string, userState: ChatUserstate, command: string, args: string[]): void
  59.     {
  60.     }
  61.  
  62.     onSocketMessage(data: SocketMessage<SocketMessageType, SocketMessagePayload>): void
  63.     {
  64.     }
  65.  
  66.     onTMIMessage(channel: string, userState: ChatUserstate, message: string): void
  67.     {
  68.     }
  69.    
  70.     private toggleSnapCam(state: boolean): void
  71.     {
  72.         this.client.send('SetSceneItemProperties', {
  73.             item: {
  74.                 name: 'SnapCam',
  75.             },
  76.             visible: state,
  77.             position: {},
  78.             bounds: {},
  79.             scale: {},
  80.             crop: {}
  81.         });
  82.     }
  83.  
  84.     private onSceneSwitch(scene: any)
  85.     {
  86.         // if (scene['scene-name'] === 'Just chatting')
  87.         // {
  88.         //     twitchModule.updateCustomReward({is_enabled: true});
  89.         // }
  90.         // else
  91.         // {
  92.         //     twitchModule.updateCustomReward({is_enabled: false});
  93.         // }
  94.     }
  95. }
  96.  
  97. export const obsModule = new OBSModule();
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement