Skip to content

Commit ba98b1b

Browse files
authored
Fix: just ask detection (#48)
* 🌟 feat: add Regular role env variables * 🧹 chore: run biome check (lint and format) * πŸ› fix: reduce false positives in justask auto responder
1 parent 9a9d92c commit ba98b1b

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

β€Ž.env.productionβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ADVENT_OF_CODE_CHANNEL_ID=1047623689488830495
1313
# Role IDs (from your dev server)
1414
REPEL_ROLE_ID=1002411741776461844
1515
MODERATORS_ROLE_IDS=849481536654803004
16+
REGULAR_ROLE_ID=1383170370634514544
1617

1718
# Other
1819
GUIDES_TRACKER_PATH=/app/data/guides-tracker.json

β€Žsrc/commands/docs/baseline.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
} from 'discord.js';
1010
import { features as data } from 'web-features';
1111
import { fuzzySearch } from '../../util/fuzzy-search.js';
12+
import { clampText } from '../../util/text.js';
1213
import type { ProviderConfig } from './types.js';
1314
import { createBaseConfig, getBaselineFeatures } from './utils.js';
14-
import { clampText } from '../../util/text.js';
1515

1616
export type FeatureData = {
1717
name: string;

β€Žsrc/env.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const config = {
3030
? requireEnv('MODERATORS_ROLE_IDS').split(',')
3131
: [],
3232
repel: requireEnv('REPEL_ROLE_ID'),
33+
regular: requireEnv('REGULAR_ROLE_ID'),
3334
a: optionalEnv('ROLE_A_ID'),
3435
b: optionalEnv('ROLE_B_ID'),
3536
c: optionalEnv('ROLE_C_ID'),

β€Žsrc/events/just-ask.tsβ€Ž

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Events } from 'discord.js';
1+
import { Events, type Message, PermissionFlagsBits } from 'discord.js';
22
import { MINUTE } from '../constants/time.js';
3+
import { config } from '../env.js';
34
import { createEvent } from '../util/events.js';
45
import { loadMarkdownOptions } from '../util/markdown.js';
56
import { rateLimit } from '../util/rate-limit.js';
@@ -20,7 +21,7 @@ const reQuestion = `(?:experience|knowledge|info(?:rmation)?|ideas?|clues?|tips?
2021
const reConnector = `(?:with|about|on|regarding|for|of)`;
2122

2223
const askToAskPattern = new RegExp(
23-
String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\??`,
24+
String.raw`\b${reSubject}\s+${reVerb}\s+(?:${reQuantifier}\s+)?(?:${reQuestion}\s+)?(?:${reConnector}\s+)?.+\?`,
2425
'i'
2526
);
2627

@@ -33,15 +34,34 @@ const [response] = await loadMarkdownOptions<{ name: string }>(
3334

3435
const { canRun, reset } = rateLimit(10 * MINUTE);
3536

37+
const shouldCheck = (message: Message) => {
38+
// check rate limit
39+
if (!canRun()) {
40+
return false;
41+
}
42+
// check author
43+
if (message.author.bot || message.author.system) {
44+
return false;
45+
}
46+
// check roles/permissions
47+
if (
48+
message.member !== null &&
49+
(message.member.roles.highest.comparePositionTo(config.roleIds.repel) >= 0 ||
50+
message.member.permissions.has(PermissionFlagsBits.ModerateMembers))
51+
) {
52+
return false;
53+
}
54+
return true;
55+
};
56+
3657
export const justAskEvent = createEvent(
3758
{
3859
name: Events.MessageCreate,
3960
},
4061
async (message) => {
41-
if (!canRun() || message.author.bot) {
62+
if (!shouldCheck(message)) {
4263
return;
4364
}
44-
4565
// Ignore long messages, likely user provided more context
4666
if (message.content.split(' ').length > 10) {
4767
return;

β€Žsrc/util/commands.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ChatInputCommandInteraction, Client } from 'discord.js';
1+
import type { ChatInputCommandInteraction } from 'discord.js';
22
import type { Command } from '../commands/types.js';
33

44
export const createCommand = (command: Command): Command => {

0 commit comments

Comments
Β (0)