b68/packages/bot/src/commands/slash/search.ts

115 lines
3.6 KiB
TypeScript

import {
ActionRowBuilder,
SlashCommandBuilder,
StringSelectMenuBuilder,
StringSelectMenuInteraction,
} from 'discord.js'
import youtube, { Video } from 'youtube-sr'
import { bot } from '../../index'
import { i18n } from '../../utils/i18n'
import { SlashCommand } from '../../sturctures/command'
export const command: SlashCommand = {
slashData: new SlashCommandBuilder()
.setName('search')
.setDescription(i18n.__('search.description'))
.addStringOption((option) =>
option
.setName('query')
.setDescription(i18n.__('search.optionQuery'))
.setRequired(true)
),
run: async (itd) => {
const { interaction }: any = itd
const query = interaction.options.getString('query', true)
const member = interaction.guild!.members.cache.get(interaction.user.id)
if (!member?.voice.channel)
return interaction
.reply({
content: i18n.__('search.errorNotChannel'),
ephemeral: true,
})
.catch(console.error)
const search = query
await interaction.reply('⏳ Loading...').catch(console.error)
let results: Video[] = []
try {
results = await youtube.search(search, { limit: 10, type: 'video' })
} catch (error: any) {
console.error(error)
interaction
.editReply({ content: i18n.__('common.errorCommand') })
.catch(console.error)
}
if (!results) return
const options = results!.map((video) => {
return {
label: video.title ?? '',
value: video.url,
}
})
const row =
new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
new StringSelectMenuBuilder()
.setCustomId('search-select')
.setPlaceholder('Nothing selected')
.setMinValues(1)
.setMaxValues(10)
.addOptions(options)
)
const followUp = await interaction.followUp({
content: 'Choose songs to play',
components: [row],
})
followUp
.awaitMessageComponent({
time: 30000,
})
.then(
(selectInteraction: {
update: (arg0: {
content: string
components: never[]
}) => void
values: any[]
}) => {
if (
!(
selectInteraction instanceof
StringSelectMenuInteraction
)
)
return
selectInteraction.update({
content: '⏳ Loading the selected songs...',
components: [],
})
bot.slashCommandsMap
.get('play')!
.execute(interaction, selectInteraction.values[0])
.then(() => {
selectInteraction.values.slice(1).forEach((url) => {
bot.slashCommandsMap
.get('play')!
.execute(interaction, url)
})
})
}
)
.catch(console.error)
},
}