Create Discord Music Bot

Can play and queue, can play music in multiple servers at once
This commit is contained in:
Myx
2023-06-21 04:24:58 +02:00
commit d9408907dd
13 changed files with 3177 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
const playdl = require('play-dl');
async function getStream(query) {
let songInformation = await playdl.soundcloud(query) // Make sure that url is track url only. For playlist, make some logic.
let stream = await playdl.stream_from_info(songInformation, { quality: 2 });
return {
title: songInformation.name,
stream: stream.stream,
duration: songInformation.durationInSec
}
}
module.exports.getStream = getStream;

15
music_sources/spotify.js Normal file
View File

@@ -0,0 +1,15 @@
const playdl = require('play-dl');
async function getStream(query) {
const trackInfo = await playdl.spotify(query);
let searched = await play.search(`${trackInfo.name}`, {
limit: 1
}) // This will search the found track on youtube.
let stream = await play.stream(searched[0].url) // This will create stream from the above search
return stream;
}
module.exports.getStream = getStream;

40
music_sources/youtube.js Normal file
View File

@@ -0,0 +1,40 @@
const ytsr = require('ytsr');
const playdl = require('play-dl');
async function getStream(query) {
try {
const regex = /(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))([^?&\n]+)/;
const match = query.match(regex);
let videoId;
if(match == null) {
const searchResults = await ytsr(query, { page: 1, type: 'video' });
videoId = searchResults.items[0].id;
// videoId = null;
console.log(searchResults.items[0].id)
if (videoId == null) {
let result = await playdl.search(query, { limit: 1})
let videoUrl = result[0].url;
videoId = videoUrl.match(regex)[1];
}
} else {
videoId = match[1];
}
const streamResult = await playdl.stream(`https://www.youtube.com/watch?v=${videoId}`, { quality: 2 });
const infoResult = await ytsr(`https://www.youtube.com/watch?v=${videoId}`, { limit: 1});
return {
title: infoResult.items[0].title ?? 'Unknown',
duration: infoResult.items[0].duration ?? 0,
stream: streamResult.stream,
type: streamResult.type
};
} catch (error) {
return null;
}
}
module.exports.getStream = getStream;