@@ -10,21 +10,25 @@ const { makeSureFolderExists } = require("stuffs");
1010const client = new Discord . Client ( config . clientOptions ) ;
1111const interactions = new Discord . Collection ( ) ;
1212const events = new Discord . Collection ( ) ;
13+ const locales = new Discord . Collection ( ) ;
1314let interactionFiles ;
1415let eventFiles ;
16+ let localeFiles ;
1517globalThis . Underline = {
1618 ...config . globalObjects ,
1719 config,
1820 client,
1921 interactions,
2022 events,
23+ locales,
2124 Interaction : require ( './types/Interaction' ) ,
2225 Event : require ( './types/Event' ) ,
2326 SlashCommand : require ( "./types/SlashCommand" ) ,
2427 MessageAction : require ( "./types/MessageAction" ) ,
2528 UserAction : require ( "./types/UserAction" ) ,
2629 SelectMenu : require ( "./types/SelectMenu" ) ,
2730 Button : require ( "./types/Button" ) ,
31+ Locale : require ( "./types/Locale" ) ,
2832}
2933
3034async function getEventFilePaths ( ) {
@@ -49,13 +53,43 @@ async function getInteractionFilePaths() {
4953 return VInteractionFiles ;
5054}
5155
56+ async function getLocaleFilePaths ( ) {
57+ let localesPath = path . resolve ( "./locales" ) ;
58+ await makeSureFolderExists ( localesPath ) ;
59+ let VLocaleFiles = await readdirRecursive ( localesPath ) ;
60+ VLocaleFiles = VLocaleFiles . filter ( i => {
61+ let state = path . basename ( i ) . startsWith ( "-" ) ;
62+ return ! state ;
63+ } ) ;
64+ return VLocaleFiles ;
65+ }
66+
5267/** @type {{name:string,listener:()=>any,base:any}[] } */
5368let eventListeners = [ ] ;
5469
5570async function load ( ) {
5671 let loadStart = Date . now ( ) ;
5772 console . debug ( `[HATA AYIKLAMA] Yüklemeye başlandı!` ) ;
5873
74+ localeFiles = await getLocaleFilePaths ( ) ;
75+ await chillout . forEach ( localeFiles , ( localeFile ) => {
76+ let start = Date . now ( ) ;
77+ let rltPath = path . relative ( __dirname , localeFile ) ;
78+ console . info ( `[BİLGİ] "${ rltPath } " konumundaki dil yükleniyor..` )
79+ /** @type {import("./types/Locale") } */
80+ let locale = require ( localeFile ) ;
81+
82+ if ( locale . _type != "locale" )
83+ return console . warn ( `[UYARI] "${ rltPath } " dil dosyası boş. Atlanıyor..` ) ;
84+
85+ if ( locales . has ( locale . locale ) )
86+ return console . warn ( `[UYARI] ${ locale . locale } dili zaten yüklenmiş. Atlanıyor..` ) ;
87+
88+ locales . set ( locale . locale , locale ) ;
89+ console . info ( `[BİLGİ] "${ locale . locale } " dili yüklendi. (${ Date . now ( ) - start } ms sürdü.)` ) ;
90+ } )
91+
92+
5993 interactionFiles = await getInteractionFilePaths ( ) ;
6094 await chillout . forEach ( interactionFiles , ( interactionFile ) => {
6195 let start = Date . now ( ) ;
@@ -248,7 +282,10 @@ async function unload() {
248282 el . base . off ( el . name , el . listener ) ;
249283 } )
250284
251- let pathsToUnload = [ ...interactionFiles , ...eventFiles ] ;
285+ console . info ( `[BILGI] Dil listesi temizleniyor..` ) ;
286+ Underline . locales . clear ( ) ;
287+
288+ let pathsToUnload = [ ...interactionFiles , ...eventFiles , ...localeFiles ] ;
252289
253290 await chillout . forEach ( pathsToUnload , async ( pathToUnload ) => {
254291 console . info ( `[BILGI] Modül "${ path . relative ( __dirname , pathToUnload ) } " önbellekten kaldırılıyor!` ) ;
@@ -259,7 +296,7 @@ async function unload() {
259296
260297 unloadStart = 0 ;
261298 pathsToUnload = 0 ;
262-
299+
263300}
264301
265302async function reload ( ) {
@@ -302,9 +339,15 @@ client.on("interactionCreate", async (interaction) => {
302339
303340 let other = { } ;
304341
305- let shouldRun1 = await config . onInteractionBeforeChecks ( uInter , interaction , other ) ;
342+ {
343+ let locale_id = ( interaction . user . locale || interaction . locale ) ?. split ( "-" ) [ 0 ] ;
344+ other . locale = ( Underline . locales . get ( locale_id ) || Underline . locales . get ( Underline . config . defaultLanguage ) ) . data ;
345+ }
306346
307- if ( ! shouldRun1 ) return ;
347+ {
348+ let shouldRun1 = await config . onInteractionBeforeChecks ( uInter , interaction , other ) ;
349+ if ( ! shouldRun1 ) return ;
350+ }
308351
309352 if ( uInter . disabled ) {
310353 config . userErrors . disabled ( interaction , uInter , other ) ;
@@ -389,10 +432,18 @@ client.on("interactionCreate", async (interaction) => {
389432 }
390433
391434 ( async ( ) => {
392- let shouldRun2 = await config . onInteraction ( uInter , interaction , other ) ;
393- if ( ! shouldRun2 ) return ;
435+
436+ {
437+ let shouldRun2 = await config . onInteraction ( uInter , interaction , other ) ;
438+ if ( ! shouldRun2 ) return ;
439+ }
440+
394441 try {
442+
395443 await uInter . onInteraction ( interaction , other ) ;
444+
445+ await config . onAfterInteraction ( uInter , interaction , other ) ;
446+
396447 } catch ( err ) {
397448 console . error ( `[HATA] "${ uInter . actionType == "CHAT_INPUT" ? `/${ uInter . name . join ( " " ) } ` : `${ uInter . name [ 0 ] } ` } " adlı interaksiyon çalıştırılırken bir hata ile karşılaşıldı!` )
398449 if ( err . message ) console . error ( `[HATA] ${ err . message } ` ) ;
0 commit comments