Add unmoderated run command

This commit is contained in:
FoxxMD
2021-06-14 10:26:05 -04:00
parent bdd72dc28e
commit eb62e39975
3 changed files with 83 additions and 58 deletions

View File

@@ -188,6 +188,7 @@ Options:
Commands:
run Runs bot normally
check [options] <activityIdentifier> [type] Run check(s) on a specific activity
unmoderated [options] <subreddits...> Run checks on all unmoderated activity in the modqueue
help [command] display help for command
```

View File

@@ -1,63 +1,67 @@
import commander, {InvalidOptionArgumentError} from "commander";
import {argParseInt, parseBool} from "../util";
export const getOptions = (): commander.Option[] => {
export const clientId = new commander.Option('-c, --clientId <id>', 'Client ID for your Reddit application (default: process.env.CLIENT_ID)')
.default(process.env.CLIENT_ID);
clientId.required = true;
export const clientSecret = new commander.Option('-e, --clientSecret <secret>', 'Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)')
.default(process.env.CLIENT_SECRET);
clientSecret.required = true;
export const accessToken = new commander.Option('-a, --accessToken <token>', 'Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)')
.default(process.env.ACCESS_TOKEN);
accessToken.required = true;
export const refreshToken = new commander.Option('-r, --refreshToken <token>', 'Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)')
.default(process.env.REFRESH_TOKEN);
refreshToken.required = true;
export const subreddits = new commander.Option('-s, --subreddits <list...>', 'List of subreddits to run on. Bot will run on all subs it has access to if not defined')
.default(process.env.SUBREDDITS || [], 'process.env.SUBREDDITS (comma-seperated)');
export const logDir = new commander.Option('-d, --logDir <dir>', 'Absolute path to directory to store rotated logs in')
.default(process.env.LOG_DIR || `${process.cwd()}/logs`, 'process.env.LOG_DIR || process.cwd()/logs');
export const logLevel = new commander.Option('-l, --logLevel <level>', 'Log level')
.default(process.env.LOG_LEVEL || 'info', 'process.env.LOG_LEVEL || info');
export const wikiConfig = new commander.Option('-w, --wikiConfig <path>', 'Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path>')
.default(process.env.WIKI_CONFIG || 'botconfig/contextbot', "process.env.WIKI_CONFIG || 'botconfig/contextbot'");
export const snooDebug = new commander.Option('--snooDebug', 'Set Snoowrap to debug')
.argParser(parseBool)
.default(process.env.SNOO_DEBUG || false, 'process.env.SNOO_DEBUG || false');
export const authorTTL = new commander.Option('--authorTTL <ms>', 'Set the TTL (ms) for the Author Activities shared cache')
.argParser(argParseInt)
.default(process.env.AUTHOR_TTL || 10000, 'process.env.AUTHOR_TTL || 10000');
export const heartbeat = new commander.Option('--heartbeat <s>', 'Interval, in seconds, between heartbeat logs. Set to 0 to disable')
.argParser(argParseInt)
//heartbeat.defaultValueDescription = 'process.env.HEARTBEAT || 300';
.default(process.env.HEARTBEAT || 300, 'process.env.HEARTBEAT || 300');
export const apiRemaining = new commander.Option('--apiLimitWarning <remaining>', 'When API limit remaining (600/10min) is lower than this value log statements for limit will be raised to WARN level')
.argParser(argParseInt)
.default(process.env.API_REMAINING || 250, 'process.env.API_REMAINING || 250');
export const dryRun = new commander.Option('--dryRun', 'Set dryRun=true for all checks/actions on all subreddits (overrides any existing)')
.argParser(parseBool)
.default(process.env.DRYRUN || false, 'process.env.DRYRUN || false');
export const disableCache = new commander.Option('--disableCache', 'Disable caching for all subreddits')
.argParser(parseBool)
.default(process.env.DISABLE_CACHE || false, 'process.env.DISABLE_CACHE || false');
export const checks = new commander.Option('-h, --checks <checkNames...>', 'An optional list of Checks, by name, that should be run. If none are specified all Checks for the Subreddit the Activity is in will be run');
export const limit = new commander.Option('--limit <limit>', 'Limit the number of unmoderated activities pulled for each subreddit')
.argParser(parseInt);
export const getUniversalOptions = (): commander.Option[] => {
let options = [];
const clientId = new commander.Option('-c, --clientId <id>', 'Client ID for your Reddit application (default: process.env.CLIENT_ID)')
.default(process.env.CLIENT_ID);
clientId.required = true;
const clientSecret = new commander.Option('-e, --clientSecret <secret>', 'Client Secret for your Reddit application (default: process.env.CLIENT_SECRET)')
.default(process.env.CLIENT_SECRET);
clientSecret.required = true;
const accessToken = new commander.Option('-a, --accessToken <token>', 'Access token retrieved from authenticating an account with your Reddit Application (default: process.env.ACCESS_TOKEN)')
.default(process.env.ACCESS_TOKEN);
accessToken.required = true;
const refreshToken = new commander.Option('-r, --refreshToken <token>', 'Refresh token retrieved from authenticating an account with your Reddit Application (default: process.env.REFRESH_TOKEN)')
.default(process.env.REFRESH_TOKEN);
refreshToken.required = true;
const subreddits = new commander.Option('-s, --subreddits <list...>', 'List of subreddits to run on. Bot will run on all subs it has access to if not defined')
.default(process.env.SUBREDDITS || [], 'process.env.SUBREDDITS (comma-seperated)');
const logDir = new commander.Option('-d, --logDir <dir>', 'Absolute path to directory to store rotated logs in')
.default(process.env.LOG_DIR || `${process.cwd()}/logs`, 'process.env.LOG_DIR || process.cwd()/logs');
const logLevel = new commander.Option('-l, --logLevel <level>', 'Log level')
.default(process.env.LOG_LEVEL || 'info', 'process.env.LOG_LEVEL || info');
const wikiConfig = new commander.Option('-w, --wikiConfig <path>', 'Relative url to contextbot wiki page EX https://reddit.com/r/subreddit/wiki/<path>')
.default(process.env.WIKI_CONFIG || 'botconfig/contextbot', "process.env.WIKI_CONFIG || 'botconfig/contextbot'");
const snooDebug = new commander.Option('--snooDebug', 'Set Snoowrap to debug')
.argParser(parseBool)
.default(process.env.SNOO_DEBUG || false, 'process.env.SNOO_DEBUG || false');
const authorTTL = new commander.Option('--authorTTL <ms>', 'Set the TTL (ms) for the Author Activities shared cache')
.argParser(argParseInt)
.default(process.env.AUTHOR_TTL || 10000, 'process.env.AUTHOR_TTL || 10000');
const heartbeat = new commander.Option('--heartbeat <s>', 'Interval, in seconds, between heartbeat logs. Set to 0 to disable')
.argParser(argParseInt)
//heartbeat.defaultValueDescription = 'process.env.HEARTBEAT || 300';
.default(process.env.HEARTBEAT || 300, 'process.env.HEARTBEAT || 300');
const apiRemaining = new commander.Option('--apiLimitWarning <remaining>', 'When API limit remaining (600/10min) is lower than this value log statements for limit will be raised to WARN level')
.argParser(argParseInt)
.default(process.env.API_REMAINING || 250, 'process.env.API_REMAINING || 250');
const dryRun = new commander.Option('--dryRun', 'Set dryRun=true for all checks/actions on all subreddits (overrides any existing)')
.argParser(parseBool)
.default(process.env.DRYRUN || false, 'process.env.DRYRUN || false');
const disableCache = new commander.Option('--disableCache', 'Disable caching for all subreddits')
.argParser(parseBool)
.default(process.env.DISABLE_CACHE || false, 'process.env.DISABLE_CACHE || false');
options.push(dryRun);
options = [

View File

@@ -6,7 +6,7 @@ import dduration from 'dayjs/plugin/duration.js';
import relTime from 'dayjs/plugin/relativeTime.js';
import {Manager} from "./Subreddit/Manager";
import {Command} from 'commander';
import {getOptions} from "./Utils/CommandConfig";
import {checks, getUniversalOptions, limit} from "./Utils/CommandConfig";
import {App} from "./App";
import Submission from "snoowrap/dist/objects/Submission";
import {COMMENT_URL_ID, parseLinkIdentifier, SUBMISSION_URL_ID} from "./util";
@@ -19,7 +19,7 @@ const commentReg = parseLinkIdentifier([COMMENT_URL_ID]);
const submissionReg = parseLinkIdentifier([SUBMISSION_URL_ID]);
const program = new Command();
for (const o of getOptions()) {
for (const o of getUniversalOptions()) {
program.addOption(o);
}
@@ -41,7 +41,7 @@ for (const o of getOptions()) {
activityIdentifier: 'Either a permalink URL or the ID of the Comment or Submission',
type: `If activityIdentifier is not a permalink URL then the type of activity ('comment' or 'submission'). May also specify 'submission' type when using a permalink to a comment to get the Submission`,
})
.option('-h, --checks <checkNames...>', 'An optional list of Checks, by name, that should be run. If none are specified all Checks for the Subreddit the Activity is in will be run')
.addOption(checks)
.action(async (activityIdentifier, type, commandOptions = {}) => {
const {checks = []} = commandOptions;
const app = new App(program.opts());
@@ -93,6 +93,26 @@ for (const o of getOptions()) {
}
});
program.command('unmoderated <subreddits...>')
.description('Run checks on all unmoderated activity in the modqueue', {
subreddits: 'The list of subreddits to run on. If not specified will run on all subreddits the account has moderation access to.'
})
.addOption(checks)
.addOption(limit)
.action(async (subreddits = [], commandOptions = {}) => {
const {checks = [], limit = 100} = commandOptions;
const app = new App(program.opts());
await app.buildManagers(subreddits);
for(const manager of app.subManagers) {
const activities = await manager.subreddit.getUnmoderated({limit});
for(const a of activities.reverse()) {
await manager.runChecks(a instanceof Submission ? 'Submission' : 'Comment', a, checks);
}
}
});
await program.parseAsync();