Create Discord Music Bot
Can play and queue, can play music in multiple servers at once
This commit is contained in:
42
utils/getMusicStream.js
Normal file
42
utils/getMusicStream.js
Normal file
@@ -0,0 +1,42 @@
|
||||
const spotify = require('../music_sources/spotify');
|
||||
const soundcloud = require('../music_sources/soundcloud');
|
||||
const youtube = require('../music_sources/youtube');
|
||||
|
||||
const { StreamType } = require('@discordjs/voice');
|
||||
|
||||
async function getMusicStream(query) {
|
||||
let stream;
|
||||
let songTitle;
|
||||
let songDuration;
|
||||
let type = StreamType.Opus;
|
||||
|
||||
if (query.includes('spotify.com')) {
|
||||
stream = await spotify.getStream(query);
|
||||
songTitle = stream.title;
|
||||
songDuration = stream.duration;
|
||||
stream = stream.stream;
|
||||
|
||||
} else if (query.includes('soundcloud.com')) {
|
||||
stream = await soundcloud.getStream(query);
|
||||
songTitle = stream.title;
|
||||
songDuration = stream.duration;
|
||||
stream = stream.stream;
|
||||
type = StreamType.OggOpus;
|
||||
|
||||
} else {
|
||||
stream = await youtube.getStream(query);
|
||||
songTitle = stream.title ?? 'Unknown';
|
||||
songDuration = stream.duration ?? 'Unknown';
|
||||
stream = stream.stream;
|
||||
type = StreamType.Opus;
|
||||
}
|
||||
|
||||
return {
|
||||
title: songTitle,
|
||||
duration: songDuration,
|
||||
stream: stream,
|
||||
type: type
|
||||
};
|
||||
}
|
||||
|
||||
module.exports.getMusicStream = getMusicStream;
|
||||
41
utils/registerCommands.js
Normal file
41
utils/registerCommands.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { SlashCommandBuilder } = require('@discordjs/builders');
|
||||
const { REST } = require('@discordjs/rest');
|
||||
const { Routes } = require('discord-api-types/v9');
|
||||
|
||||
async function registerCommands(clientId, token) {
|
||||
const commands = [
|
||||
new SlashCommandBuilder()
|
||||
.setName('play')
|
||||
.setDescription('Plays songs!')
|
||||
.addStringOption(option =>
|
||||
option.setName('input')
|
||||
.setDescription('Play song from YouTube, Spotify, SoundCloud, etc.')
|
||||
.setRequired(true)
|
||||
),
|
||||
new SlashCommandBuilder()
|
||||
.setName('queue')
|
||||
.setDescription('Adds a song to the queue!')
|
||||
.addStringOption(option =>
|
||||
option.setName('song')
|
||||
.setDescription('Add song from YouTube, Spotify, SoundCloud, etc. to the queue')
|
||||
.setRequired(true)
|
||||
)
|
||||
];
|
||||
|
||||
const rest = new REST({ version: '9' }).setToken(token);
|
||||
|
||||
try {
|
||||
console.log('Started refreshing application (/) commands.');
|
||||
|
||||
await rest.put(
|
||||
Routes.applicationCommands(clientId),
|
||||
{ body: commands },
|
||||
);
|
||||
|
||||
console.log('Successfully reloaded application (/) commands.');
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.registerCommands = registerCommands;
|
||||
Reference in New Issue
Block a user