|
1 |
| -const generatePoToken = require('./generatePoToken'); |
2 |
| -modVersion = "s.v1.0 | AceTweaks" |
3 |
| - |
4 |
| -module.exports = { |
5 |
| - data: { |
6 |
| - name: "Play YouTube Song", |
7 |
| - }, |
8 |
| - category: "Music", |
9 |
| - info: { |
10 |
| - source: "https://github.com/slothyace/bmods-acedia/tree/main/QOLs", |
11 |
| - creator: "Acedia QOLs", |
12 |
| - donate: "https://ko-fi.com/slothyacedia", |
13 |
| - }, |
14 |
| - UI: [ |
15 |
| - { |
16 |
| - element: "input", |
17 |
| - name: "URL", |
18 |
| - placeholder: "YouTube Video URL", |
19 |
| - storeAs: "url", |
20 |
| - }, |
21 |
| - "-", |
22 |
| - { |
23 |
| - element: "dropdown", |
24 |
| - name: "Queuing", |
25 |
| - storeAs: "queuing", |
26 |
| - extraField: "queuePosition", |
27 |
| - choices: [ |
28 |
| - { name: "Don't Queue, Just Play" }, |
29 |
| - { name: "At End Of Queue" }, |
30 |
| - { name: "At Start Of Queue" }, |
31 |
| - { |
32 |
| - name: "At Custom Position", |
33 |
| - field: true, |
34 |
| - placeholder: "Queue Starts At #0", |
35 |
| - }, |
36 |
| - ], |
37 |
| - }, |
38 |
| - "-", |
39 |
| - { |
40 |
| - element: "input", |
41 |
| - name: "Timeout After x Seconds Of Trying To Fetch Audio | Leave Empty For 60s", |
42 |
| - placeholder: "20", |
43 |
| - storeAs: "timeoutAfter", |
44 |
| - }, |
45 |
| - { |
46 |
| - element: "condition", |
47 |
| - storeAs: "timeoutCondition", |
48 |
| - storeActionsAs: "timeoutActions", |
49 |
| - name: "If Timeout Occurs", |
50 |
| - }, |
51 |
| - { |
52 |
| - element: "text", |
53 |
| - text: modVersion |
54 |
| - } |
55 |
| - ], |
56 |
| - subtitle: (values, constants) => { |
57 |
| - return `URL: ${values.url} - ${values.queuing}`; |
58 |
| - }, |
59 |
| - compatibility: ["Any"], |
60 |
| - async run(values, message, client, bridge) { |
61 |
| - const fs = require('fs'); |
62 |
| - const search = require('yt-search'); |
63 |
| - const stream = require('stream'); |
64 |
| - const ytdl = require('@distube/ytdl-core'); |
65 |
| - const randInt = (Date.now()*Math.random()*1000*Math.random()*1000).toString().replaceAll(".","").replaceAll(",","").slice(0,16) |
66 |
| - const generatedFilePath = `./temp_${new Date().getTime()}_${randInt}.mp3` |
67 |
| - const { createAudioResource } = require('@discordjs/voice'); |
68 |
| - let timeoutDur = values.timeoutAfter ? parseInt(bridge.transf(values.timeoutAfter))*1000 : 60000 |
69 |
| - |
70 |
| - const result = await search(bridge.transf(values.url)); |
71 |
| - let url = result.videos[0]?.url || bridge.transf(values.url); |
72 |
| - try{ |
73 |
| - await Promise.race([ |
74 |
| - new Promise((resolve, reject) => { |
75 |
| - let stream = ytdl(url, { filter: 'audioonly' }).pipe(fs.createWriteStream(generatedFilePath)).on('finish', () => { |
76 |
| - stream.close(); |
77 |
| - resolve(); |
78 |
| - }).on("error", (err) =>{ |
79 |
| - fs.unlinkSync(generatedFilePath) |
80 |
| - reject(err) |
81 |
| - }) |
82 |
| - }), |
83 |
| - |
84 |
| - new Promise((_, reject) => setTimeout(()=> { |
85 |
| - reject(new Error(`Fetching Audio Took Too Long!`)) |
86 |
| - }, timeoutDur)) |
87 |
| - ]) |
88 |
| - } catch (err){ |
89 |
| - console.error(err) |
90 |
| - bridge.call(values.timeoutCondition, values.timeoutActions) |
91 |
| - fs.unlinkSync(generatedFilePath) |
92 |
| - return |
93 |
| - } |
94 |
| - |
95 |
| - let Readable = stream.Readable.from(fs.readFileSync(generatedFilePath)); |
96 |
| - const audio = createAudioResource(Readable); |
97 |
| - |
98 |
| - fs.unlinkSync(generatedFilePath); |
99 |
| - |
100 |
| - let utilities = bridge.getGlobal({ |
101 |
| - class: "voice", |
102 |
| - name: bridge.guild.id, |
103 |
| - }); |
104 |
| - |
105 |
| - |
106 |
| - switch (values.queuing) { |
107 |
| - case `Don't Queue, Just Play`: |
108 |
| - utilities.player.play(audio); |
109 |
| - utilities.nowPlaying = { |
110 |
| - file: null, |
111 |
| - name: result.videos[0].title, |
112 |
| - author: result.videos[0].author.name, |
113 |
| - url: bridge.transf(values.url), |
114 |
| - src: "YouTube", |
115 |
| - audio, |
116 |
| - raw: result.videos[0], |
117 |
| - playURL: url |
118 |
| - }; |
119 |
| - client.emit('trackStart', bridge.guild, utilities.channel, utilities.nowPlaying); |
120 |
| - break; |
121 |
| - |
122 |
| - case `At End Of Queue`: |
123 |
| - utilities.addToQueue(utilities.queue.length, { |
124 |
| - file: null, |
125 |
| - name: result.videos[0].title, |
126 |
| - author: result.videos[0].author.name, |
127 |
| - url: bridge.transf(values.url), |
128 |
| - src: "YouTube", |
129 |
| - audio: audio, |
130 |
| - raw: result.videos[0] |
131 |
| - }); |
132 |
| - break; |
133 |
| - |
134 |
| - case `At Start Of Queue`: |
135 |
| - utilities.addToQueue(0, { |
136 |
| - file: null, |
137 |
| - name: result.videos[0].title, |
138 |
| - author: result.videos[0].author.name, |
139 |
| - url: bridge.transf(values.url), |
140 |
| - src: "YouTube", |
141 |
| - audio: audio, |
142 |
| - raw: result.videos[0] |
143 |
| - }); |
144 |
| - break; |
145 |
| - |
146 |
| - case `At Custom Position`: |
147 |
| - utilities.addToQueue(Number(bridge.transf(values.queuePosition)), { |
148 |
| - file: null, |
149 |
| - name: result.videos[0].title, |
150 |
| - author: result.videos[0].author.name, |
151 |
| - url: bridge.transf(values.url), |
152 |
| - src: "YouTube", |
153 |
| - audio: audio, |
154 |
| - raw: result.videos[0] |
155 |
| - }); |
156 |
| - break; |
157 |
| - } |
158 |
| - }, |
159 |
| -}; |
| 1 | +const generatePoToken = require('./generatePoToken'); |
| 2 | +modVersion = "s.v1.0 | AceTweaks" |
| 3 | + |
| 4 | +module.exports = { |
| 5 | + data: { |
| 6 | + name: "Play YouTube Song", |
| 7 | + }, |
| 8 | + category: "Music", |
| 9 | + info: { |
| 10 | + source: "https://github.com/slothyace/bmods-acedia/tree/main/QOLs", |
| 11 | + creator: "Acedia QOLs", |
| 12 | + donate: "https://ko-fi.com/slothyacedia", |
| 13 | + }, |
| 14 | + UI: [ |
| 15 | + { |
| 16 | + element: "input", |
| 17 | + name: "URL", |
| 18 | + placeholder: "YouTube Video URL", |
| 19 | + storeAs: "url", |
| 20 | + }, |
| 21 | + "-", |
| 22 | + { |
| 23 | + element: "dropdown", |
| 24 | + name: "Queuing", |
| 25 | + storeAs: "queuing", |
| 26 | + extraField: "queuePosition", |
| 27 | + choices: [ |
| 28 | + { name: "Don't Queue, Just Play" }, |
| 29 | + { name: "At End Of Queue" }, |
| 30 | + { name: "At Start Of Queue" }, |
| 31 | + { |
| 32 | + name: "At Custom Position", |
| 33 | + field: true, |
| 34 | + placeholder: "Queue Starts At #0", |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + "-", |
| 39 | + { |
| 40 | + element: "input", |
| 41 | + name: "Timeout After x Seconds Of Trying To Fetch Audio | Leave Empty For 60s", |
| 42 | + placeholder: "20", |
| 43 | + storeAs: "timeoutAfter", |
| 44 | + }, |
| 45 | + { |
| 46 | + element: "condition", |
| 47 | + storeAs: "timeoutCondition", |
| 48 | + storeActionsAs: "timeoutActions", |
| 49 | + name: "If Timeout Occurs", |
| 50 | + }, |
| 51 | + { |
| 52 | + element: "text", |
| 53 | + text: modVersion |
| 54 | + } |
| 55 | + ], |
| 56 | + subtitle: (values, constants) => { |
| 57 | + return `URL: ${values.url} - ${values.queuing}`; |
| 58 | + }, |
| 59 | + compatibility: ["Any"], |
| 60 | + async run(values, message, client, bridge) { |
| 61 | + const fs = require('fs'); |
| 62 | + const search = require('yt-search'); |
| 63 | + const stream = require('stream'); |
| 64 | + const ytdl = require('@distube/ytdl-core'); |
| 65 | + const randInt = (Date.now()*Math.random()*1000*Math.random()*1000).toString().replaceAll(".","").replaceAll(",","").slice(0,16) |
| 66 | + const generatedFilePath = `./temp_${new Date().getTime()}_${randInt}.mp3` |
| 67 | + const { createAudioResource } = require('@discordjs/voice'); |
| 68 | + let timeoutDur = values.timeoutAfter ? parseInt(bridge.transf(values.timeoutAfter))*1000 : 60000 |
| 69 | + |
| 70 | + const result = await search(bridge.transf(values.url)); |
| 71 | + let url = result.videos[0]?.url || bridge.transf(values.url); |
| 72 | + try{ |
| 73 | + await Promise.race([ |
| 74 | + new Promise((resolve, reject) => { |
| 75 | + let stream = ytdl(url, { filter: 'audioonly' }).pipe(fs.createWriteStream(generatedFilePath)).on('finish', () => { |
| 76 | + stream.close(); |
| 77 | + resolve(); |
| 78 | + }).on("error", (err) =>{ |
| 79 | + fs.unlinkSync(generatedFilePath) |
| 80 | + reject(err) |
| 81 | + }) |
| 82 | + }), |
| 83 | + |
| 84 | + new Promise((_, reject) => setTimeout(()=> { |
| 85 | + reject(new Error(`Fetching Audio Took Too Long!`)) |
| 86 | + }, timeoutDur)) |
| 87 | + ]) |
| 88 | + } catch (err){ |
| 89 | + console.error(err) |
| 90 | + bridge.call(values.timeoutCondition, values.timeoutActions) |
| 91 | + fs.unlinkSync(generatedFilePath) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + let Readable = stream.Readable.from(fs.readFileSync(generatedFilePath)); |
| 96 | + const audio = createAudioResource(Readable); |
| 97 | + |
| 98 | + fs.unlinkSync(generatedFilePath); |
| 99 | + |
| 100 | + let utilities = bridge.getGlobal({ |
| 101 | + class: "voice", |
| 102 | + name: bridge.guild.id, |
| 103 | + }); |
| 104 | + |
| 105 | + |
| 106 | + switch (values.queuing) { |
| 107 | + case `Don't Queue, Just Play`: |
| 108 | + utilities.player.play(audio); |
| 109 | + utilities.nowPlaying = { |
| 110 | + file: null, |
| 111 | + name: result.videos[0].title, |
| 112 | + author: result.videos[0].author.name, |
| 113 | + url: bridge.transf(values.url), |
| 114 | + src: "YouTube", |
| 115 | + audio, |
| 116 | + raw: result.videos[0], |
| 117 | + playURL: url |
| 118 | + }; |
| 119 | + client.emit('trackStart', bridge.guild, utilities.channel, utilities.nowPlaying); |
| 120 | + break; |
| 121 | + |
| 122 | + case `At End Of Queue`: |
| 123 | + utilities.addToQueue(utilities.queue.length, { |
| 124 | + file: null, |
| 125 | + name: result.videos[0].title, |
| 126 | + author: result.videos[0].author.name, |
| 127 | + url: bridge.transf(values.url), |
| 128 | + src: "YouTube", |
| 129 | + audio: audio, |
| 130 | + raw: result.videos[0] |
| 131 | + }); |
| 132 | + break; |
| 133 | + |
| 134 | + case `At Start Of Queue`: |
| 135 | + utilities.addToQueue(0, { |
| 136 | + file: null, |
| 137 | + name: result.videos[0].title, |
| 138 | + author: result.videos[0].author.name, |
| 139 | + url: bridge.transf(values.url), |
| 140 | + src: "YouTube", |
| 141 | + audio: audio, |
| 142 | + raw: result.videos[0] |
| 143 | + }); |
| 144 | + break; |
| 145 | + |
| 146 | + case `At Custom Position`: |
| 147 | + utilities.addToQueue(Number(bridge.transf(values.queuePosition)), { |
| 148 | + file: null, |
| 149 | + name: result.videos[0].title, |
| 150 | + author: result.videos[0].author.name, |
| 151 | + url: bridge.transf(values.url), |
| 152 | + src: "YouTube", |
| 153 | + audio: audio, |
| 154 | + raw: result.videos[0] |
| 155 | + }); |
| 156 | + break; |
| 157 | + } |
| 158 | + }, |
| 159 | +}; |
0 commit comments