musicbot.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. const Discord = require ( 'discord.js' );
  2. const Client = new Discord.Client ( );
  3. const logger = require ( './logger.js' );
  4. const Config = require ( './config.json' );
  5. const fs = require ( 'fs' );
  6. let isVoiceChannel = false;
  7. let dispatcher = undefined;
  8. let voteSkips = 0;
  9. let voteSkipped = { };
  10. let globalVoiceChannel = null;
  11. let songs = [];
  12. function playMusic ( )
  13. {
  14. generateSongList ( );
  15. let song = getRandomSong ( );
  16. logger.log ( '[Sound/automusic] now playing: ' + song );
  17. let voiceChannel = Client.channels.get ( Config.channel_id );
  18. globalVoiceChannel = voiceChannel;
  19. if ( voiceChannel instanceof Discord.VoiceChannel )
  20. {
  21. voiceChannel.join ( ).then ( ( connection ) =>
  22. {
  23. connection.on ( 'disconnect', function ( )
  24. {
  25. logger.log ( '[Info/automusic] disconnected from channel, will reconnect soon' );
  26. setTimeout ( function ( )
  27. {
  28. logger.log ( '[Info/automusic] reconnecting' );
  29. playMusic ( );
  30. }, 3000 );
  31. } );
  32. globalConnection = connection;
  33. dispatcher = connection.play ( './playlist/' + song, { passes: 3 } );
  34. dispatcher.on ( 'error', function ( m ) { logger.error ( m ); } );
  35. dispatcher.on ( 'end', ( ) =>
  36. {
  37. setTimeout ( function ( )
  38. {
  39. let announcer = connection.play ( './announcer.mp3', { passes: 3 } );
  40. announcer.on ( 'error', function ( m ) { logger.error ( m ); } );
  41. logger.log ( '[Info/automusic] playing announcer' );
  42. announcer.on ( 'end', ( ) =>
  43. {
  44. voteSkips = 0;
  45. voteSkipped = { };
  46. logger.log ( '[Info/automusic] song concluded, playing another random song' );
  47. setTimeout ( function ( ) { playMusic ( ); }, 500 );
  48. } );
  49. }, 1000 );
  50. } );
  51. } )
  52. .catch ( ( error ) => {
  53. logger.log ( '[Error/automusic] cannot join voice channel' );
  54. logger.log ( error );
  55. } );
  56. }
  57. else
  58. {
  59. logger.log ( '[Info/automusic] WARNING! ' + Config.channel_id + ' is not a valid voice channel id!' );
  60. }
  61. }
  62. function getRandomSong ( )
  63. {
  64. let rng = Math.floor ( Math.random ( ) * songs.length );
  65. return songs [rng];
  66. }
  67. function generateSongList ( )
  68. {
  69. fs.readdirSync ( './playlist' ).forEach ( function ( file )
  70. {
  71. songs.push ( file );
  72. } );
  73. }
  74. Client.on ( 'ready', ( ) =>
  75. {
  76. // client is ready
  77. generateSongList ( );
  78. logger.log ( '[Info/automusic] logged in!' );
  79. Client.user.setActivity ( 'Minecraft' );
  80. playMusic ( );
  81. } );
  82. Client.on ( 'warning', function ( m ) { logger.warn ( m ); } )
  83. .on ( 'error', function ( m ) { logger.error ( m ); } )
  84. .on ( 'disconnect', ( ) => {
  85. logger.warn ( '[Info/automusic] disconnected!' );
  86. } );
  87. function formatResponse ( text )
  88. {
  89. if ( typeof text === 'string' )
  90. return text.replace( /`/g, "`" + String.fromCharCode ( 8203 ) ).replace ( /@/g, "@" + String.fromCharCode ( 8203 ) );
  91. else
  92. return text;
  93. }
  94. Client.on ( 'message', ( message ) =>
  95. {
  96. // maintenance
  97. if ( message.content.startsWith ( '$voteskip' ) )
  98. {
  99. let userid = message.author.id;
  100. let onlineusers = globalVoiceChannel.members.size;
  101. let required = Math.floor ( ( onlineusers - 1 ) / 2 );
  102. if ( !voteSkipped [userid] )
  103. {
  104. voteSkips++;
  105. message.channel.send ( "Zarejestrowano twój glos **" + voteSkips + "/" + ( required + 1 ) + "**" );
  106. voteSkipped [userid] = true;
  107. if ( voteSkips > Math.floor ( ( onlineusers - 1 ) / 2 ) )
  108. {
  109. message.channel.send ( "Głosem większości dostępnych piosenka została pominięta." );
  110. dispatcher.end ( );
  111. }
  112. }
  113. }
  114. if ( message.content.startsWith ( '$skip' ) && message.author.id === '276791868141076480' )
  115. {
  116. dispatcher.end ( );
  117. return;
  118. }
  119. if ( message.content.startsWith ( '$eval ' ) && message.author.id === '276791868141076480' )
  120. {
  121. try
  122. {
  123. let payload = message.content.substring ( 6, message.content.length );
  124. let output = true;
  125. let result = eval ( payload );
  126. if ( output )
  127. message.channel.send ( formatResponse ( result ), { code: 'xl' } );
  128. }
  129. catch ( error )
  130. {
  131. message.channel.send ( '```' + formatResponse ( error ) + '```' );
  132. }
  133. return;
  134. }
  135. } );
  136. module.exports.run = function ( )
  137. {
  138. Client.login ( Config.token );
  139. }