summaryrefslogtreecommitdiff
path: root/server/src/API/routers/GuildRouter.ts
blob: 01a8a9b870b0d53031a310e568114364df4b161f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Router, Request, Response, Application } from 'express';
import { AkairoClient } from 'discord-akairo';
import { Guild } from 'discord.js';
import { authorization } from '../../Config';

export default class GuildRouter {
    protected app: Application;
    protected client: AkairoClient;
    protected router: Router;
    
    public constructor(app: Application, client: AkairoClient) {
        this.app = app;
        this.client = client;
        this.router = Router();
        
        this.app.use(this.router);
        
        this.router.get('/v1/get/guild/:id', (req: Request, res: Response) => {
            const guild: Guild = this.client.guilds.cache.get(req.params.id);
            if (!guild) return res.status(404).send({ message: 'Guild Not Found' });
            
            return res.status(200).send({
                name: guild.name,
                owner: guild.owner.user.tag,
                members: guild.memberCount
            });
        });
        
        this.router.post('/v1/post/guild-name/:id', (req: Request, res: Response) => {
            if (req.headers.authorization !== authorization) return res.status(401).send({ message: 'Unauthorized' });
            
            const guild: Guild = this.client.guilds.cache.get(req.params.id);
            if (!guild) return res.status(404).send({ message: 'Guild Not Found' });
            
            if (!req.body.name) return res.status(404).send({ message: 'No Guild Name Provided' });
            if (req.body.name.length > 32) return res.status(400).send({ message: 'Guild Name Exceeds 32 Characters' });
            if (!guild.me.permissions.has('MANAGE_GUILD')) return res.status(401).send({ message: 'Cannot Manage Guild' });
            
            guild.setName(req.body.name);
            
            return res.status(201).send(req.body);
        });
    }
}