diff --git a/.env.production b/.env.production index 1d93141..232b9e7 100644 --- a/.env.production +++ b/.env.production @@ -13,6 +13,7 @@ ADVENT_OF_CODE_CHANNEL_ID=1047623689488830495 # Role IDs (from your dev server) REPEL_ROLE_ID=1002411741776461844 MODERATORS_ROLE_IDS=849481536654803004 +REGULAR_ROLE_ID=1383170370634514544 # Other GUIDES_TRACKER_PATH=/app/data/guides-tracker.json diff --git a/src/commands/docs/baseline.ts b/src/commands/docs/baseline.ts index f1dc848..c9022f5 100644 --- a/src/commands/docs/baseline.ts +++ b/src/commands/docs/baseline.ts @@ -9,9 +9,9 @@ import { } from 'discord.js'; import { features as data } from 'web-features'; import { fuzzySearch } from '../../util/fuzzy-search.js'; +import { clampText } from '../../util/text.js'; import type { ProviderConfig } from './types.js'; import { createBaseConfig, getBaselineFeatures } from './utils.js'; -import { clampText } from '../../util/text.js'; export type FeatureData = { name: string; diff --git a/src/env.ts b/src/env.ts index 2cdabeb..670c821 100644 --- a/src/env.ts +++ b/src/env.ts @@ -30,6 +30,7 @@ export const config = { ? requireEnv('MODERATORS_ROLE_IDS').split(',') : [], repel: requireEnv('REPEL_ROLE_ID'), + regular: requireEnv('REGULAR_ROLE_ID'), a: optionalEnv('ROLE_A_ID'), b: optionalEnv('ROLE_B_ID'), c: optionalEnv('ROLE_C_ID'), diff --git a/src/events/just-ask.ts b/src/events/just-ask.ts index 331859d..f3506f1 100644 --- a/src/events/just-ask.ts +++ b/src/events/just-ask.ts @@ -1,5 +1,6 @@ -import { Events } from 'discord.js'; +import { Events, type Message, PermissionFlagsBits } from 'discord.js'; import { MINUTE } from '../constants/time.js'; +import { config } from '../env.js'; import { createEvent } from '../util/events.js'; import { loadMarkdownOptions } from '../util/markdown.js'; import { rateLimit } from '../util/rate-limit.js'; @@ -20,7 +21,7 @@ const reQuestion = `(?:experience|knowledge|info(?:rmation)?|ideas?|clues?|tips? const reConnector = `(?:with|about|on|regarding|for|of)`; const askToAskPattern = new RegExp( - String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\??`, + String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\?`, 'i' ); @@ -33,15 +34,34 @@ const [response] = await loadMarkdownOptions<{ name: string }>( const { canRun, reset } = rateLimit(10 * MINUTE); +const shouldCheck = (message: Message) => { + // check rate limit + if (!canRun()) { + return false; + } + // check author + if (message.author.bot || message.author.system) { + return false; + } + // check roles/permissions + if ( + message.member !== null && + (message.member.roles.highest.comparePositionTo(config.roleIds.repel) >= 0 || + message.member.permissions.has(PermissionFlagsBits.ModerateMembers)) + ) { + return false; + } + return true; +}; + export const justAskEvent = createEvent( { name: Events.MessageCreate, }, async (message) => { - if (!canRun() || message.author.bot) { + if (!shouldCheck(message)) { return; } - // Ignore long messages, likely user provided more context if (message.content.split(' ').length > 10) { return; diff --git a/src/util/commands.ts b/src/util/commands.ts index 7fa14de..bfc6da0 100644 --- a/src/util/commands.ts +++ b/src/util/commands.ts @@ -1,4 +1,4 @@ -import type { ChatInputCommandInteraction, Client } from 'discord.js'; +import type { ChatInputCommandInteraction } from 'discord.js'; import type { Command } from '../commands/types.js'; export const createCommand = (command: Command): Command => {