@@ -2,13 +2,41 @@ import { RefreshingAuthProvider } from '@twurple/auth';
2
2
import { ChatClient } from '@twurple/chat' ;
3
3
import { useEnvironment } from '../../utils/env/env.js' ;
4
4
import { createSnapshotRequest } from '../snapshot/index.js' ;
5
- import { restoreToken , saveToken } from './auth/auth.js' ;
5
+ import { restoreToken , saveToken } from './auth/token.js' ;
6
+ interface Status {
7
+ status : 'pending' | 'offline:not_authenticated' | 'offline' | 'online' ;
8
+ channels : string [ ] ;
9
+ commands : string [ ] ;
10
+ }
11
+
12
+ const handler : Record < string , ( chat : ChatClient , channel : string , user : string , message : string ) => Promise < void > > = {
13
+ capture : async ( chat : ChatClient , channel : string , user : string , message : string ) => {
14
+ const env = useEnvironment ( ) ;
15
+ const [ _ , duration , rewind ] = message . split ( ' ' ) ;
16
+ if ( isNaN ( Number ( duration ) ) || isNaN ( Number ( rewind ) ) ) {
17
+ chat . say ( channel , `@${ user } Invalid duration or rewind. Please use !capture <duration> <rewind>` ) ;
18
+ return ;
19
+ }
20
+ const request = await createSnapshotRequest ( 'twitch' , user , Number ( duration ) , Number ( rewind ) ) ;
21
+ chat . say ( channel , `@${ user } Critter captured! ${ env . variables . UI_URL } /snapshots/${ request . id } ` ) ;
22
+ }
23
+ } ;
24
+
25
+ export const status : Status = {
26
+ status : 'pending' ,
27
+ channels : [ 'strangecyan' , 'alveusgg' ] ,
28
+ commands : Object . keys ( handler )
29
+ } ;
6
30
7
31
export const start = async ( ) => {
8
32
const env = useEnvironment ( ) ;
9
33
10
34
const token = await restoreToken ( ) ;
11
- if ( ! token ) throw new Error ( 'No token found, cannot start chat. Please sign in.' ) ;
35
+ if ( ! token ) {
36
+ status . status = 'offline:not_authenticated' ;
37
+ return ;
38
+ }
39
+
12
40
const auth = new RefreshingAuthProvider ( {
13
41
clientId : env . variables . TWITCH_CLIENT_ID ,
14
42
clientSecret : env . variables . TWITCH_CLIENT_SECRET
@@ -17,20 +45,20 @@ export const start = async () => {
17
45
auth . onRefresh ( ( _ , token ) => saveToken ( token ) ) ;
18
46
await auth . addUserForToken ( token , [ 'chat' ] ) ;
19
47
20
- const chat = new ChatClient ( { authProvider : auth , channels : [ 'strangecyan' , 'alveusgg' ] } ) ;
48
+ const chat = new ChatClient ( { authProvider : auth , channels : status . channels } ) ;
49
+ chat . onConnect ( ( ) => ( status . status = 'online' ) ) ;
50
+
51
+ chat . onDisconnect ( ( ) => ( status . status = 'offline' ) ) ;
52
+
21
53
chat . connect ( ) ;
22
54
23
55
chat . onMessage ( async ( channel , user , message ) => {
24
56
if ( ! message . startsWith ( '!' ) ) return ;
25
57
26
- if ( message . startsWith ( '!polcapture' ) ) {
27
- const [ _ , duration , rewind ] = message . split ( ' ' ) ;
28
- if ( isNaN ( Number ( duration ) ) || isNaN ( Number ( rewind ) ) ) {
29
- chat . say ( channel , `@${ user } Invalid duration or rewind. Please use !polcapture <duration> <rewind>` ) ;
30
- return ;
58
+ for ( const command of status . commands ) {
59
+ if ( message . startsWith ( `!${ command } ` ) ) {
60
+ await handler [ command ] ( chat , channel , user , message ) ;
31
61
}
32
- const request = await createSnapshotRequest ( 'twitch' , user , Number ( duration ) , Number ( rewind ) ) ;
33
- chat . say ( channel , `@${ user } Critter captured! ${ env . variables . UI_URL } /snapshots/${ request . id } ` ) ;
34
62
}
35
63
} ) ;
36
64
} ;
0 commit comments