Music Streaming Player for Discord with Google Drive using Node.js

Gists

Overview

This is a sample script for the music streaming player for Discord with Google Drive using Node.js.

Description

This sample script can achieve to play the music when uses the voice channel on Discord. The music files are put in the specific folder of Google Drive. This script downloads all music files and plays the music files at the voice channel with the stream.

Usage

1. Register BOT to Discord

At first, please register your BOT to Discord. And please retrieve the token.

2. Sample script

When you use this, please set the variables of auth, token and folderId. And after you run the script, please input the text of play to the voice channel. By this, the files in the specific folder on Google Drive are loaded and are played at the voice channel on Discord.

function playFiles(obj) {
  if (obj.files.length == 0) {
    obj.message.channel.send("end");
    obj.channel.leave(); // <--- Important
    return;
  }
  obj.drive.files.get(
    {
      fileId: obj.files[0].id,
      alt: "media"
    },
    { responseType: "stream" },
    (err, { data }) => {
      if (err) throw new Error(err);
      console.log(obj.files[0]);
      obj.message.channel.send(`Playing ${obj.files[0].name}`);
      obj.connection
        .playStream(data)
        .on("end", () => {
          obj.files.shift();
          playFiles(obj);
        })
        .on("error", err => console.log(err));
    }
  );
}

function main(auth) {
  const token = "###"; // Please set your token for Discord.
  const folderId = "###"; // Please set the folder ID of Google Drive.

  client.login(token);
  client.on("message", message => {
    console.log("Passed0");
    const channel = message.member.voiceChannel;
    if (channel && message.content == "play") {
      console.log("start");
      message.channel.send("start");
      const drive = google.drive({ version: "v3", auth });
      drive.files.list(
        {
          q: `'${folderId}' in parents`,
          fields: "files(id,name)"
        },
        (err, { data }) => {
          if (err) throw new Error(err);
          channel
            .join()
            .then(connection => {
              let obj = {
                drive: drive,
                channel: channel,
                connection: connection,
                message: message,
                files: data.files
              };
              playFiles(obj);
            })
            .catch(err => console.log(err));
        }
      );
    }
  });
}

Note:

  • About the authorization for using Drive API, please check the Quickstart of Node.js at the official document. In this script, it supposes that the object of auth can be used for downloading the files from Google Drive.

  • In this case, channel.leave() is very important. Because I confirmed that when this is not used, there are the cases that at the next play, the sound cannot be listened from 2nd file. Please be careful this.

  • This script used discord.js. So please add the following script.

const Discord = require("discord.js");
const client = new Discord.Client();

References

 Share!