Skip to content

Commit

Permalink
feat(*): re-implement mentioning users with suggestions
Browse files Browse the repository at this point in the history
resolves #210
  • Loading branch information
almostSouji committed Jul 25, 2024
1 parent e5ec9a1 commit c16949c
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 34 deletions.
6 changes: 5 additions & 1 deletion src/functions/algoliaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function algoliaResponse(
algoliaObjectId: string,
emojiId: string,
emojiName: string,
user?: string,
ephemeral?: boolean,
): Promise<Response> {
const full = `http://${algoliaAppId}.${API_BASE_ALGOLIA}/1/indexes/${algoliaIndex}/${encodeURIComponent(
Expand Down Expand Up @@ -48,7 +49,10 @@ export async function algoliaResponse(
`${hyperlink('read more', hideLinkEmbed(hit.url))}`,
].filter(Boolean) as string[];

prepareResponse(res, contentParts.join('\n'), ephemeral ?? false);
prepareResponse(res, contentParts.join('\n'), {
ephemeral,
suggestion: user ? { userId: user, kind: 'documentation' } : undefined,
});
} catch {
prepareErrorResponse(res, 'Invalid result. Make sure to select an entry from the autocomplete.');
}
Expand Down
6 changes: 6 additions & 0 deletions src/functions/autocomplete/docsAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export async function djsAutoComplete(

type DocsAutoCompleteData = {
ephemeral?: boolean;
mention?: string;
query: string;
source: string;
version: string;
Expand All @@ -107,6 +108,7 @@ export function resolveOptionsToDocsAutoComplete(
let query = 'Client';
let version = versions?.at(1) ?? 'main';
let ephemeral;
let mention;

logger.debug(
{
Expand All @@ -115,6 +117,7 @@ export function resolveOptionsToDocsAutoComplete(
versions,
version,
ephemeral,
mention,
source,
},
},
Expand All @@ -132,6 +135,8 @@ export function resolveOptionsToDocsAutoComplete(
}
} else if (opt.type === ApplicationCommandOptionType.Boolean && opt.name === 'hide') {
ephemeral = opt.value;
} else if (opt.type === ApplicationCommandOptionType.User && opt.name === 'mention') {
mention = opt.value;
}
}

Expand All @@ -140,5 +145,6 @@ export function resolveOptionsToDocsAutoComplete(
source,
ephemeral,
version,
mention,
};
}
19 changes: 11 additions & 8 deletions src/functions/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,14 @@ async function resolveDjsDocsQuery(query: string, source: string, branch: string
}
}

export async function djsDocs(res: Response, branch: string, _query: string, source: string, ephemeral = false) {
export async function djsDocs(
res: Response,
branch: string,
_query: string,
source: string,
user?: string,
ephemeral?: boolean,
) {
try {
const query = await resolveDjsDocsQuery(_query, source, branch);
if (!query) {
Expand All @@ -309,14 +316,10 @@ export async function djsDocs(res: Response, branch: string, _query: string, sou
return res.end();
}

prepareResponse(
res,
truncate(formatItem(item, _package, branch, member), MAX_MESSAGE_LENGTH),
prepareResponse(res, truncate(formatItem(item, _package, branch, member), MAX_MESSAGE_LENGTH), {
ephemeral,
[],
[],
InteractionResponseType.ChannelMessageWithSource,
);
suggestion: user ? { userId: user, kind: 'documentation' } : undefined,
});
return res.end();
} catch (_error) {
const error = _error as Error;
Expand Down
7 changes: 5 additions & 2 deletions src/functions/mdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function escape(text: string) {
return text.replaceAll('||', '|\u200B|').replaceAll('*', '\\*');
}

export async function mdnSearch(res: Response, query: string, ephemeral?: boolean): Promise<Response> {
export async function mdnSearch(res: Response, query: string, user?: string, ephemeral?: boolean): Promise<Response> {
const trimmedQuery = query.trim();
try {
const qString = `${API_BASE_MDN}/${trimmedQuery}/index.json`;
Expand Down Expand Up @@ -41,7 +41,10 @@ export async function mdnSearch(res: Response, query: string, ephemeral?: boolea
intro,
];

prepareResponse(res, parts.join('\n'), ephemeral ?? false);
prepareResponse(res, parts.join('\n'), {
ephemeral,
suggestion: user ? { userId: user, kind: 'documentation' } : undefined,
});

return res;
} catch (error) {
Expand Down
12 changes: 8 additions & 4 deletions src/functions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ function docsUrl(version: string, source: string, anchorTextRaw: string) {
const jsonCache: Map<string, NodeDocs> = new Map();
const docsCache: Map<string, string> = new Map();

export async function nodeAutoCompleteResolve(res: Response, query: string, ephemeral?: boolean) {
export async function nodeAutoCompleteResolve(res: Response, query: string, user?: string, ephemeral?: boolean) {
const url = urlOption(`${API_BASE_NODE}/${query}`);

if (!url || !query.startsWith('docs')) {
return nodeSearch(res, query, undefined, ephemeral);
return nodeSearch(res, query, undefined, user, ephemeral);
}

const key = `${url.origin}${url.pathname}`;
Expand Down Expand Up @@ -112,7 +112,7 @@ export async function nodeAutoCompleteResolve(res: Response, query: string, ephe
`<:node:${EMOJI_ID_NODE}> ${hyperlink(inlineCode(headingCode.length ? headingCode : heading), url.toString())}`,
`${fullSentence ?? partSentence ?? `${truncate(text, 20, '')}..`}.`,
].join('\n'),
ephemeral ?? false,
{ ephemeral, suggestion: user ? { userId: user, kind: 'documentation' } : undefined },
);

return res;
Expand All @@ -122,6 +122,7 @@ export async function nodeSearch(
res: Response,
query: string,
version = 'latest-v20.x',
user?: string,
ephemeral?: boolean,
): Promise<Response> {
const trimmedQuery = query.trim();
Expand Down Expand Up @@ -161,7 +162,10 @@ export async function nodeSearch(
.replaceAll(boldCodeBlockRegex, bold(inlineCode('$1'))),
);

prepareResponse(res, parts.join('\n'), ephemeral ?? false);
prepareResponse(res, parts.join('\n'), {
ephemeral,
suggestion: user ? { userId: user, kind: 'documentation' } : undefined,
});

return res;
} catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions src/functions/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function reloadTags(res: Response, tagCache: Collection<string, Tag
`${PREFIX_SUCCESS} **Tags have fully reloaded ${remote ? '(remote)' : '(local)'}!**`,
`Tag cache size has changed from ${prev} to ${tagCache.size}.`,
].join('\n'),
true,
{ ephemeral: true },
);
} catch (error) {
logger.error(error as Error);
Expand All @@ -83,12 +83,14 @@ export function showTag(
res: Response,
query: string,
tagCache: Collection<string, Tag>,
user?: string,
ephemeral?: boolean,
): Response {
const trimmedQuery = query.trim().toLowerCase();
const content = findTag(tagCache, trimmedQuery);

if (content) {
prepareResponse(res, content, ephemeral ?? false);
prepareResponse(res, content, { ephemeral, suggestion: user ? { userId: user, kind: 'tag' } : undefined });
} else {
prepareErrorResponse(res, `Could not find a tag with name or alias similar to \`${trimmedQuery}\`.`);
}
Expand Down
15 changes: 9 additions & 6 deletions src/handling/handleApplicationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ export async function handleApplicationCommand(
break;
}

const { query, version, ephemeral, source } = resolved;
await djsDocs(res, version, query, source, ephemeral);
const { query, version, ephemeral, source, mention } = resolved;
await djsDocs(res, version, query, source, mention, ephemeral);
break;
}

Expand All @@ -72,6 +72,7 @@ export async function handleApplicationCommand(
castArgs.query,
EMOJI_ID_CLYDE_BLURPLE,
'discord',
castArgs.mention,
castArgs.hide,
);
break;
Expand All @@ -88,6 +89,7 @@ export async function handleApplicationCommand(
castArgs.query,
EMOJI_ID_DTYPES,
'dtypes',
castArgs.mention,
castArgs.hide,
);

Expand All @@ -104,26 +106,27 @@ export async function handleApplicationCommand(
castArgs.query,
EMOJI_ID_GUIDE,
'guide',
castArgs.mention,
castArgs.hide,
);
break;
}

case 'mdn': {
const castArgs = args as ArgumentsOf<typeof MdnCommand>;
await mdnSearch(res, castArgs.query, castArgs.hide);
await mdnSearch(res, castArgs.query, castArgs.mention, castArgs.hide);
break;
}

case 'node': {
const castArgs = args as ArgumentsOf<typeof NodeCommand>;
await nodeAutoCompleteResolve(res, castArgs.query, castArgs.hide);
await nodeAutoCompleteResolve(res, castArgs.query, castArgs.mention, castArgs.hide);
break;
}

case 'tag': {
const castArgs = args as ArgumentsOf<typeof TagCommand>;
showTag(res, castArgs.query, tagCache, castArgs.hide);
showTag(res, castArgs.query, tagCache, castArgs.mention, castArgs.hide);
break;
}

Expand All @@ -141,7 +144,7 @@ export async function handleApplicationCommand(

case 'reloadversions': {
await reloadDjsVersions();
prepareResponse(res, `Reloaded versions for all ${inlineCode('@discordjs')} packages.`, true);
prepareResponse(res, `Reloaded versions for all ${inlineCode('@discordjs')} packages.`, { ephemeral: true });
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ export async function start() {
break;

default:
prepareResponse(res, `${PREFIX_TEAPOT} This shouldn't be here...`, true);
prepareResponse(res, `${PREFIX_TEAPOT} This shouldn't be here...`, { ephemeral: true });
}
} catch (error) {
logger.error(error as Error);
prepareResponse(res, `${PREFIX_BUG} Looks like something went wrong here, please try again later!`, true);
prepareResponse(res, `${PREFIX_BUG} Looks like something went wrong here, please try again later!`, {
ephemeral: true,
});
}

res.end();
Expand Down
6 changes: 6 additions & 0 deletions src/interactions/discorddocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ export const DiscordDocsCommand = {
description: 'Hide command output',
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
6 changes: 6 additions & 0 deletions src/interactions/discordtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ export const DTypesCommand = {
description: EPHEMERAL_DESCRIPTION,
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
7 changes: 7 additions & 0 deletions src/interactions/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ function buildSubCommandOptions(name: string) {
value: 'main',
},
],
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
] as const;
}
Expand Down
6 changes: 6 additions & 0 deletions src/interactions/guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ export const GuideCommand = {
description: 'Hide command output',
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
6 changes: 6 additions & 0 deletions src/interactions/mdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ export const MdnCommand = {
description: 'Hide command output',
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
6 changes: 6 additions & 0 deletions src/interactions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ export const NodeCommand = {
description: 'Hide command output',
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
6 changes: 6 additions & 0 deletions src/interactions/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@ export const TagCommand = {
description: 'Hide command output',
required: false,
},
{
type: ApplicationCommandOptionType.User,
name: 'mention',
description: 'User to mention',
required: false,
},
],
} as const;
Loading

0 comments on commit c16949c

Please sign in to comment.