Skip to content

Instantly share code, notes, and snippets.

@Strandxo
Last active November 1, 2019 10:46
Show Gist options
  • Save Strandxo/a8b1fb5a378decf9f4ab5fc23990ce47 to your computer and use it in GitHub Desktop.
Save Strandxo/a8b1fb5a378decf9f4ab5fc23990ce47 to your computer and use it in GitHub Desktop.
Playing Music
module.exports = (bot) => {
bot.playCommand = (message, args) => {
const [song] = await bot.getSongs(args[0]);
const voiceChannel = message.member.voiceChannelID
if (!song) return message.channel.send("No songs found. try again!");
if (!voiceChannel) return message.channel.send("You need to be in a voice channel to play music.")
const player = await lavalink.join({
guild: message.guild.id,
channel: voiceChannel,
host: lavalink.nodes.first().host
}, { selfdeaf: true });
if (!player) return message.channel.send("Could not join");
player.play(song.track);
player.once("error", console.error);
player.once("end", async (data) => {
if(data.reason === "REPLACED") return;
message.channel.send("Song has ended...");
await player.leave(message.guild.id);
});
return message.channel.send(`Now playing: **${song.info.title}** by *${song.info.author}*`);
}
};
const axios = require("axios");
module.exports = (bot) => {
bot.getSongs = async (identifier) => {
const node = lavalink.nodes.first();
const {data} = await axios.get(`http://${node.host}:${node.port}/loadtracks`, {
params: {
identifier
},
headers: {
Authorization: node.password
}
});
return data.tracks;
};
bot.decodeTrack = async (track) => {
const node = lavalink.nodes.first();
const {data} = await axios.get(`http://${node.host}:${node.port}/decodetrack`, {
params: {
track
},
headers: {
Authorization: node.password
}
});
return data;
};
};
module.exports = {
config: {
name: "play",
aliases: ["p"],
description: "Play a song from youtube.",
accessableby: "Member",
category: "music",
usage: "<input>"
},
run: (bot, message, args) => {
return bot.playCommand(message, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment