This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

character / src / obs-control.ts
2.0 kB 75 lines
1import OBSWebSocket from 'obs-websocket-js'; 2 3export type OBSControlOptions = { 4 url?: string; 5 password?: string; 6 sceneName?: string; 7 textInputName?: string; 8}; 9 10export class OBSControlClient { 11 private readonly obs = new OBSWebSocket(); 12 private readonly url: string; 13 private readonly password?: string; 14 private readonly sceneName?: string; 15 private readonly textInputName?: string; 16 private connected = false; 17 18 constructor(options: OBSControlOptions = {}) { 19 this.url = options.url ?? process.env.OBS_WS_URL ?? 'ws://127.0.0.1:4455'; 20 this.password = options.password ?? process.env.OBS_WS_PASSWORD; 21 this.sceneName = options.sceneName ?? process.env.OBS_SCENE_NAME; 22 this.textInputName = options.textInputName ?? process.env.OBS_TEXT_INPUT_NAME; 23 } 24 25 async connect() { 26 if (this.connected) return; 27 await this.obs.connect(this.url, this.password, { rpcVersion: 1 }); 28 this.connected = true; 29 } 30 31 async setProgramScene(sceneName = this.sceneName) { 32 if (!sceneName) return; 33 await this.connect(); 34 await this.obs.call('SetCurrentProgramScene', { sceneName }); 35 } 36 37 async ensureTextInput(inputName = this.textInputName, sceneName = this.sceneName) { 38 if (!inputName || !sceneName) return; 39 await this.connect(); 40 41 try { 42 await this.obs.call('GetInputSettings', { inputName }); 43 return; 44 } catch { 45 await this.obs.call('CreateInput', { 46 sceneName, 47 inputName, 48 inputKind: 'text_ft2_source', 49 inputSettings: { 50 text: '', 51 }, 52 sceneItemEnabled: true, 53 }); 54 } 55 } 56 57 async setText(text: string, inputName = this.textInputName) { 58 if (!inputName) return; 59 await this.connect(); 60 await this.ensureTextInput(inputName, this.sceneName); 61 await this.obs.call('SetInputSettings', { 62 inputName, 63 inputSettings: { 64 text, 65 }, 66 overlay: true, 67 }); 68 } 69 70 async disconnect() { 71 if (!this.connected) return; 72 await this.obs.disconnect(); 73 this.connected = false; 74 } 75}