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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 344 by rakin, Mon Jul 29 17:29:40 2024 UTC
# Line 1  Line 1 
1  import path from 'path';  import path from 'path';
2  import { promises as fs } from 'fs';  import { promises as fs } from 'fs';
3  import DiscordClient from '../client/Client';  import DiscordClient from '../client/Client';
4    import { registered } from './debug';
5    
6    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  export async function registerCommands(client: DiscordClient, dir: string = '') {  export async function registerCommands(client: DiscordClient, dir: string = '') {
30      const filePath = path.join(__dirname, dir);      const filePath = path.join(__dirname, dir);
# Line 10  export async function registerCommands(c Line 34  export async function registerCommands(c
34          const stat = await fs.lstat(path.join(filePath, file));          const stat = await fs.lstat(path.join(filePath, file));
35    
36          if (stat.isDirectory())          if (stat.isDirectory())
37              registerCommands(client, path.join(dir, file));              await registerCommands(client, path.join(dir, file));
38                    
39          if (file.endsWith('.js') || file.endsWith('.ts')) {          if (file.endsWith('.js') || file.endsWith('.ts')) {
40                const startTime = Date.now();
41              const { default: Command } = await import(path.join(dir, file));              const { default: Command } = await import(path.join(dir, file));
42              const command = new Command();              const command = new Command();
43    
44              client.commands.set(command.getName(), command);              client.commands.set(command.getName(), command);
45                
46              command.getAliases().forEach((alias: string) => {              command.getAliases().forEach((alias: string) => {
47                  client.commands.set(alias, command);                  client.commands.set(alias, command);
48              });              });
49    
50                const endTime = Date.now();
51    
52                registered(command, startTime, endTime);
53          }          }
54      }      }
55  }  }
# Line 33  export async function registerEvents(cli Line 62  export async function registerEvents(cli
62          const stat = await fs.lstat(path.join(filePath, file));          const stat = await fs.lstat(path.join(filePath, file));
63    
64          if (stat.isDirectory())          if (stat.isDirectory())
65              registerEvents(client, path.join(dir, file));              await registerEvents(client, path.join(dir, file));
66    
67          if (file.endsWith('.js') || file.endsWith('.ts')) {          if (file.endsWith('.js') || file.endsWith('.ts')) {
68                const startTime = Date.now();
69              const { default: Event } = await import(path.join(dir, file));              const { default: Event } = await import(path.join(dir, file));
70              const event = new Event();              const event = new Event();
71              client.events.set(event.getName(), event);              client.events.set(event.getName(), event);
72              client.on(event.getName(), event.run.bind(event, client));              client.on(event.getName(), event.run.bind(event, client));
73                const endTime = Date.now();
74                registered(event, startTime, endTime);
75          }          }
76      }      }
77  }  }

Legend:
Removed from v.51  
changed lines
  Added in v.344

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26