From 5a341a3281e1ae3b097246726d4cf068cf851cd3 Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Sat, 4 May 2024 16:29:27 +0200 Subject: [PATCH] Add files via upload --- README.md | 7 +++-- dist/index.d.mts | 5 ++-- dist/index.d.ts | 5 ++-- dist/index.js | 60 +++++++++++++++++++++++++++++------------- dist/index.mjs | 50 ++++++++++++++++++++++------------- package-lock.json | 24 +++++++++++++++-- package.json | 2 ++ src/functions/start.ts | 56 ++++++++++++++++++++++++++------------- src/functions/stop.ts | 6 +++-- src/index.ts | 1 + src/stuff/error.ts | 1 + src/stuff/types.ts | 5 ++-- 12 files changed, 150 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index a0099a8..bb9dc61 100644 --- a/README.md +++ b/README.md @@ -35,9 +35,8 @@ Audio.StreamStart({ imvci: 0, //Voice Channel ID e.g. interaction.member.voice.channel.id igi: 0, //Guild ID e.g. interaction.guild.id igv: 0, //Bot Voice Adapter e.g. interaction.guild.voiceAdapterCreator - type: "Link", //Choose the Stream resource - File or Link - StreamFile: "", //Link to File e.g. ../assets/Stream.mp3 - StreamLink: "" //Link to Audio Stream e.g. YouTube Video or LautFM Stream Link + type: "", //Choose the Stream resource - File or Link or Analyze + Resource: "", //Link to File e.g. ../assets/Stream.mp3 or Link to Audio Stream e.g. YouTube Video or LautFM Stream Link }) ``` @@ -52,4 +51,4 @@ Audio.StreamStart({ ## 🤝 Enjoy the package? -Give it a star ⭐ on github! +Give it a star ⭐ on github or [donate](https://buymeacoffee.com/fraujuliannn) a hot chocolate! \ No newline at end of file diff --git a/dist/index.d.mts b/dist/index.d.mts index 8c4a24e..764e82e 100644 --- a/dist/index.d.mts +++ b/dist/index.d.mts @@ -3,14 +3,13 @@ type StartProps = { igi: number; igv: number; type: string; - StreamFile?: string; - StreamLink?: string; + Resource: string; }; type StopProps = { igi: number; }; -declare function StreamStart({ imvci, igi, igv, type, StreamFile, StreamLink }: StartProps): void; +declare function StreamStart({ imvci, igi, igv, type, Resource, }: StartProps): void; declare function StreamStop({ igi, }: StopProps): void; diff --git a/dist/index.d.ts b/dist/index.d.ts index 8c4a24e..764e82e 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -3,14 +3,13 @@ type StartProps = { igi: number; igv: number; type: string; - StreamFile?: string; - StreamLink?: string; + Resource: string; }; type StopProps = { igi: number; }; -declare function StreamStart({ imvci, igi, igv, type, StreamFile, StreamLink }: StartProps): void; +declare function StreamStart({ imvci, igi, igv, type, Resource, }: StartProps): void; declare function StreamStop({ igi, }: StopProps): void; diff --git a/dist/index.js b/dist/index.js index 68e5943..bf35d23 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1,7 +1,9 @@ "use strict"; +var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; +var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) @@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => { } return to; }; +var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( + // If the importer is in node compatibility mode or this is not an ESM + // file that has been converted to a CommonJS file using a Babel- + // compatible transform (i.e. "__esModule" has not been set), then set + // "default" to the CommonJS "module.exports" for node compatibility. + isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, + mod +)); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts @@ -38,34 +48,46 @@ function ERROR() { } // src/functions/start.ts +var import_fs = __toESM(require("fs")); +var import_node_path = require("path"); var { joinVoiceChannel, createAudioPlayer, createAudioResource } = require("@discordjs/voice"); -var { join } = require("path"); function StreamStart({ imvci, igi, igv, type, - StreamFile, - StreamLink + Resource }) { try { + let FileResource2 = function() { + let Audio = createAudioResource((0, import_node_path.join)(__dirname, Resource), { inlineVolume: true }); + AudioPlayer.play(Audio); + joinVoiceChannel({ + channelId: imvci, + guildId: igi, + adapterCreator: igv + }).subscribe(AudioPlayer); + }, LinkResource2 = function() { + let Audio = createAudioResource(Resource); + AudioPlayer.play(Audio); + joinVoiceChannel({ + channelId: imvci, + guildId: igi, + adapterCreator: igv + }).subscribe(AudioPlayer); + }; + var FileResource = FileResource2, LinkResource = LinkResource2; const AudioPlayer = createAudioPlayer(); - if (type === "File") { - let Audio = createAudioResource(StreamLink); - AudioPlayer.play(Audio); - joinVoiceChannel({ - channelId: imvci, - guildId: igi, - adapterCreator: igv - }).subscribe(AudioPlayer); - } else if (type === "Link") { - const Audio = createAudioResource(join(__dirname, StreamFile)); - AudioPlayer.play(Audio); - joinVoiceChannel({ - channelId: imvci, - guildId: igi, - adapterCreator: igv - }).subscribe(AudioPlayer); + if (type === "Link") { + LinkResource2(); + } else if (type === "File") { + FileResource2(); + } else if (type === "Analyze") { + if (import_fs.default.existsSync(Resource)) { + FileResource2(); + } else { + LinkResource2(); + } } else { ERROR(); } diff --git a/dist/index.mjs b/dist/index.mjs index c83c376..23681c6 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -26,34 +26,46 @@ function ERROR() { } // src/functions/start.ts +import fs from "fs"; +import { join } from "node:path"; var { joinVoiceChannel, createAudioPlayer, createAudioResource } = __require("@discordjs/voice"); -var { join } = __require("node:path"); function StreamStart({ imvci, igi, igv, type, - StreamFile, - StreamLink + Resource }) { try { + let FileResource2 = function() { + let Audio = createAudioResource(join(__dirname, Resource), { inlineVolume: true }); + AudioPlayer.play(Audio); + joinVoiceChannel({ + channelId: imvci, + guildId: igi, + adapterCreator: igv + }).subscribe(AudioPlayer); + }, LinkResource2 = function() { + let Audio = createAudioResource(Resource); + AudioPlayer.play(Audio); + joinVoiceChannel({ + channelId: imvci, + guildId: igi, + adapterCreator: igv + }).subscribe(AudioPlayer); + }; + var FileResource = FileResource2, LinkResource = LinkResource2; const AudioPlayer = createAudioPlayer(); - if (type === "File") { - let Audio = createAudioResource(StreamLink); - AudioPlayer.play(Audio); - joinVoiceChannel({ - channelId: imvci, - guildId: igi, - adapterCreator: igv - }).subscribe(AudioPlayer); - } else if (type === "Link") { - const Audio = createAudioResource(join(__dirname, StreamFile)); - AudioPlayer.play(Audio); - joinVoiceChannel({ - channelId: imvci, - guildId: igi, - adapterCreator: igv - }).subscribe(AudioPlayer); + if (type === "Link") { + LinkResource2(); + } else if (type === "File") { + FileResource2(); + } else if (type === "Analyze") { + if (fs.existsSync(Resource)) { + FileResource2(); + } else { + LinkResource2(); + } } else { ERROR(); } diff --git a/package-lock.json b/package-lock.json index d4d65f7..f0995ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,19 @@ { "name": "discord-audio-stream", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "discord-audio-stream", - "version": "0.0.2", + "version": "0.0.3", "license": "BSD-3-Clause", "dependencies": { "@discordjs/voice": "^0.16.1", "discord.js": "^14.14.1", + "ffmpeg": "^0.0.4", "ffmpeg-static": "^5.2.0", + "fs": "^0.0.1-security", "libsodium-wrappers": "^0.7.13", "opusscript": "^0.0.8" }, @@ -1313,6 +1315,14 @@ "reusify": "^1.0.4" } }, + "node_modules/ffmpeg": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/ffmpeg/-/ffmpeg-0.0.4.tgz", + "integrity": "sha512-3TgWUJJlZGQn+crJFyhsO/oNeRRnGTy6GhgS98oUCIfZrOW5haPPV7DUfOm3xJcHr5q3TJpjk2GudPutrNisRA==", + "dependencies": { + "when": ">= 0.0.1" + } + }, "node_modules/ffmpeg-static": { "version": "5.2.0", "resolved": "https://registry.npmjs.org/ffmpeg-static/-/ffmpeg-static-5.2.0.tgz", @@ -1368,6 +1378,11 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/fs": { + "version": "0.0.1-security", + "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz", + "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==" + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -2422,6 +2437,11 @@ "webidl-conversions": "^4.0.2" } }, + "node_modules/when": { + "version": "3.7.8", + "resolved": "https://registry.npmjs.org/when/-/when-3.7.8.tgz", + "integrity": "sha512-5cZ7mecD3eYcMiCH4wtRPA5iFJZ50BJYDfckI5RRpQiktMiYTcn0ccLTZOvcbBume+1304fQztxeNzNS9Gvrnw==" + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index f7a78d5..3a0905e 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,9 @@ "dependencies": { "@discordjs/voice": "^0.16.1", "discord.js": "^14.14.1", + "ffmpeg": "^0.0.4", "ffmpeg-static": "^5.2.0", + "fs": "^0.0.1-security", "libsodium-wrappers": "^0.7.13", "opusscript": "^0.0.8" } diff --git a/src/functions/start.ts b/src/functions/start.ts index 0c4cf65..33d61ab 100644 --- a/src/functions/start.ts +++ b/src/functions/start.ts @@ -1,34 +1,26 @@ import { StartProps } from "../stuff/types"; import { ERROR } from "../stuff/error"; +import fs from "fs"; +import { join } from "node:path"; +import { AudioPlayer } from "@discordjs/voice"; const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require("@discordjs/voice"); -const { join } = require('node:path'); export function StreamStart({ imvci, igi, igv, type, - StreamFile, - StreamLink + Resource, }: StartProps) { try { + //Create AudioPlayer variable. const AudioPlayer = createAudioPlayer(); - if (type === "File") { - - let Audio = createAudioResource(StreamLink); - AudioPlayer.play(Audio); - - joinVoiceChannel({ - channelId: imvci, - guildId: igi, - adapterCreator: igv - }).subscribe(AudioPlayer) - - } else if (type === "Link") { - - const Audio = createAudioResource(join(__dirname, StreamFile)); + //File Resource + function FileResource() { + + let Audio = createAudioResource(join(__dirname, Resource), { inlineVolume: true }); AudioPlayer.play(Audio); joinVoiceChannel({ @@ -36,11 +28,39 @@ export function StreamStart({ guildId: igi, adapterCreator: igv }).subscribe(AudioPlayer); - + } + + //Link Resource + function LinkResource() { + let Audio = createAudioResource(Resource); + AudioPlayer.play(Audio); + + joinVoiceChannel({ + channelId: imvci, + guildId: igi, + adapterCreator: igv + }).subscribe(AudioPlayer) + } + + //If the type is "Link". + if (type === "Link") { + LinkResource(); + //If the type is "File". + } else if (type === "File") { + FileResource(); + //If the type is "Analyze". + } else if (type === "Analyze") { + if (fs.existsSync(Resource)) { //Check if Resource is a file and exist. + FileResource(); + } else { //If Resoure is not a file or doesn't exist. + LinkResource(); + } } else { + //If no valid value is specified. ERROR(); } } catch (error) { + //If syntax is wrong. ERROR(); console.log("ERR:\n" + error); console.log(" "); diff --git a/src/functions/stop.ts b/src/functions/stop.ts index 1c77bcb..89913da 100644 --- a/src/functions/stop.ts +++ b/src/functions/stop.ts @@ -7,9 +7,11 @@ export function StreamStop({ igi, }: StopProps) { try { - + //Get connection from interaction guild. const connection = getVoiceConnection(igi); - connection.disconnect(); + + //Destroy connection from interaction guild. + connection.destroy(); } catch (error) { ERROR(); diff --git a/src/index.ts b/src/index.ts index 40ccf81..53d74c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,3 @@ +//Export Start and Stop Function export * from "./functions/start"; export * from "./functions/stop"; \ No newline at end of file diff --git a/src/stuff/error.ts b/src/stuff/error.ts index 18fc551..897896e 100644 --- a/src/stuff/error.ts +++ b/src/stuff/error.ts @@ -1,3 +1,4 @@ +//ERROR Message export function ERROR() { console.log(" "); console.log(" ______ _____ _____ ____ _____ \r\n | ____| __ \\| __ \\ \/ __ \\| __ \\ \r\n | |__ | |__) | |__) | | | | |__) |\r\n | __| | _ \/| _ \/| | | | _ \/ \r\n | |____| | \\ \\| | \\ \\| |__| | | \\ \\ \r\n |______|_| \\_\\_| \\_\\\\____\/|_| \\_\\"); diff --git a/src/stuff/types.ts b/src/stuff/types.ts index 1085f94..5eee456 100644 --- a/src/stuff/types.ts +++ b/src/stuff/types.ts @@ -1,12 +1,13 @@ +//Variables to use start function. export type StartProps = { imvci: number; igi: number; igv: number; type: string; - StreamFile?: string; - StreamLink?: string; + Resource: string; }; +//Variables to use stop function. export type StopProps = { igi: number; }; \ No newline at end of file