Add 'local' command

This commit is contained in:
KaKi87 2024-01-28 23:17:53 +01:00
parent 7f034a659c
commit d8ee08f7bb

46
main.js
View file

@ -18,7 +18,8 @@ import {
botToken,
botWhitelist,
adbIp,
adbPort
adbPort,
localMediaRootPath
} from './config.js';
overrideConsole();
@ -29,7 +30,7 @@ const
{ intents: ['directMessages'] }
),
adb = command => new Promise((resolve, reject) => exec(
`adb -s ${adbIp}:${adbPort} shell ${command}`,
`adb -s ${adbIp}:${adbPort} shell "${command.replaceAll('"', '\\"')}"`,
(
error,
stdout,
@ -50,6 +51,17 @@ const
}
},
setConfig = async (id, config) => await writeFile(`./data/${id}.json`, JSON.stringify(config)),
parseMediaPath = path => {
const title = path
.slice(path.lastIndexOf('/') + 1, path.lastIndexOf('.'))
.replace(/[\s\-._()&+\[\],]/g, ' ')
.replace(/\s+/g, ' ');
return {
path,
title,
keywords: title.toLowerCase().split(' ')
};
},
handleCommand = async ({
content,
authorID,
@ -214,6 +226,36 @@ const
await setConfig(authorID, config);
response = { embed: { title: `✅ Playlist removed` } };
});
program
.command('local')
.alias('l')
.description('List & play local media')
.argument('[media]', 'Media title/partial title')
.action(async title => {
const mediaList = (await adb(`find ${localMediaRootPath} -iname "*.mkv" -o -iname "*.mp4"`)).split('\n').map(parseMediaPath);
if(!title)
return response = {
output: outdent `
📂 Local media :
${mediaList.map(item => `- \`${item.title}\``).join('\n')}
`
};
const
keywords = title.toLowerCase().split(' '),
matchingMediaList = mediaList.filter(item => keywords.every(keyword => item.keywords.includes(keyword)));
if(matchingMediaList.length === 0)
return response = { embed: { title: `❌ Media not found` } };
if(matchingMediaList.length > 1)
return response = {
output: outdent `
🔎 Multiple media found :
${matchingMediaList.map(item => `- \`${item.title}\``).join('\n')}
`
};
const [matchingMedia] = matchingMediaList;
await adb(`am start -a android.intent.action.VIEW -n com.android.gallery3d/.app.MovieActivity -d "${matchingMedia.path}" -e MediaPlayerType 2`);
return response = { embed: { title: `▶️ \`${matchingMedia.title}\`` } };
});
program
.command('kiwi')
.description('Launch Kiwi browser')