diff --git a/commands/Music/play.js b/commands/Music/play.js index b1a735a..ba2814a 100644 --- a/commands/Music/play.js +++ b/commands/Music/play.js @@ -3,6 +3,9 @@ const { convertToHHMMSS, msToSec } = require("../../utils/time"); const { isYouTubeUrl, isHStudioPlayUrl } = require("../../utils/youtube"); const AdsSchema = require("../../schemas/Ad"); const YoutubeDirectSchema = require("../../schemas/YoutubeDirect"); +const DefaultSourceSchema = require("../../schemas/DefaultSource"); + +const sources = require("../../configs/sources.json"); module.exports = { data: new SlashCommandBuilder() @@ -32,6 +35,10 @@ module.exports = { let query = interaction.options.getString("query"); + const user_source = await DefaultSourceSchema.findOne({ + userId: interaction.user.id + }); + let player = client.moon.createPlayer({ guildId: interaction.guild.id, voiceChannelId: interaction.member.voice.channel.id, @@ -68,7 +75,7 @@ module.exports = { let res = await client.moon.search({ query, - source: "spsearch", + source: user_source ? sources[user_source.source] : "spsearch", requester: interaction.user.id }); diff --git a/commands/Utils/setting.js b/commands/Utils/setting.js new file mode 100644 index 0000000..6c0be08 --- /dev/null +++ b/commands/Utils/setting.js @@ -0,0 +1,90 @@ +const { SlashCommandBuilder, EmbedBuilder, Colors } = require("discord.js"); +const DefaultSourceSchema = require("../../schemas/DefaultSource"); + +module.exports = { + data: new SlashCommandBuilder() + .setName("setting") + .setDescription("Setting") + .setDescriptionLocalizations({ + th: "การตั้งค่า" + }) + .addSubcommand((sub) => + sub.setName("search") + .setDescription("Change default search source") + .setDescriptionLocalizations({ + th: "การตั้งค่าการค้นหาเริ่มต้น" + }) + .addStringOption((option) => + option + .setName("source") + .setDescription("Source") + .setDescriptionLocalizations({ + th: "แหล่งที่มา" + }) + .setRequired(true) + .setChoices( + { + name: "Spotify", + value: "spotify" + }, + { + name: "Youtube", + value: "youtube" + }, + { + name: "Youtube Music", + value: "youtube_music" + } + )) + ), + + /** + * + * @param {import("discord.js").CommandInteraction} interaction + * @param {import("discord.js").Client} client + * @param {import("../../class/Locale")} locale + */ + async execute(interaction, client, locale) { + const sub = interaction.options.getSubcommand(); + + switch (sub) { + case "search": { + await interaction.deferReply(); + const source = interaction.options.getString("source"); + + if (!source) return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setColor(Colors.Red) + .setDescription(locale.replacePlaceholders(locale.getLocaleString("command.setting.search.save.fail"), [source])) + ] + }); + + const oldConfig = await DefaultSourceSchema.findOne({ + userId: interaction.user.id + }); + + if (oldConfig) { + await DefaultSourceSchema.updateOne({ + userId: interaction.user.id + }, { + source: source + }); + } else { + await DefaultSourceSchema.create({ + userId: interaction.user.id, + source: source + }); + } + + await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setColor(Colors.Blue) + .setDescription(locale.replacePlaceholders(locale.getLocaleString("command.setting.search.saved"), [source])) + ] + }); + } + } + } +}; \ No newline at end of file diff --git a/configs/sources.json b/configs/sources.json new file mode 100644 index 0000000..ee25d80 --- /dev/null +++ b/configs/sources.json @@ -0,0 +1,5 @@ +{ + "spotify": "spsearch", + "youtube": "ytsearch", + "youtube_music": "ytmsearch" +} \ No newline at end of file diff --git a/locales/en-US.json b/locales/en-US.json index 299b611..2f82fc1 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -116,5 +116,7 @@ "moonlink.trackStart.withoutUrl": "{0} Start playing **{1}**", "moonlink.trackEnd": "There are no more tracks", "command.clear.success": "🗑️ Clear Queue Success", - "command.clear.fail": "❌ Can't clear queue" + "command.clear.fail": "❌ Can't clear queue", + "command.setting.search.saved": "💾 Saved default source with `{0}`", + "command.setting.search.save.fail": "❌ Fail to save default source with `{0}`" } \ No newline at end of file diff --git a/schemas/DefaultSource.js b/schemas/DefaultSource.js new file mode 100644 index 0000000..3918add --- /dev/null +++ b/schemas/DefaultSource.js @@ -0,0 +1,8 @@ +const { model, Schema } = require("mongoose"); + +module.exports = model("default_source", new Schema({ + userId: String, + source: String +}, { + timestamps: true +})); \ No newline at end of file