/[sudobot]/trunk/src/api/controllers/InfoController.ts
ViewVC logotype

Contents of /trunk/src/api/controllers/InfoController.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 381 - (show annotations)
Mon Jul 29 17:29:53 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2893 byte(s)
feat(api): add endpoint for fetching commands
1 import { Collection } from "discord.js";
2 import { dot, object } from "dot-object";
3 import { NextFunction, Response } from "express";
4 import { body } from "express-validator";
5 import KeyValuePair from "../../types/KeyValuePair";
6 import BaseCommand from "../../utils/structures/BaseCommand";
7 import Controller from "../Controller";
8 import RequireAuth from "../middleware/RequireAuth";
9 import ValidatorError from "../middleware/ValidatorError";
10 import Request from "../Request";
11
12 export default class InfoController extends Controller {
13 globalMiddleware(): Function[] {
14 return [RequireAuth, ValidatorError, (request: Request, response: Response, next: NextFunction) => {
15 console.log(`URI: ` + request.path);
16
17 if (request.path === "/systeminfo/commands") {
18 next();
19 return;
20 }
21
22 const { id } = request.params;
23
24 if (!request.user?.guilds.includes(id)) {
25 return response.status(403).send({ error: "You don't have permission to access information of this guild." });
26 }
27
28 if (id === "global" || !this.client.config.props[id]) {
29 return response.status(404).send({ error: "No guild found with the given ID" });
30 }
31
32 next();
33 }];
34 }
35
36 middleware(): KeyValuePair<Function[]> {
37 return {
38
39 };
40 }
41
42 public async indexChannels(request: Request) {
43 const channels = this.client.guilds.cache.get(request.params.id)?.channels.cache;
44
45 if (request.query?.types) {
46 const types = request.query?.types.toString().split('|');
47 return channels?.filter(channel => types.includes(channel.type));
48 }
49
50 return channels;
51 }
52
53 public async indexRoles(request: Request) {
54 return this.client.guilds.cache.get(request.params.id)?.roles?.cache.sort((first, second) => second.position - first.position);
55 }
56
57 public async indexCommands() {
58 const commands = new Collection<string, BaseCommand>();
59
60 for (const [name, command] of this.client.commands) {
61 if (command.getAliases().includes(name)) {
62 console.log(command.getAliases(), "includes", name);
63 continue;
64 }
65
66 if (commands.has(name) || command.ownerOnly) {
67 console.log(commands.get(name)?.getName(), name);
68 continue;
69 }
70
71 if (commands.get(name) !== command)
72 commands.set(name, command);
73 }
74
75 return commands.map(command => ({
76 ...command,
77 permissions: command.permissions.map(perm => perm.toString()),
78 }));
79 }
80
81 public async indexGuilds(request: Request) {
82 return this.client.guilds.cache.filter(g => request.user?.guilds.includes(g.id) ?? false);
83 }
84 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26