Added music abd !play

This commit is contained in:
entar 2025-05-22 20:39:02 +07:00
parent 28d38422fd
commit 811465e3ad
5 changed files with 149 additions and 7 deletions

74
commands/voice.js Normal file
View File

@ -0,0 +1,74 @@
const {createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection} = require("@discordjs/voice")
const { Message } = require("discord.js")
const {client, dir} = require("../index")
const YTDlpWrap = require("yt-dlp-wrap").default
const ytDlp = new YTDlpWrap(`${dir}/yt-dlp`)
module.exports = [
{
name: "play",
description: "Hello tested for now",
arguments: ["link"], // If its not a OBJECT type you provide it like that lol.
run: async (ctx, link) => {
// Getting the guild member to get their voice client
const author = ctx.author
const member = await ctx.guild.members.fetch(author)
// Initiating the channel variable (undefined)
var channel
// Checking if member is in a voice chat
if (member.voice) {
channel = member.voice.channel
}
// Returns
if (!channel) {
return ctx.reply("You are not in a VC right now.")
} else if (ctx.guild.members.me.voice.channel && ctx.guild.members.me.voice.channel.id != channel.id) {
return ctx.reply("Already in a vc.")
}
var connection
// Checking if the bot is already in the vc
if (!ctx.guild.members.me.voice.channel) {
// Connecting to voice channel
connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
})
} else {
// Getting a existing connection
connection = getVoiceConnection(ctx.guild.id)
}
console.log(connection)
const metadata = await ytDlp.getVideoInfo(link);
ctx.reply(`Playing ${metadata.title}`)
const music = ytDlp.exec([
link,
'-f',
'best',
'-o',
`${dir}/music/${metadata.title}.mp3`,
'--extract-audio',
'--audio-format',
'mp3'
]).on('close', () => {
const player = createAudioPlayer()
const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`)
player.play(resource)
connection.subscribe(player)
})
}
}
]

View File

@ -63,7 +63,7 @@ function getCommand(commandName) {
}
// Parsing commands on message create
client.on("messageCreate", (message) => {
client.on("messageCreate", async (message) => {
var content = message.content
// Continue only if starts with the prefix
@ -92,7 +92,11 @@ client.on("messageCreate", (message) => {
if (!realCommand.arguments) {
// No arguments so no need to parse them
// Run command with no args
realCommand.run(message)
if (realCommand.run.constructor.name == "AsyncFunction"){
await realCommand.run(message)
} else {
realCommand.run(message)
}
} else {
// Removing command from the args
const args = parsed.filter(value => {return value != command})
@ -144,7 +148,11 @@ client.on("messageCreate", (message) => {
}
// Run command
realCommand.run(message, ...args)
if (realCommand.run.constructor.name == "AsyncFunction"){
await realCommand.run(message, ...args)
} else {
realCommand.run(message, ...args)
}
//...args passes it as separate args rather than an Array
}
}

64
package-lock.json generated
View File

@ -9,9 +9,10 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"discord.js": "^14.19.3"
},
"devDependencies": {}
"@discordjs/voice": "^0.18.0",
"discord.js": "^14.19.3",
"yt-dlp-wrap": "^2.3.12"
}
},
"node_modules/@discordjs/builders": {
"version": "1.11.2",
@ -105,6 +106,31 @@
"url": "https://github.com/discordjs/discord.js?sponsor"
}
},
"node_modules/@discordjs/voice": {
"version": "0.18.0",
"resolved": "https://registry.npmjs.org/@discordjs/voice/-/voice-0.18.0.tgz",
"integrity": "sha512-BvX6+VJE5/vhD9azV9vrZEt9hL1G+GlOdsQaVl5iv9n87fkXjf3cSwllhR3GdaUC8m6dqT8umXIWtn3yCu4afg==",
"license": "Apache-2.0",
"dependencies": {
"@types/ws": "^8.5.12",
"discord-api-types": "^0.37.103",
"prism-media": "^1.3.5",
"tslib": "^2.6.3",
"ws": "^8.18.0"
},
"engines": {
"node": ">=18"
},
"funding": {
"url": "https://github.com/discordjs/discord.js?sponsor"
}
},
"node_modules/@discordjs/voice/node_modules/discord-api-types": {
"version": "0.37.120",
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.120.tgz",
"integrity": "sha512-7xpNK0EiWjjDFp2nAhHXezE4OUWm7s1zhc/UXXN6hnFFU8dfoPHgV0Hx0RPiCa3ILRpdeh152icc68DGCyXYIw==",
"license": "MIT"
},
"node_modules/@discordjs/ws": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/@discordjs/ws/-/ws-1.2.2.tgz",
@ -261,6 +287,32 @@
"integrity": "sha512-ThQLOhN86ZkJ7qemtVRGYM+gRgR8GEXNli9H/PMvpnZsE44Xfh3wx9kGJaldg314v85m+bFW6WBMaVHJc/c3zA==",
"license": "MIT"
},
"node_modules/prism-media": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.3.5.tgz",
"integrity": "sha512-IQdl0Q01m4LrkN1EGIE9lphov5Hy7WWlH6ulf5QdGePLlPas9p2mhgddTEHrlaXYjjFToM1/rWuwF37VF4taaA==",
"license": "Apache-2.0",
"peerDependencies": {
"@discordjs/opus": ">=0.8.0 <1.0.0",
"ffmpeg-static": "^5.0.2 || ^4.2.7 || ^3.0.0 || ^2.4.0",
"node-opus": "^0.3.3",
"opusscript": "^0.0.8"
},
"peerDependenciesMeta": {
"@discordjs/opus": {
"optional": true
},
"ffmpeg-static": {
"optional": true
},
"node-opus": {
"optional": true
},
"opusscript": {
"optional": true
}
}
},
"node_modules/ts-mixer": {
"version": "6.0.4",
"resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.4.tgz",
@ -308,6 +360,12 @@
"optional": true
}
}
},
"node_modules/yt-dlp-wrap": {
"version": "2.3.12",
"resolved": "https://registry.npmjs.org/yt-dlp-wrap/-/yt-dlp-wrap-2.3.12.tgz",
"integrity": "sha512-P8fJ+6M1YjukyJENCTviNLiZ8mokxprR54ho3DsSKPWDcac489OjRiStGEARJr6un6ETS6goTn4CWl/b/rM3aA==",
"license": "MIT"
}
}
}

View File

@ -14,7 +14,9 @@
"test": "node index.js"
},
"dependencies": {
"discord.js": "^14.19.3"
"@discordjs/voice": "^0.18.0",
"discord.js": "^14.19.3",
"yt-dlp-wrap": "^2.3.12"
},
"module": "true"
}

BIN
yt-dlp Executable file

Binary file not shown.