/[sudobot]/trunk/src/utils/registry.ts
ViewVC logotype

Annotation of /trunk/src/utils/registry.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 77 - (hide annotations)
Mon Jul 29 17:28:30 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2713 byte(s)
Added command line system
1 rakin 51 import path from 'path';
2     import { promises as fs } from 'fs';
3     import DiscordClient from '../client/Client';
4 rakin 58 import { registered, registrationStart } from './debug';
5 rakin 51
6 rakin 77 export async function registerCLICommands(client: DiscordClient, dir: string = '') {
7     const filePath = path.join(__dirname, dir);
8     const files = await fs.readdir(filePath);
9    
10     for (const file of files) {
11     const stat = await fs.lstat(path.join(filePath, file));
12    
13     if (stat.isDirectory())
14     await registerCLICommands(client, path.join(dir, file));
15    
16     if (file.endsWith('.js') || file.endsWith('.ts')) {
17     const { default: Command } = await import(path.join(dir, file));
18     const command = new Command();
19    
20     client.cliCommands.set(command.getName(), command);
21    
22     command.getAliases().forEach((alias: string) => {
23     client.cliCommands.set(alias, command);
24     });
25     }
26     }
27     }
28    
29 rakin 51 export async function registerCommands(client: DiscordClient, dir: string = '') {
30     const filePath = path.join(__dirname, dir);
31     const files = await fs.readdir(filePath);
32    
33     for (const file of files) {
34     const stat = await fs.lstat(path.join(filePath, file));
35    
36     if (stat.isDirectory())
37 rakin 58 await registerCommands(client, path.join(dir, file));
38 rakin 51
39     if (file.endsWith('.js') || file.endsWith('.ts')) {
40 rakin 58 const startTime = Date.now();
41 rakin 51 const { default: Command } = await import(path.join(dir, file));
42     const command = new Command();
43    
44     client.commands.set(command.getName(), command);
45 rakin 58
46 rakin 51 command.getAliases().forEach((alias: string) => {
47     client.commands.set(alias, command);
48     });
49 rakin 58
50     const endTime = Date.now();
51    
52     registered(command, startTime, endTime);
53 rakin 51 }
54     }
55     }
56    
57     export async function registerEvents(client: DiscordClient, dir: string = '') {
58     const filePath = path.join(__dirname, dir);
59     const files = await fs.readdir(filePath);
60    
61     for (const file of files) {
62     const stat = await fs.lstat(path.join(filePath, file));
63    
64     if (stat.isDirectory())
65 rakin 58 await registerEvents(client, path.join(dir, file));
66 rakin 51
67     if (file.endsWith('.js') || file.endsWith('.ts')) {
68 rakin 58 const startTime = Date.now();
69 rakin 51 const { default: Event } = await import(path.join(dir, file));
70     const event = new Event();
71     client.events.set(event.getName(), event);
72     client.on(event.getName(), event.run.bind(event, client));
73 rakin 58 const endTime = Date.now();
74     registered(event, startTime, endTime);
75 rakin 51 }
76     }
77     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26