@@ -6,54 +6,44 @@ const CommandOutput = require('../command-output');
6
6
function live ( input , services ) {
7
7
return services . dggApi . getStreamInfo ( ) . then ( ( data ) => {
8
8
const now = moment ( ) ;
9
- let totalViewers = 0 ;
10
- let twitchViewers = 0 ;
11
- let youtubeViewers = 0 ;
12
- let startedAt = 0 ;
13
-
14
- const twitch = data . twitch ;
15
- const youtube = data . youtube ;
16
-
17
- if ( ! twitch && ! youtube ) return new CommandOutput ( null , 'Could not retrieve live status.' ) ;
18
-
19
- if ( ( ! twitch || ! twitch . live ) && ( ! youtube || ! youtube . live ) ) {
20
- const endedAt = youtube . ended_at ? youtube . ended_at : twitch . ended_at ;
21
- const duration = youtube . duration ? youtube . duration : twitch . duration ;
22
- if ( endedAt ) {
23
- const lastOnlineDuration = formatDuration ( moment . duration ( now . diff ( endedAt ) ) ) ;
24
- const timeStreamedDuration = formatDuration ( moment . duration ( duration , 'seconds' ) ) ;
9
+
10
+ const isLive = ( ) => {
11
+ return data . some ( ( s ) => s ?. live ) ;
12
+ } ;
13
+
14
+ const oldestStartedLiveStream = ( ) => {
15
+ return data . filter ( ( s ) => s ?. live ) . sort ( ( a , b ) => ( a . started_at < b . started_at ? - 1 : 1 ) ) [ 0 ] ;
16
+ } ;
17
+
18
+ const newestEndedStream = ( ) => {
19
+ return data
20
+ . filter ( ( s ) => s ?. live === false )
21
+ . sort ( ( a , b ) => ( a ?. ended_at > b ?. ended_at ? - 1 : 1 ) ) [ 0 ] ;
22
+ } ;
23
+
24
+ const totalViewers = ( ) => {
25
+ return data . filter ( ( s ) => s ?. live ) . reduce ( ( acc , s ) => acc + s . viewers , 0 ) ;
26
+ } ;
27
+
28
+ if ( ! isLive ( ) ) {
29
+ const { duration, ended_at } = newestEndedStream ( ) ?? { } ;
30
+ if ( duration && ended_at ) {
31
+ const endedAgo = formatDuration ( moment . duration ( now . diff ( ended_at ) ) ) ;
32
+ const friendlyDuration = formatDuration ( moment . duration ( duration , 'seconds' ) ) ;
25
33
return new CommandOutput (
26
34
null ,
27
- `Stream was last online ${ lastOnlineDuration } ago. Time Streamed: ${ timeStreamedDuration } ` ,
35
+ `Stream was last online ${ endedAgo } ago. Time Streamed: ${ friendlyDuration } . ` ,
28
36
) ;
29
37
} else {
30
38
return new CommandOutput ( null , 'Stream is offline.' ) ;
31
39
}
32
40
}
33
41
34
- if ( twitch && twitch . live ) {
35
- twitchViewers = parseInt ( twitch . viewers , 10 ) ;
36
- totalViewers += twitchViewers ;
37
- startedAt = twitch . started_at ;
38
- }
39
-
40
- if ( youtube && youtube . live ) {
41
- youtubeViewers = parseInt ( youtube . viewers , 10 ) ;
42
- totalViewers += youtubeViewers ;
43
- startedAt = youtube . started_at ;
44
- }
45
-
46
- const formattedDuration = formatDuration ( moment . duration ( now . diff ( startedAt ) ) ) ;
47
- if ( twitchViewers && youtubeViewers ) {
48
- return new CommandOutput (
49
- null ,
50
- `Viewers: ${ totalViewers } , TTV: ${ twitchViewers } , YT: ${ youtubeViewers } . Stream live as of ${ formattedDuration } ago` ,
51
- ) ;
52
- }
53
-
42
+ const { viewers, started_at } = oldestStartedLiveStream ( ) ;
43
+ const startedAgo = formatDuration ( moment . duration ( now . diff ( started_at ) ) ) ;
54
44
return new CommandOutput (
55
45
null ,
56
- `Viewers: ${ totalViewers } . Stream live as of ${ formattedDuration } ago` ,
46
+ `Viewers: ${ totalViewers ( ) } . Stream live as of ${ startedAgo } ago. ` ,
57
47
) ;
58
48
} ) ;
59
49
}
0 commit comments