From 354cd4d44f13af2917283f9add9bc7474d327c7b Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Tue, 10 Sep 2024 22:00:43 +0700 Subject: [PATCH 1/2] Optimize Youtube Direct --- commands/Music/play.js | 70 ++++++++++++++++++++++++---------------- locales/en-US.json | 3 +- schemas/YoutubeDirect.js | 9 ++++++ 3 files changed, 53 insertions(+), 29 deletions(-) diff --git a/commands/Music/play.js b/commands/Music/play.js index 6af49b3..66fef44 100644 --- a/commands/Music/play.js +++ b/commands/Music/play.js @@ -47,23 +47,8 @@ module.exports = { autoPlay: true }); - if (isYouTubeUrl(query)) { - const canDirect = await YoutubeDirectSchema.findOne({ - userId: interaction.user.id - }); - - if (!(canDirect && canDirect.userId && canDirect.userId == interaction.user.id)) return interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setColor(Colors.Red) - .setTitle(locale.getLocaleString("command.play.youtube.disabled")) - .setDescription(locale.getLocaleString("command.play.youtube.disabled.description")) - ] - }); - } - - if (user_source ? sources[user_source.source].require.includes("YOUTUBE_DIRECT") : false) { - const canDirect = await YoutubeDirectSchema.findOne({ + async function checkYoutubeDirect() { + const ytDirect = await YoutubeDirectSchema.findOne({ userId: interaction.user.id }); @@ -75,17 +60,46 @@ module.exports = { const adsActionRow = new ActionRowBuilder() .addComponents(adsButton); - if (!(canDirect && canDirect.userId && canDirect.userId == interaction.user.id)) return interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setColor(Colors.Red) - .setDescription(locale.getLocaleString("youtube.direct.disable.text")) - .setImage("https://cdn.jsdelivr.net/gh/HStudioDiscordBot/Storage@main/ads/HStudio.ads.youtube_direct_user.png") - ], - components: [ - adsActionRow - ] - }); + if (!ytDirect) { + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setColor(Colors.Red) + .setDescription(locale.getLocaleString("youtube.direct.disable.text")) + .setImage("https://cdn.jsdelivr.net/gh/HStudioDiscordBot/Storage@main/ads/HStudio.ads.youtube_direct_user.png") + ], + components: [ + adsActionRow + ] + }); + return false; + } else if (ytDirect.expireAt ? ytDirect.expireAt.getTime() < Date.now() : false && !ytDirect.infinity) { + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setColor(Colors.Red) + .setDescription(locale.getLocaleString("youtube.direct.disable.expired")) + ], + components: [ + adsActionRow + ] + }); + return false; + } else { + return true; + } + } + + if (isYouTubeUrl(query)) { + if (!(await checkYoutubeDirect())) { + return; + } + } + + if (user_source ? sources[user_source.source].require.includes("YOUTUBE_DIRECT") : false) { + if (!(await checkYoutubeDirect())) { + return; + } } if (isHStudioPlayUrl(query)) { diff --git a/locales/en-US.json b/locales/en-US.json index d3f8d9e..af07c0e 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -120,5 +120,6 @@ "command.setting.search.saved": "💾 Saved default source with `{0}`", "command.setting.search.save.fail": "❌ Fail to save default source with `{0}`", "youtube.direct.disable.text": "# Want to listen to music directly from YouTube?\nYou can easily order through the HStudio website\n\n**[Support Discord](https://discord.gg/GzTbuZHTEx)**", - "youtube.direct.disable.button.text": "Buy now" + "youtube.direct.disable.button.text": "Buy now", + "youtube.direct.disable.expired": "# Youtube Directly is expired\nYou can't use this feature anymore\nYou can order through the Studio website" } \ No newline at end of file diff --git a/schemas/YoutubeDirect.js b/schemas/YoutubeDirect.js index f28c83e..f4af98b 100644 --- a/schemas/YoutubeDirect.js +++ b/schemas/YoutubeDirect.js @@ -2,6 +2,15 @@ const { model, Schema } = require("mongoose"); module.exports = model("youtubeDirect", new Schema({ userId: String, + expireAt: { + type: Date, + required: false + }, + infinity: { + type: Boolean, + required: true, + default: false + }, }, { timestamps: true })); \ No newline at end of file From 70b1117db4156548f3441bee42a54e6afacd71f9 Mon Sep 17 00:00:00 2001 From: HewkawAr Date: Tue, 10 Sep 2024 22:04:31 +0700 Subject: [PATCH 2/2] Update play.js --- commands/Music/play.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/Music/play.js b/commands/Music/play.js index 66fef44..60b53a7 100644 --- a/commands/Music/play.js +++ b/commands/Music/play.js @@ -73,7 +73,7 @@ module.exports = { ] }); return false; - } else if (ytDirect.expireAt ? ytDirect.expireAt.getTime() < Date.now() : false && !ytDirect.infinity) { + } else if ((ytDirect.expireAt ? ytDirect.expireAt.getTime() < Date.now() : false) && !ytDirect.infinity) { await interaction.editReply({ embeds: [ new EmbedBuilder()