diff --git a/commands/media/tiktok.js b/commands/media/tiktok.js index 4400d29..ca19c3c 100644 --- a/commands/media/tiktok.js +++ b/commands/media/tiktok.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder } = require('discord.js'); -const { ListObjectsV2Command, PutObjectCommand } = require('@aws-sdk/client-s3'); +const { ListObjectsV2Command, PutObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3'); const { execSync } = require('child_process'); const fs = require('node:fs'); const S3 = require('../../s3'); @@ -20,8 +20,13 @@ module.exports = { .setDescription('Displays an embedded TikTok video.') .addStringOption(option => option.setName('url') .setDescription('The TikTok video URL.') - .setRequired(true)), async execute(interaction) { + .setRequired(true)) + .addBooleanOption(option => option.setName('force') + .setDescription('Re-download?')), + async execute(interaction) { const videoUrl = interaction.options.getString('url'); + const force = interaction.options.getBoolean('force') || false; + console.log({ videoUrl, force }); const replyMsg = await interaction.reply('Processing TikTok link...'); @@ -41,11 +46,19 @@ module.exports = { console.log(data); - if (data.KeyCount === 0) { + if (data.KeyCount !== 0 && force) { + console.log('Existing video with `force`, deleting'); + await S3.send(new DeleteObjectCommand({ + Bucket: S3_BUCKET_NAME, + Key: data.Contents[0].Key, + })); + } + + if (data.KeyCount === 0 || force) { // download the video execSync('mkdir -p tmp/tiktok'); try { - execSync(`yt-dlp -q -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best -o ./tmp/tiktok/${hash}.mp4 ` + videoUrl, { + execSync(`yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best -S vcodec:h264 -o ./tmp/tiktok/${hash}.mp4 ` + videoUrl, { stdio: 'inherit', }); } catch (e) { @@ -57,7 +70,7 @@ module.exports = { // get width and height let width = 0, height = 0; try { - const res = '' + execSync(`ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of json ./tmp/tiktok/${hash}.mp4`); + const res = execSync(`ffprobe -v error -select_streams v:0 -show_entries stream=width,height,codec_name -of json ./tmp/tiktok/${hash}.mp4`).toString(); console.log(res); const stream = JSON.parse(res).streams[0]; width = stream.width; diff --git a/index.js b/index.js index 39ea945..adbc55a 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,24 @@ client.on(Events.InteractionCreate, async interaction => { }); client.on(Events.MessageCreate, async message => { - const content = message.content; + let msgRef = null; + + try { + msgRef = await message.fetchReference(); + } catch (e) { + // do nothing + } + + let content = message.content; + let force = null; + + if (content.toLocaleLowerCase().trim() === 'liben redo' && msgRef) { + content = msgRef.content; + force = true; + + message = msgRef; + } + const videoUrlMatch = content.match(/https:\/\/(?:m|www|vm)?\.?tiktok\.com\/((?:.*\b(?:(?:usr|v|embed|user|video)\/|\?shareId=|&item_id=)(\d+))|\w+)/gim); if (!videoUrlMatch) { @@ -72,6 +89,7 @@ client.on(Events.MessageCreate, async message => { client.emit(Events.InteractionCreate, { isChatInputCommand: () => true, client: client, commandName: 'tiktok', options: { getString: () => videoUrl, + getBoolean: () => force, }, reply: message.reply.bind(message), }); });