@@ -16,6 +16,24 @@ const {localize} = require('../../../src/functions/localize');
16
16
function twitchNotifications ( client , apiClient ) {
17
17
const streamers = client . configurations [ 'twitch-notifications' ] [ 'streamers' ] ;
18
18
19
+ /**
20
+ * Function to add the Live-Role
21
+ * @param {string } userID ID of the User
22
+ * @param {String } roleID ID of the Role
23
+ * @param {boolean } liveRole Should the live-role be active
24
+ */
25
+ async function addLiveRole ( userID , roleID , liveRole ) {
26
+ if ( ! liveRole ) return ;
27
+ if ( ! userID || userID === '' || ! roleID || roleID === '' ) return ;
28
+ await client . guild . members . fetch ( ) ;
29
+ const member = client . guild . members . cache . get ( userID ) ;
30
+ if ( ! member ) {
31
+ client . logger . error ( localize ( 'twitch-notifications' , 'user-not-found' , { u : userID } ) ) ;
32
+ return ;
33
+ }
34
+ await member . roles . add ( roleID ) ;
35
+ }
36
+
19
37
/**
20
38
* Sends the live-message
21
39
* @param {string } username Username of the streamer
@@ -75,10 +93,24 @@ function twitchNotifications(client, apiClient) {
75
93
startedAt : stream . startDate . toString ( )
76
94
} ) ;
77
95
sendMsg ( stream . userDisplayName , stream . gameName , stream . thumbnailUrl , streamers [ index ] [ 'liveMessageChannel' ] , stream . title , index ) ;
96
+ addLiveRole ( streamers [ index ] [ 'id' ] , streamers [ index ] [ 'role' ] , streamers [ index ] [ 'liveRole' ] ) ;
78
97
} else if ( stream !== null && stream . startDate . toString ( ) !== streamer . startedAt ) {
79
98
streamer . startedAt = stream . startDate . toString ( ) ;
80
99
streamer . save ( ) ;
81
100
sendMsg ( stream . userDisplayName , stream . gameName , stream . thumbnailUrl , streamers [ index ] [ 'liveMessageChannel' ] , stream . title , index ) ;
101
+ addLiveRole ( streamers [ index ] [ 'id' ] , streamers [ index ] [ 'role' ] , streamers [ index ] [ 'liveRole' ] ) ;
102
+ } else if ( stream === null ) {
103
+ if ( ! streamers [ index ] [ 'liveRole' ] ) return ;
104
+ if ( ! streamers [ index ] [ 'id' ] || streamers [ index ] [ 'id' ] === '' || ! streamers [ index ] [ 'role' ] || streamers [ index ] [ 'role' ] === '' ) return ;
105
+ await client . guild . members . fetch ( ) ;
106
+ const member = client . guild . members . cache . get ( streamers [ index ] [ 'id' ] ) ;
107
+ if ( ! member ) {
108
+ client . logger . error ( localize ( 'twitch-notifications' , 'user-not-found' , { u : streamers [ index ] [ 'id' ] } ) ) ;
109
+ return ;
110
+ }
111
+ if ( member . roles . cache . has ( streamers [ index ] [ 'role' ] ) ) {
112
+ await member . roles . remove ( streamers [ index ] [ 'role' ] ) ;
113
+ }
82
114
}
83
115
}
84
116
}
0 commit comments