Type tweet task and CLI

This commit is contained in:
Jordan Eldredge 2020-04-25 01:50:45 -04:00
parent b8ed7ec39e
commit 0ff0c702a9
3 changed files with 27 additions and 23 deletions

View file

@ -1,16 +1,16 @@
#!/usr/bin/env node
const path = require("path");
const fs = require("fs");
const argv = require("yargs").argv;
const fetchInternetArchiveMetadata = require("./tasks/fetchInternetArchiveMetadata");
const ensureInternetArchiveItemsIndexByMd5 = require("./tasks/ensureInternetArchiveItemsIndexByMd5");
const logger = require("./logger");
const DiscordWinstonTransport = require("./DiscordWinstonTransport");
const Skins = require("./data/skins");
const db = require("./db");
const Discord = require("discord.js");
const tweet = require("./tasks/tweet");
const { addSkinFromBuffer } = require("./addSkin");
import path from "path";
import fs from "fs";
import { argv } from "yargs";
import fetchInternetArchiveMetadata from "./tasks/fetchInternetArchiveMetadata";
import ensureInternetArchiveItemsIndexByMd5 from "./tasks/ensureInternetArchiveItemsIndexByMd5";
import logger from "./logger";
import DiscordWinstonTransport from "./DiscordWinstonTransport";
import * as Skins from "./data/skins";
import db from "./db";
import Discord from "discord.js";
import { tweet } from "./tasks/tweet";
import { addSkinFromBuffer } from "./addSkin";
async function main() {
const client = new Discord.Client();

View file

@ -1,6 +1,6 @@
import { Message } from "discord.js";
import tweet from "../../tasks/tweet";
import { tweet } from "../../tasks/tweet";
import { CAPTBARITONE_USER_ID } from "../../config";
async function handler(message: Message): Promise<void> {

View file

@ -1,11 +1,11 @@
const path = require("path");
const logger = require("../logger");
const Skins = require("../data/skins");
const { TWEET_BOT_CHANNEL_ID } = require("../config");
import path from "path";
import logger from "../logger";
import * as Skins from "../data/skins";
import { TWEET_BOT_CHANNEL_ID } from "../config";
import { spawn } from "child_process";
import { Client } from "discord.js";
const { spawn } = require("child_process");
function spawnPromise(command, args) {
function spawnPromise(command: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
const ls = spawn(command, args);
let stdout = "";
@ -31,13 +31,14 @@ function spawnPromise(command, args) {
});
}
async function tweet(discordClient) {
export async function tweet(discordClient: Client) {
const tweetBotChannel = discordClient.channels.get(TWEET_BOT_CHANNEL_ID);
if (tweetBotChannel == null) {
throw new Error("Could not connect to the #tweet-bot channel");
}
const tweetableSkin = await Skins.getSkinToTweet();
if (tweetableSkin == null) {
// @ts-ignore
await tweetBotChannel.send(
"Oops! I ran out of skins to tweet. Could someone please `!review` some more?"
);
@ -46,6 +47,9 @@ async function tweet(discordClient) {
}
const { md5, filename } = tweetableSkin;
if (filename == null) {
throw new Error(`Could not find filename for skin with hash ${md5}`);
}
const output = await spawnPromise(
// This will be run from the dist directory
path.resolve(__dirname, "../../../tweetBot/tweet.py"),
@ -57,9 +61,11 @@ async function tweet(discordClient) {
]
);
await Skins.markAsTweeted(md5);
// @ts-ignore
await tweetBotChannel.send(output.trim());
const remainingSkinCount = await Skins.getTweetableSkinCount();
if (remainingSkinCount < 10) {
// @ts-ignore
await tweetBotChannel.send(
`Only ${remainingSkinCount} approved skins left. Could someone please \`!review\` some more?`
);
@ -70,5 +76,3 @@ async function tweet(discordClient) {
url: output.trim(),
});
}
module.exports = tweet;