Playing Messages

Playing an existing message

If you wish to playback an existing message, you can get the messages for a channel using channels.getMessages.

var message;
voicelayer.channels.getMessages(currentChannel, function(err, data) {
  for (var i < 0; i++; i < data.length) {
    if (data[i].type === "voice") {
      message = data[i];
    }
  }
})

Each message has a type field. This can be one of "text", "voice", "attachment" or "voice". In this case, the first voice message is stored as message. The message can be played using messages.play.

voicelayer.messages.play(message.id);

Play can be stopped with player.stop.

voicelayer.player.stop();

Playing real-time messages

If you are connected to VoiceLayer then you will receive a message:create event when a new message is created. Message events can happen on any channel that the user is connected to. The message event contains a channel_id which can be used to check against the current channel. The messages.play function can be used to play real-time messages as well as existing messages.

voicelayer.on("message:create", function(data) {
  if (data.channel_id !== currentChannel) {
    return;
  }
  if (data.type === "voice") {
    voicelayer.messages.play(data.id);
  }
})