Skip to content

Commit 66f9575

Browse files
committed
fix(reload): adapt to new docs command approach
* original idea of workflow action is not feasible (in-process version cache required by docs cmd defaulting) * command needs to reload versions and re-deploy global commands (docs cmd rebuilt based on newly fetched versions) * move command building process into a function to make it available externally * abstract the list of global commands to be available externally * re-deploy commands and refresh version cache when command is used resolves #232
1 parent 91d619b commit 66f9575

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/deployFunctions/deployGlobal.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import { TagCommand } from '../interactions/tag.js';
99
import { TestTagCommand } from '../interactions/testtag.js';
1010
import { deploy } from './deploy.js';
1111

12-
void deploy([
12+
export const staticGlobalCommands = [
1313
DiscordDocsCommand,
14-
DocsCommand,
1514
GuideCommand,
1615
MdnCommand,
1716
NodeCommand,
1817
TagCommand,
1918
TestTagCommand,
2019
DTypesCommand,
21-
]);
20+
];
21+
22+
void deploy([...staticGlobalCommands, DocsCommand]);

src/handling/handleApplicationCommand.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { Collection } from '@discordjs/collection';
44
import type { APIApplicationCommandInteraction } from 'discord-api-types/v10';
55
import { ApplicationCommandType } from 'discord-api-types/v10';
66
import type { Response } from 'polka';
7+
import { deploy } from '../deployFunctions/deploy.js';
8+
import { staticGlobalCommands } from '../deployFunctions/deployGlobal.js';
79
import { algoliaResponse } from '../functions/algoliaResponse.js';
810
import { resolveOptionsToDocsAutoComplete } from '../functions/autocomplete/docsAutoComplete.js';
911
import { djsDocs } from '../functions/docs.js';
@@ -14,6 +16,7 @@ import { showTag, reloadTags } from '../functions/tag.js';
1416
import { testTag } from '../functions/testtag.js';
1517
import type { DiscordDocsCommand } from '../interactions/discorddocs.js';
1618
import type { DTypesCommand } from '../interactions/discordtypes.js';
19+
import { buildDocsCommand, DocsCommand } from '../interactions/docs.js';
1720
import type { GuideCommand } from '../interactions/guide.js';
1821
import type { MdnCommand } from '../interactions/mdn.js';
1922
import type { NodeCommand } from '../interactions/node.js';
@@ -143,8 +146,13 @@ export async function handleApplicationCommand(
143146
}
144147

145148
case 'reloadversions': {
146-
await reloadDjsVersions();
147-
prepareResponse(res, `Reloaded versions for all ${inlineCode('@discordjs')} packages.`, { ephemeral: true });
149+
const versions = await reloadDjsVersions();
150+
const updatedDocsCommand = await buildDocsCommand(versions);
151+
await deploy([...staticGlobalCommands, updatedDocsCommand]);
152+
153+
prepareResponse(res, `Reloaded versions for all supported packages (dependency of discord.js).`, {
154+
ephemeral: true,
155+
});
148156
break;
149157
}
150158
}

src/interactions/docs.ts

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
import { ApplicationCommandOptionType } from 'discord-api-types/v10';
2-
import { reloadDjsVersions } from '../util/djsdocs.js';
2+
import { type DjsVersions, reloadDjsVersions } from '../util/djsdocs.js';
33

44
const versions = await reloadDjsVersions();
5-
if (!versions.packages.length) {
6-
throw new Error('Error while loading versions');
5+
export async function buildDocsCommand(versions: DjsVersions) {
6+
return {
7+
name: 'docs',
8+
description: 'Display discord.js documentation',
9+
options: [
10+
{
11+
type: ApplicationCommandOptionType.String,
12+
name: 'query',
13+
description: 'Phrase to search for',
14+
required: true,
15+
autocomplete: true,
16+
},
17+
{
18+
type: ApplicationCommandOptionType.Boolean,
19+
name: 'hide',
20+
description: 'Hide command output (default: False)',
21+
required: false,
22+
},
23+
{
24+
type: ApplicationCommandOptionType.String,
25+
name: 'version',
26+
description: 'Version of discord.js to use (default: Latest release)',
27+
choices: versions.versions
28+
.get('discord.js')
29+
?.slice(0, 25)
30+
.map((version) => {
31+
return {
32+
name: version,
33+
value: version,
34+
};
35+
}) ?? [
36+
{
37+
name: 'main',
38+
value: 'main',
39+
},
40+
],
41+
required: false,
42+
},
43+
{
44+
type: ApplicationCommandOptionType.User,
45+
name: 'mention',
46+
description: 'User to mention',
47+
required: false,
48+
},
49+
],
50+
} as const;
751
}
852

9-
export const DocsCommand = {
10-
name: 'docs',
11-
description: 'Display discord.js documentation',
12-
options: [
13-
{
14-
type: ApplicationCommandOptionType.String,
15-
name: 'query',
16-
description: 'Phrase to search for',
17-
required: true,
18-
autocomplete: true,
19-
},
20-
{
21-
type: ApplicationCommandOptionType.Boolean,
22-
name: 'hide',
23-
description: 'Hide command output (default: False)',
24-
required: false,
25-
},
26-
{
27-
type: ApplicationCommandOptionType.String,
28-
name: 'version',
29-
description: 'Version of discord.js to use (default: Latest release)',
30-
choices: versions.versions
31-
.get('discord.js')
32-
?.slice(0, 25)
33-
.map((version) => {
34-
return {
35-
name: version,
36-
value: version,
37-
};
38-
}) ?? [
39-
{
40-
name: 'main',
41-
value: 'main',
42-
},
43-
],
44-
required: false,
45-
},
46-
{
47-
type: ApplicationCommandOptionType.User,
48-
name: 'mention',
49-
description: 'User to mention',
50-
required: false,
51-
},
52-
],
53-
} as const;
53+
export const DocsCommand = await buildDocsCommand(versions);

0 commit comments

Comments
 (0)