I'm integrating internationalization into a discord bot with the typesafe-i18n library, but when I use the fetchT function (made by me), it never resolves.
- The command:
import { Slash, execute } from 'sunar';
import { fetchT, getT, localizations } from '../utils/i18n';
const slash = new Slash({
name: getT().ping.name(),
description: getT().ping.description(),
nameLocalizations: localizations((t) => t.ping.name()),
descriptionLocalizations: localizations((t) => t.ping.description()),
});
execute(slash, async (interaction) => {
await interaction.deferReply();
const ping = interaction.client.ws.ping;
console.log('Fetching translations...');
const t = await fetchT(interaction);
console.log('Translations fetched:', t); // This console log is never executed
await interaction.editReply({ content: t.ping.reply({ ping }) });
});
export { slash };
- The
fetchTfunction:
export async function fetchT(target: FetchTTarget): Promise<TranslationFunctions> {
if (target instanceof BaseInteraction) {
console.log('target is instanceof BaseInteraction');
console.log('Fetching locale...');
const locale = await fetchLocale({
user: target.user,
guild: target.guild,
channel: target.channel,
interactionLocale: target.locale,
interactionGuildLocale: target.guildLocale,
});
console.log('Locale fetched:', locale);
console.log('Resolving locale...');
const resolved = resolveLocale(locale);
console.log('Locale resolved:', resolved);
return resolved;
}
console.log('THIS SHOULD NOT BE SHOWN');
if (target instanceof Message) {
const locale = await fetchLocale({
user: target.author,
guild: target.guild,
channel: target.channel,
});
return resolveLocale(locale);
}
if (target instanceof Guild) {
const locale = await fetchLocale({ guild: target });
return resolveLocale(locale);
}
if (target.isDMBased()) {
const locale = await fetchLocale({ channel: target });
return resolveLocale(locale);
}
const locale = await fetchLocale({ guild: target.guild, channel: target });
return L[isLocale(locale) ? locale : baseLocale];
}
- The logs:
Fetching translations...
target is instanceof BaseInteraction
Fetching locale...
Locale fetched: en-US
Resolving locale...
Locale resolved: [Function (anonymous)] {
ping: {
name: 'internationalization',
description: 'Show the bot latency.',
reply: 'The ping is {ping:number}ms.'
}
}
No errors are output in the console nor are any errors being ignored.
Review the entire code or try it on your own machine:
mmm... it's so weird