Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. const Discord = require("discord.js")
  2. const fs = require('fs');
  3.  
  4. module.exports.run = async (bot, message, args) => {
  5. // make a new stream for each time someone starts to talk
  6. function generateOutputFile(channel, member) {
  7. // use IDs instead of username cause some people have stupid emojis in their name
  8. const fileName = `./recordings/${channel.id}-${member.id}-${Date.now()}.pcm`;
  9. return fs.createWriteStream(fileName);
  10. }
  11.  
  12. if (msg.content.startsWith(config.prefix+'join')) {
  13. let [command, ...channelName] = msg.content.split(" ");
  14. if (!msg.guild) {;
  15. return msg.reply('no private service is available in your area at the moment. Please contact a service representative for more details.');
  16. }
  17. const voiceChannel = msg.guild.channels.find("name", channelName.join(" "));
  18. //console.log(voiceChannel.id);
  19. if (!voiceChannel || voiceChannel.type !== 'voice') {
  20. return msg.reply(`I couldn't find the channel ${channelName}. Can you spell?`);
  21. }
  22. voiceChannel.join()
  23. .then(conn => {
  24. msg.reply('ready!');
  25. // create our voice receiver
  26. const receiver = conn.createReceiver();
  27.  
  28. conn.on('speaking', (user, speaking) => {
  29. if (speaking) {
  30. msg.channel.sendMessage(`I'm listening to ${user}`);
  31. // this creates a 16-bit signed PCM, stereo 48KHz PCM stream.
  32. const audioStream = receiver.createPCMStream(user);
  33. // create an output stream so we can dump our data in a file
  34. const outputStream = generateOutputFile(voiceChannel, user);
  35. // pipe our audio data into the file stream
  36. audioStream.pipe(outputStream);
  37. outputStream.on("data", console.log);
  38. // when the stream ends (the user stopped talking) tell the user
  39. audioStream.on('end', () => {
  40. msg.channel.sendMessage(`I'm no longer listening to ${user}`);
  41. });
  42. }
  43. });
  44. })
  45. .catch(console.log);
  46. }
  47. if(msg.content.startsWith(config.prefix+'leave')) {
  48. let [command, ...channelName] = msg.content.split(" ");
  49. let voiceChannel = msg.guild.channels.find("name", channelName.join(" "));
  50. voiceChannel.leave();
  51. }
  52. }
  53.  
  54.  
  55. module.exports.help = {
  56. name: "join"
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement