Skip to content

Commit 1368ea2

Browse files
author
Akhil Pillai
committed
2 parents 16f7386 + d34bbfb commit 1368ea2

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

src/commands/recache.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { refreshCachedLangSpec } from '@/lib/kumilinwa/refreshCache';
2+
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';
3+
4+
export const data = new SlashCommandBuilder()
5+
.setName('recache')
6+
.setDescription('Refresh the cached language specification');
7+
8+
export async function execute(interaction: ChatInputCommandInteraction) {
9+
await interaction.deferReply();
10+
await refreshCachedLangSpec();
11+
await interaction.editReply('Refreshed the language specification cache!');
12+
}

src/index.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@ import {
1313
import { argv, stdout } from 'process';
1414
import { dirname, join } from 'path';
1515
import { fileURLToPath } from 'url';
16-
import { readdir, rm, writeFile } from 'fs/promises';
16+
import { readdir, rm } from 'fs/promises';
1717
import { Jsoning } from 'jsoning';
18-
import { DevIds, LangSpecURL, permissionsBits, PORT } from './config';
19-
import {
20-
Command,
21-
CommandClient,
22-
createServer,
23-
FullEntry,
24-
logger,
25-
Methods
26-
} from './lib';
18+
import { DevIds, permissionsBits, PORT } from './config';
19+
import { Command, CommandClient, createServer, logger, Methods } from './lib';
2720
import { scheduleJob } from 'node-schedule';
21+
import { refreshCachedLangSpec } from '@/lib/kumilinwa/refreshCache';
2822

2923
const Dirname = dirname(fileURLToPath(import.meta.url));
3024

@@ -252,15 +246,3 @@ async function sendError(e: Error) {
252246
});
253247
}
254248
}
255-
256-
async function refreshCachedLangSpec() {
257-
const data = (await fetch(LangSpecURL)
258-
.then(res => res.json())
259-
.catch(e => sendError)) as FullEntry[];
260-
await writeFile(
261-
join(Dirname, 'lib', 'kumilinwa', 'langspec.cache.json'),
262-
JSON.stringify(data)
263-
);
264-
await StatsDB.set('langSpecCacheAge', Date.now());
265-
logger.debug('Cached language specification.');
266-
}

src/lib/kumilinwa/refreshCache.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { LangSpecURL } from '@/config';
2+
import Jsoning from 'jsoning';
3+
import { logger } from '../misc/logger';
4+
import { writeFile } from 'fs/promises';
5+
import { dirname, join } from 'path';
6+
import { fileURLToPath } from 'url';
7+
8+
const Dirname = join(dirname(fileURLToPath(import.meta.url)));
9+
10+
export async function refreshCachedLangSpec() {
11+
const data = await fetch(LangSpecURL).then(res => res.json());
12+
await writeFile(join(Dirname, 'langspec.cache.json'), JSON.stringify(data));
13+
await new Jsoning(join('..', '..', '..', 'stats.tmp.db.json')).set(
14+
'langSpecCacheAge',
15+
Date.now()
16+
);
17+
logger.debug('Cached language specification.');
18+
}

0 commit comments

Comments
 (0)