Keep every SQL statement out of the command layer so each feature can be built and tested without a Discord dependency.
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
import { getDatabase } from '../index.js';
|
|
|
|
export function recordAction({
|
|
guildId,
|
|
action,
|
|
moderatorId,
|
|
targetId = null,
|
|
reason = null,
|
|
durationMs = null,
|
|
}) {
|
|
getDatabase()
|
|
.prepare(
|
|
`INSERT INTO moderation_actions (guild_id, action, target_id, moderator_id, reason, duration_ms)
|
|
VALUES (?, ?, ?, ?, ?, ?)`,
|
|
)
|
|
.run(guildId, action, targetId, moderatorId, reason, durationMs);
|
|
}
|
|
|
|
export function listActions({ guildId, targetId = null, action = null, limit = 20 }) {
|
|
const clauses = ['guild_id = ?'];
|
|
const params = [guildId];
|
|
if (targetId) {
|
|
clauses.push('target_id = ?');
|
|
params.push(targetId);
|
|
}
|
|
if (action) {
|
|
clauses.push('action = ?');
|
|
params.push(action);
|
|
}
|
|
params.push(limit);
|
|
|
|
return getDatabase()
|
|
.prepare(`SELECT * FROM moderation_actions WHERE ${clauses.join(' AND ')} ORDER BY id DESC LIMIT ?`)
|
|
.all(...params);
|
|
}
|
|
|
|
export function countActions({ guildId, targetId, action }) {
|
|
const row = getDatabase()
|
|
.prepare(
|
|
'SELECT COUNT(*) AS count FROM moderation_actions WHERE guild_id = ? AND target_id = ? AND action = ?',
|
|
)
|
|
.get(guildId, targetId, action);
|
|
return row.count;
|
|
}
|
|
|
|
export function countByAction(guildId) {
|
|
return getDatabase()
|
|
.prepare('SELECT action, COUNT(*) AS count FROM moderation_actions WHERE guild_id = ? GROUP BY action')
|
|
.all(guildId);
|
|
}
|