mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-10 07:03:08 -04:00
[cmd] audio search
This commit is contained in:
parent
c02eda1b2a
commit
851bd2f75d
5 changed files with 184 additions and 2 deletions
97
commands/message/search/audio.js
Normal file
97
commands/message/search/audio.js
Normal file
|
@ -0,0 +1,97 @@
|
|||
const { createEmbed, formatPaginationEmbeds } = require('../../../labscore/utils/embed')
|
||||
const { link } = require('../../../labscore/utils/markdown')
|
||||
const { editOrReply } = require('../../../labscore/utils/message')
|
||||
|
||||
const { paginator } = require('../../../labscore/client');
|
||||
const { searchAudio } = require('../../../labscore/api');
|
||||
const { getRecentVideo } = require('../../../labscore/utils/attachment');
|
||||
const { renderMusicButtons } = require('../../../labscore/utils/buttons');
|
||||
|
||||
const superagent = require('superagent')
|
||||
|
||||
const urlr = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/g
|
||||
|
||||
module.exports = {
|
||||
name: 'audio',
|
||||
aliases: ['aud'],
|
||||
metadata: {
|
||||
description: 'video audio detection',
|
||||
examples: ['aud'],
|
||||
category: 'search',
|
||||
usage: 'audio <media>'
|
||||
},
|
||||
run: async (context) => {
|
||||
context.triggerTyping();
|
||||
try{
|
||||
|
||||
if(context.message.messageReference){
|
||||
let msg;
|
||||
try{
|
||||
msg = await context.message.channel.fetchMessage(context.message.messageReference.messageId)
|
||||
} catch(e){
|
||||
return editOrReply(context, {embeds:[createEmbed("error", context, "Unable to fetch message.")]})
|
||||
}
|
||||
console.log(msg)
|
||||
let urls = msg.content.match(urlr)
|
||||
console.log(urls)
|
||||
if(urls){
|
||||
try{
|
||||
let songlink = await superagent.get(`https://api.song.link/v1-alpha.1/links?url=${encodeURIComponent(urls[0])}`)
|
||||
let song = songlink.body.entitiesByUniqueId[songlink.body.entityUniqueId]
|
||||
|
||||
let btns = renderMusicButtons(songlink.body.linksByPlatform)
|
||||
console.log(JSON.stringify(btns, null, 2))
|
||||
return editOrReply(context, {embeds:[
|
||||
createEmbed("default", context, {
|
||||
author: {
|
||||
name: `${song.title} by ${song.artistName}`.substr(0,1000),
|
||||
iconUrl: song.thumbnailUrl,
|
||||
url: url
|
||||
},
|
||||
footer: {}
|
||||
})
|
||||
], components: btns})
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
} //ignore it and run the audio detection flow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let audios = await getRecentVideo(context, 50)
|
||||
if(!audios.length) return editOrReply(context, {embeds:[createEmbed("warning", context, `Could not find supported video.`)]})
|
||||
let audioSearch = await searchAudio(context, audios[0].url)
|
||||
search = audioSearch.response
|
||||
console.log(audioSearch.response.body)
|
||||
if(audioSearch.response.body.status == 0){
|
||||
// API lowkey sucks, fetch more metadata via songlink
|
||||
let url = audioSearch.response.body.media[Object.keys(audioSearch.response.body.media)[0]]
|
||||
if(audioSearch.response.body.media.deezer) url = audioSearch.response.body.media.deezer
|
||||
if(audioSearch.response.body.media.spotify) url = audioSearch.response.body.media.spotify
|
||||
let songlink = await superagent.get(`https://api.song.link/v1-alpha.1/links?url=${encodeURIComponent(url)}`)
|
||||
//get song meta
|
||||
let song = songlink.body.entitiesByUniqueId[songlink.body.entityUniqueId]
|
||||
|
||||
let btns = renderMusicButtons(songlink.body.linksByPlatform)
|
||||
console.log(JSON.stringify(btns, null, 2))
|
||||
return editOrReply(context, {embeds:[
|
||||
createEmbed("default", context, {
|
||||
author: {
|
||||
name: `${song.title} by ${song.artistName}`.substr(0,1000),
|
||||
iconUrl: song.thumbnailUrl,
|
||||
url: url
|
||||
},
|
||||
footer: {}
|
||||
})
|
||||
], components: btns})
|
||||
}
|
||||
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
if(e.response?.body?.status){
|
||||
return editOrReply(context, {embeds:[createEmbed("error", context, e.response.body.message)]})
|
||||
}
|
||||
return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to perform audio search.`)]})
|
||||
}
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue