import { Command, createCommand } from 'commander'; import { TweetFilter } from 'rettiwt-core'; import { output } from '../helper/CliUtils'; import { Rettiwt } from '../Rettiwt'; /** * Creates a new 'tweet' command which uses the given Rettiwt instance. * * @param rettiwt - The Rettiwt instance to use. * @returns The created 'tweet' command. */ function createTweetCommand(rettiwt: Rettiwt): Command { // Creating the 'tweet' command const tweet = createCommand('tweet').description('Access resources releated to tweets'); // Details tweet .command('details') .description('Fetch the details of tweet with the given id') .argument('', 'The id of the tweet whose details are to be fetched') .action(async (id: string) => { try { const details = await rettiwt.tweet.details(id); output(details); } catch (error) { output(error); } }); // Like tweet .command('like') .description('Like a tweet') .argument('', 'The tweet to like') .action(async (id: string) => { try { const result = await rettiwt.tweet.like(id); output(result); } catch (error) { output(error); } }); // List tweet .command('list') .description('Fetch the list of tweets in the tweet list with the given id') .argument('', 'The id of the tweet list') .argument('[count]', 'The number of tweets to fetch') .argument('[cursor]', 'The cursor to the batch of tweets to fetch') .action(async (id: string, count?: string, cursor?: string) => { try { const tweets = await rettiwt.tweet.list(id, count ? parseInt(count) : undefined, cursor); output(tweets); } catch (error) { output(error); } }); // Post tweet .command('post') .description('Post a tweet (text only)') .argument('', 'The text to post as a tweet') .option('-m, --media [string]', 'Comma-separated list of ids of the media item(s) to be posted') .option('-q, --quote [string]', 'The id of the tweet to quote in the tweet to be posted') .option( '-r, --reply [string]', 'The id of the tweet to which the reply is to be made, if the tweet is to be a reply', ) .action(async (text: string, options?: { media?: string; quote?: string; reply?: string }) => { try { const result = await rettiwt.tweet.post({ text: text, media: options?.media ? options?.media.split(',').map((item) => ({ id: item })) : undefined, quote: options?.quote, replyTo: options?.reply, }); output(result); } catch (error) { output(error); } }); // Retweet tweet .command('retweet') .description('Retweet a tweet') .argument('', 'The tweet to retweet') .action(async (id: string) => { try { const result = await rettiwt.tweet.retweet(id); output(result); } catch (error) { output(error); } }); // Retweeters tweet .command('retweeters') .description('Fetch the list of users who retweeted the given tweets') .argument('', 'The id of the tweet') .argument('[count]', 'The number of retweeters to fetch') .argument('[cursor]', 'The cursor to the batch of retweeters to fetch') .action(async (id: string, count?: string, cursor?: string) => { try { const tweets = await rettiwt.tweet.retweeters(id, count ? parseInt(count) : undefined, cursor); output(tweets); } catch (error) { output(error); } }); // Schedule tweet .command('schedule') .description('Schedule a tweet to be posted at a given date/time') .argument('', 'The text to post as a tweet') .argument('