Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import { db } from '../src/db.js';
const [, , discordId, ...rest] = process.argv;if (!discordId) { console.error(`usage: npm run rule:add -- <owner_discord_id> [options] --did <did> which atproto identity (job) to attach the rule to; required when the account has more than one job --guild <id> limit to a specific guild --channel <id> limit to a specific channel --author <id|self> limit to messages from a specific author ('self' = owner) --mode <urls|content> what to extract (default: urls) --match-kind <kind> 'substring' or 'regex' to filter by message content --match-value <value> pattern for the content filter --dest <kind> destination kind (default: stdout) --config <json> destination config as JSON string --disabled create disabled
omit a filter to make it a wildcard.`); process.exit(1);}
const flags = new Map();for (let i = 0; i < rest.length; i++) { if (rest[i].startsWith('--')) flags.set(rest[i].slice(2), rest[i + 1] ?? true);}
const user = db.prepare(`SELECT id FROM users WHERE discord_id = ?`).get(discordId);if (!user) { console.error(`no user with discord_id=${discordId} — run user:add first`); process.exit(1);}
const didFlag = flags.get('did') || null;const jobs = db.prepare(`SELECT id, did FROM jobs WHERE user_id = ? ORDER BY id ASC`).all(user.id);if (jobs.length === 0) { console.error(`no jobs for discord_id=${discordId} — run user:add with --did, or sign in via the web UI`); process.exit(1);}let job;if (didFlag) { job = jobs.find(j => j.did === didFlag); if (!job) { console.error(`no job with did=${didFlag} for this user. existing jobs: ${jobs.map(j => j.did).join(', ')}`); process.exit(1); }} else if (jobs.length === 1) { job = jobs[0];} else { console.error(`account has ${jobs.length} jobs — pass --did <did>. options: ${jobs.map(j => j.did).join(', ')}`); process.exit(1);}
const row = { job_id: job.id, guild_id: flags.get('guild') || null, channel_id: flags.get('channel') || null, author_id: flags.get('author') || null, mode: flags.get('mode') || 'urls', match_kind: flags.get('match-kind') || null, match_value: flags.get('match-value') || null, destination_kind: flags.get('dest') || 'stdout', destination_config: flags.get('config') || null, enabled: flags.has('disabled') ? 0 : 1,};
const result = db.prepare(` INSERT INTO rules (job_id, guild_id, channel_id, author_id, mode, match_kind, match_value, destination_kind, destination_config, enabled) VALUES (@job_id, @guild_id, @channel_id, @author_id, @mode, @match_kind, @match_value, @destination_kind, @destination_config, @enabled)`).run(row);
console.log(`inserted rule id=${result.lastInsertRowid} (job=${job.id} did=${job.did})`, row);