Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
akpi816218 committed Nov 18, 2024
2 parents 16f7386 + d34bbfb commit 1368ea2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
12 changes: 12 additions & 0 deletions src/commands/recache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { refreshCachedLangSpec } from '@/lib/kumilinwa/refreshCache';
import { ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js';

export const data = new SlashCommandBuilder()
.setName('recache')
.setDescription('Refresh the cached language specification');

export async function execute(interaction: ChatInputCommandInteraction) {
await interaction.deferReply();
await refreshCachedLangSpec();
await interaction.editReply('Refreshed the language specification cache!');
}
26 changes: 4 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@ import {
import { argv, stdout } from 'process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
import { readdir, rm, writeFile } from 'fs/promises';
import { readdir, rm } from 'fs/promises';
import { Jsoning } from 'jsoning';
import { DevIds, LangSpecURL, permissionsBits, PORT } from './config';
import {
Command,
CommandClient,
createServer,
FullEntry,
logger,
Methods
} from './lib';
import { DevIds, permissionsBits, PORT } from './config';
import { Command, CommandClient, createServer, logger, Methods } from './lib';
import { scheduleJob } from 'node-schedule';
import { refreshCachedLangSpec } from '@/lib/kumilinwa/refreshCache';

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

Expand Down Expand Up @@ -252,15 +246,3 @@ async function sendError(e: Error) {
});
}
}

async function refreshCachedLangSpec() {
const data = (await fetch(LangSpecURL)
.then(res => res.json())
.catch(e => sendError)) as FullEntry[];
await writeFile(
join(Dirname, 'lib', 'kumilinwa', 'langspec.cache.json'),
JSON.stringify(data)
);
await StatsDB.set('langSpecCacheAge', Date.now());
logger.debug('Cached language specification.');
}
18 changes: 18 additions & 0 deletions src/lib/kumilinwa/refreshCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { LangSpecURL } from '@/config';
import Jsoning from 'jsoning';
import { logger } from '../misc/logger';
import { writeFile } from 'fs/promises';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

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

export async function refreshCachedLangSpec() {
const data = await fetch(LangSpecURL).then(res => res.json());
await writeFile(join(Dirname, 'langspec.cache.json'), JSON.stringify(data));
await new Jsoning(join('..', '..', '..', 'stats.tmp.db.json')).set(
'langSpecCacheAge',
Date.now()
);
logger.debug('Cached language specification.');
}

0 comments on commit 1368ea2

Please sign in to comment.