From 5d6103a0aab85bbf3c0e6729dc78ecbf6ce7628a Mon Sep 17 00:00:00 2001 From: Entarno54 Date: Sat, 24 May 2025 02:12:40 +0700 Subject: [PATCH] More comments and changed README.md --- .gitignore | 7 ++----- README.md | 11 +++++----- commands/voice.js | 53 ++++++++++++++++++++++++++++++----------------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 125949f..ad95902 100644 --- a/.gitignore +++ b/.gitignore @@ -103,7 +103,6 @@ dist # vuepress v2.x temp and cache directory .temp -.cache # Docusaurus cache and generated files .docusaurus @@ -129,7 +128,5 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* - - -.env -music/ \ No newline at end of file +music/ +.idea \ No newline at end of file diff --git a/README.md b/README.md index 01d18c1..6abacb8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Squod Administrator / Fun bot. +# Squog Administrator / Fun bot. Features: - Prefix command parser for discord.js @@ -7,10 +7,11 @@ Features: Every single part of code is commented to be understandable for others How to use: -- ```git clone https://git.squog.ru/entar/SquogAdmin.git``` -- ```cd SquogAdmin``` -- ```npm install``` -- ```npm run exec``` +- Clone the repository: ```git clone https://git.squog.ru/entar/SquogAdmin.git``` +- Change the directory into it: ```cd SquogAdmin``` +- Install dependencies: ```npm install``` +- Create a .env file where you provide the token: ```TOKEN = xxxx``` +- Run the bot: ```npm run exec``` ###### Code is F2U, keep it OpenSource ###### Crediting me is optional but i'd prefer to be there. \ No newline at end of file diff --git a/commands/voice.js b/commands/voice.js index ca9f645..a901bca 100644 --- a/commands/voice.js +++ b/commands/voice.js @@ -1,6 +1,5 @@ const {createAudioPlayer, createAudioResource, joinVoiceChannel, getVoiceConnection} = require("@discordjs/voice") -const { Message, VoiceState } = require("discord.js") -const {client, dir} = require("../index") +const {dir} = require("../index") const { AudioPlayerStatus } = require("@discordjs/voice") const YTDlpWrap = require("yt-dlp-wrap").default @@ -12,16 +11,16 @@ module.exports.commands = [ { name: "play", description: "Joins your VC and plays music by youtube link.", - arguments: ["link", false], // If its not a OBJECT type you provide it like that lol. + arguments: ["link", false], // If it's not an OBJECT type, you provide it like that lol. run: async (ctx, link, loop) => { // 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 + let channel; - // Checking if member is in a voice chat + // Checking if the member is in a voice chat if (member.voice) { channel = member.voice.channel } @@ -29,22 +28,22 @@ module.exports.commands = [ // Returns if the user is not in vc or the bot is already in one 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) { + } 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 + let connection; // Checking if the bot is already in the vc if (!ctx.guild.members.me.voice.channel) { - // Connecting to voice channel + // Connecting to the voice channel connection = joinVoiceChannel({ - channelId: channel.id, - guildId: channel.guild.id, + channelId: channel.id, // Jetbrains is very angry over this for no reason? + guildId: channel.guild.id, // Or maybe this, it shows String instead of Snowflake. adapterCreator: channel.guild.voiceAdapterCreator, }) } else { - // Getting a existing connection + // Getting an existing connection connection = getVoiceConnection(ctx.guild.id) } @@ -56,7 +55,7 @@ module.exports.commands = [ await reply.edit(`Downloading ${metadata.title}`) // Running yt-dlp (downloading music) - const music = ytDlp.exec([ + ytDlp.exec([ // Arguments to run yt-dlp with link, '-f', @@ -69,18 +68,22 @@ module.exports.commands = [ ]).on('close', () => { reply.edit(`Playing ${metadata.title}`) - // Stopping old audioplayer + // Stopping old audio player if (players[ctx.guild.id]) { players[ctx.guild.id].stop() } - if (!ctx.guild.members.me.voice || (ctx.guild.members.me.voice && !ctx.guild.members.me.voice.channel) || (ctx.guild.members.me.voice && ctx.guild.members.me.voice.channel && ctx.guild.members.me.voice.channel.id != channel.id)) { + // Checking if the bot is still in the VC and if it's the same VC. + if (!ctx.guild.members.me.voice || (ctx.guild.members.me.voice && !ctx.guild.members.me.voice.channel)|| + (ctx.guild.members.me.voice && ctx.guild.members.me.voice.channel && + ctx.guild.members.me.voice.channel.id !== channel.id)) { return reply.edit("Not in a VC anymore, not playing.") } // Connecting audio player const player = createAudioPlayer() - + + // Setting the global player to this players[ctx.guild.id] = player const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`) @@ -91,12 +94,14 @@ module.exports.commands = [ // Subscribing the connection to the player connection.subscribe(player) + // Make it play again and again if it's looping if (loop) { player.on("stateChange", (oldState, newState) => { - if (newState.status == AudioPlayerStatus.Idle && players[ctx.guild.id] != undefined) { - // Gotta create a new resource every time + if (newState.status === AudioPlayerStatus.Idle && players[ctx.guild.id] !== undefined) { + // We have to create a new resource every time const resource = createAudioResource(`${dir}/music/${metadata.title}.mp3`) + // Playing the music with the player player.play(resource) } }) @@ -110,27 +115,37 @@ module.exports.commands = [ run: (ctx) => { const voice = ctx.guild.members.me.voice + // Checking if bot is in a voice channel if (!voice || !players[ctx.guild.id]) { return ctx.reply("Not in a voice channel.") } + // Stopping the player players[ctx.guild.id].stop() + // Removing the player (needed on line 100 check) players[ctx.guild.id] = undefined } }, { name: "leave", description: "Leave the channel", - run: (ctx) => { + run: async (ctx) => { const voice = ctx.guild.members.me.voice + // Checking if bot is in a voice channel if (!voice || !players[ctx.guild.id]) { return ctx.reply("Not in a voice channel.") } + + // Stopping the player players[ctx.guild.id].stop() - voice.disconnect() + // Removing the player (needed on line 100 check) + players[ctx.guild.id] = undefined + + // Disconnecting, obviously. + await voice.disconnect() } } ]