1+ var fs = require ( 'fs' ) ;
2+ var Discord = require ( 'discord.io' ) ;
3+ var PlexAPI = require ( 'plex-api' ) ;
4+ var logger = require ( 'winston' ) ;
5+ var config = require ( './config.js' ) ;
6+
7+ // Configure logger settings
8+ /*
9+ logger.remove(logger.transports.Console);
10+ logger.add(new logger.transports.Console, {
11+ colorize: true
12+ });
13+ logger.level = 'debug';
14+ */
15+
16+ // Initialize Discord Bot
17+ var bot = new Discord . Client ( {
18+ token : config . botToken ,
19+ autorun : true
20+ } ) ;
21+
22+ // Initialize Plex
23+ var plex = new PlexAPI ( {
24+ hostname : config . hostname ,
25+ port : config . port ,
26+ username : config . plexUsername ,
27+ password : config . plexPassword ,
28+ token : config . plexToken
29+ } ) ;
30+
31+ // Check Plex Server Information
32+ plex . query ( "/" ) . then ( function ( result ) {
33+ console . log ( "Sever: %s" + "\n" + "Plex Version: %s" ,
34+ result . MediaContainer . friendlyName ,
35+ result . MediaContainer . version ) ;
36+ } , function ( err ) {
37+ console . error ( "The Plex server appears to be down, go yell at Josh" , err ) ;
38+ } ) ;
39+ //console.log(plex);
40+
41+ //Whenever a library is added, the number increments by 1, on my server:
42+ //Music is 1
43+ //Movies is 3
44+ //TV Shows is 4
45+
46+ //C:\Program Files (x86)\Plex\Plex Media Server>"Plex Media Scanner.exe" --list
47+ // 3: Movies
48+ // 1: Music
49+ // 4: TV Shows
50+
51+ // Ready the bot
52+ bot . on ( 'ready' , function ( evt ) {
53+ console . log ( 'Bot has connected' ) ;
54+ console . log ( 'Name: ' + bot . username + "\n" + 'ID: ' + bot . id ) ;
55+ } ) ;
56+
57+ // Message handling
58+ bot . on ( 'message' , function ( user , userID , channelID , message , evt ) {
59+ // Our bot needs to know if it will execute a command
60+ // It will listen for messages that will start with `!`
61+ if ( message . substring ( 0 , 1 ) == '!' ) {
62+ var args = message . substring ( 1 ) . split ( ' ' ) ;
63+ var cmd = args [ 0 ] ;
64+ args = args . splice ( 1 ) ;
65+
66+ switch ( cmd ) {
67+ // Report valid commands
68+ case 'help' :
69+ bot . sendMessage ( {
70+ to : channelID ,
71+ message : 'Valid commands: !help, !serverstatus, !search, !playlist, !schwifty'
72+ } ) ;
73+ logger . info ( 'Command executed: help' ) ;
74+ break ;
75+ // Check the Plex server status and report back if it's running
76+ case 'serverstatus' :
77+ plex . query ( "/" ) . then ( function ( result ) {
78+ bot . sendMessage ( {
79+ to : channelID ,
80+ message : 'The Plex server appears to be up'
81+ } ) ;
82+ } , function ( err ) {
83+ bot . sendMessage ( {
84+ to : channelID ,
85+ message : 'The Plex server appears to be down, go yell at Josh' ,
86+ color : black
87+ } ) ;
88+ console . error ( "The Plex server appears to be down, go yell at Josh" , err ) ;
89+ } ) ;
90+ console . log ( 'Command executed: serverstatus' ) ;
91+ break ;
92+ // Check if a given piece of media (film, television show, anime, or music) exists on the Plex server
93+ case 'search' :
94+ var query = "" ;
95+
96+ if ( args . length == 0 ) {
97+ bot . sendMessage ( {
98+ to : channelID ,
99+ message : "No search term specified, please use: !search <query> to search for albums, movies, and shows on the Plex server"
100+ } ) ;
101+ break ;
102+ } else {
103+ for ( var i = 0 ; i < args . length ; i ++ ) {
104+ query += args [ i ] + " " ;
105+ }
106+ console . log ( "Query: " + query ) ;
107+ }
108+
109+ // List of query types: https://github.com/Arcanemagus/plex-api/wiki/MediaTypes
110+ // 1 - movie, 2 - show, 3 - season, 4 - episode, 5 - trailer, 6 - comic, 7 - person, 8 - artist,
111+ // 9 - album, 10 - track, 11 - photo album, 12 - picture, 13 - photo, 14 - clip, 15 - playlist
112+
113+ // TODO: Restructure with callbacks
114+ // Search movies
115+ plex . query ( '/search/?type=1&query=' + query ) . then ( function ( res ) {
116+ var results = res . MediaContainer . Metadata ;
117+ var resultSize = res . MediaContainer . size ;
118+ var searchOutput = "" ;
119+
120+ if ( resultSize >= 1 ) {
121+ searchOutput += "Films:" + "\n"
122+ for ( var i = 0 ; i < resultSize ; i ++ ) {
123+ //console.log(results[i]);
124+ searchOutput += results [ i ] . title + "\n" ;
125+ }
126+ }
127+ bot . sendMessage ( {
128+ to : channelID ,
129+ message : searchOutput
130+ } ) ;
131+ } , function ( err ) {
132+ bot . sendMessage ( {
133+ to : channelID ,
134+ message : "An error has occurred, go yell at Derek"
135+ } ) ;
136+ console . log ( 'An error occurred in search' ) ;
137+ } ) ;
138+
139+ // Search shows
140+ plex . query ( '/search/?type=2&query=' + query ) . then ( function ( res ) {
141+ var results = res . MediaContainer . Metadata ;
142+ var resultSize = res . MediaContainer . size ;
143+ var searchOutput = "" ;
144+
145+ if ( resultSize >= 1 ) {
146+ searchOutput += "Shows:" + "\n"
147+ for ( var i = 0 ; i < resultSize ; i ++ ) {
148+ //console.log(results[i]);
149+ searchOutput += results [ i ] . title + "\n" ;
150+ }
151+ }
152+ bot . sendMessage ( {
153+ to : channelID ,
154+ message : searchOutput
155+ } ) ;
156+ } , function ( err ) {
157+ bot . sendMessage ( {
158+ to : channelID ,
159+ message : "An error has occurred, go yell at Derek"
160+ } ) ;
161+ console . log ( 'An error occurred in search' ) ;
162+ } ) ;
163+
164+ // Search albums
165+ plex . query ( '/search/?type=9&query=' + query ) . then ( function ( res ) {
166+ var results = res . MediaContainer . Metadata ;
167+ var resultSize = res . MediaContainer . size ;
168+ var searchOutput = "" ;
169+
170+ if ( resultSize >= 1 ) {
171+ searchOutput += "Albums:" + "\n"
172+ for ( var i = 0 ; i < resultSize ; i ++ ) {
173+ //console.log(results[i]);
174+ searchOutput += results [ i ] . title + "\n" ;
175+ }
176+ }
177+ bot . sendMessage ( {
178+ to : channelID ,
179+ message : searchOutput
180+ } ) ;
181+ } , function ( err ) {
182+ bot . sendMessage ( {
183+ to : channelID ,
184+ message : "An error has occurred, go yell at Derek"
185+ } ) ;
186+ console . log ( 'An error occurred in search' ) ;
187+ } ) ;
188+ console . log ( 'Command executed: search' ) ;
189+ break ;
190+ // Pull a Youtube link from a seed file (random video or index)
191+ case 'playlist' :
192+ var youtubeLink = "" ;
193+ if ( args == "" ) {
194+ fs . readFile ( "playlist.txt" , { "encoding" : "utf8" } , function ( err , data ) {
195+ if ( err ) {
196+ console . log ( 'An error occurred in playlist' ) ;
197+ } else {
198+ var lines = data . toString ( ) . split ( '\n' ) ;
199+ youtubeLink = lines [ Math . floor ( Math . random ( ) * lines . length ) ] ;
200+ bot . sendMessage ( {
201+ to : channelID ,
202+ message : youtubeLink
203+ } ) ;
204+ console . log ( 'Command executed: playlist' ) ;
205+ }
206+ } ) ;
207+ } else {
208+ if ( args . length > 1 ) {
209+ bot . sendMessage ( {
210+ to : channelID ,
211+ message : "Please specify a single index idiot"
212+ } ) ;
213+ console . log ( 'Command executed: playlist with index' ) ;
214+ break ;
215+ }
216+
217+ var argInt = parseInt ( args [ 0 ] , 10 ) ;
218+ if ( typeof argInt != 'number' || isNaN ( argInt ) ) {
219+ bot . sendMessage ( {
220+ to : channelID ,
221+ message : "Please specify a number idiot"
222+ } ) ;
223+ console . log ( 'Command executed: playlist with index' ) ;
224+ break ;
225+ }
226+
227+ fs . readFile ( "playlist.txt" , { "encoding" : "utf8" } , function ( err , data ) {
228+ if ( err ) {
229+ console . log ( err ) ;
230+ } else {
231+ var lines = data . toString ( ) . split ( '\n' ) ;
232+ if ( args >= lines . length ) {
233+ bot . sendMessage ( {
234+ to : channelID ,
235+ message : "The index specified is larger than the number of items in the playlist idiot"
236+ } ) ;
237+ } else {
238+ youtubeLink = lines [ args ] ;
239+ bot . sendMessage ( {
240+ to : channelID ,
241+ message : youtubeLink
242+ } ) ;
243+ }
244+ console . log ( 'Command executed: playlist with index' ) ;
245+ }
246+ } ) ;
247+ }
248+ break ;
249+ // Return Get Schwifty (Andromulus Remix)
250+ case 'schwifty' :
251+ bot . sendMessage ( {
252+ to : channelID ,
253+ message : 'https://www.youtube.com/watch?v=m3RUYMGD9-o'
254+ } ) ;
255+ logger . info ( 'Command executed: schwifty' ) ;
256+ break ;
257+ //return movie titles
258+ case 'movietitles' :
259+ plex . query ( "/library/sections/3/all" ) . then ( function ( result ) {
260+ //console.log( result.MediaContainer );
261+ const {
262+ Metadata
263+ } = result . MediaContainer ;
264+
265+ // Metadata.map(item => {
266+ // console.log(item)
267+ // console.log('---------------------------------')
268+ // });
269+
270+ console . log ( Metadata ) ;
271+ //console.log(Metadata[0].title);
272+ //console.log(Metadata[0].Media[0]);
273+ bot . sendMessage ( {
274+ to : channelID ,
275+ message : Metadata [ 0 ] . title
276+ } ) ;
277+
278+ } , function ( err ) {
279+ console . error ( "An error has occurred, go yell at Derek" , err ) ;
280+ } ) ;
281+ console . log ( 'Command executed: movietitles' ) ;
282+ break ;
283+ // Check the release date (theatrical or DVD release) of a given piece of media
284+ case 'releasedate' :
285+ bot . sendMessage ( {
286+ to : channelID ,
287+ message : 'releasedate'
288+ } ) ;
289+ console . log ( 'Command executed: releasedate' ) ;
290+ break ;
291+ //https://gist.github.com/Godimas/ae8e7c7cbd6236622c777d6bcb7a6748
292+ // Youtube video webhooks - post Dunkey, etc videos to channel in Discord
293+ // imgur playlist - latest imgur link and greatesthits from sol - https://api.imgur.com/
294+ // Minigame (lolis)
295+ // Build up a to-do list of things to watch or do - Set up Mongo
296+ // Onmbi plex asks command
297+ // Bot joins audio and plays Rick clips or YouTube clips
298+ default :
299+ bot . sendMessage ( {
300+ to : channelID ,
301+ message : 'Type !help for commands'
302+ } ) ;
303+ console . log ( 'No command executed' ) ;
304+ break ;
305+ }
306+ }
307+ } ) ;
0 commit comments