summaryrefslogtreecommitdiff
path: root/server/src/client/BotClient.ts
blob: cf8b778c1497eb9f08c63db76c12ba137a09a557 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { AkairoClient, CommandHandler, ListenerHandler, InhibitorHandler } from 'discord-akairo';
import { Message } from 'discord.js';
import { join } from 'path';
import { prefix, owners } from '../Config';
import { logger } from '../utils/Logger';
import { SettingsProvider } from '../database';
import { Logger } from 'winston';
import svg2img from 'svg2img';

declare module 'discord-akairo' {
    interface AkairoClient {
        commandHandler: CommandHandler;
        listenerHandler: ListenerHandler;
        logger: Logger;
        settings: SettingsProvider;
        img,
        wait,
        queue
    }
}

interface BotOptions {
    token?: string;
    owners?: string[];
    // prefix?: string;
}

export default class BotClient extends AkairoClient {
    public readonly config: BotOptions;
    
    public logger = logger;
    
    public img = (data) => {
        return new Promise((resolve, reject) => {
            svg2img(data, (error, buffer) => {
                return resolve(buffer);
            });
        });
    }

    public wait = require("util").promisify(setTimeout);
    
    public queue = new Map();
    
    public inhibitorHandler: InhibitorHandler = new InhibitorHandler(this, {
        directory: join(__dirname, '..', 'inhibitors')
    });
    
    public listenerHandler: ListenerHandler = new ListenerHandler(this, {
        directory: join(__dirname, '..', 'listeners')
    });
    
    public commandHandler: CommandHandler = new CommandHandler(this, {
        directory: join(__dirname, '..', 'commands'),
        /* prefix: async (msg: Message): Promise<string> => {
            if (msg.guild) {
                const doc = this.settings.cache.guilds.get(msg.guild.id);
                if (doc?.prefix) return doc.prefix;
            }
            return this.config.prefix
        }, */
        prefix,
        allowMention: true,
        defaultCooldown: 6e4, // 60000 - 6 - count the zeroes... = 4, so its 6e4
        ignorePermissions: owners,
        // Extra stuff
        argumentDefaults: {
            prompt: {
                modifyStart: (_: Message, str: string): string => `${str}\n\nType \`cancel\` to cancel the command...`,
                modifyRetry: (_: Message, str: string): string => `${str}\n\nType \`cancel\` to cancel the command...`,
                timeout: 'You took too long, the command has now been cancelled...',
                ended: 'You exceeded the maximum amount of tries, this command has now been cancelled...',
                cancel: 'This command has been cancelled...',
                retries: 3,
                time: 3e4
            },
            otherwise: ''
        }
    });
    
    public settings: SettingsProvider = new SettingsProvider(this);
    
    public constructor(config: BotOptions) {
        super({
            ownerID: config.owners,
            messageCacheMaxSize: 50,
            messageSweepInterval: 900,
            messageCacheLifetime: 300,
            partials: ['MESSAGE', 'REACTION']
        });
        
        this.config = config;
        
        this.on('shardError', (err: Error, id: any): Logger => this.logger.warn(`[SHARD ${id} ERROR] ${err.message}`, err.stack))
            .on('warn', (warn: any): Logger => this.logger.warn(`[CLIENT WARN] ${warn}`));
    }
    
    private async init(): Promise<this> {
        await this.settings.init();
        this.commandHandler.useInhibitorHandler(this.inhibitorHandler);
        this.commandHandler.useListenerHandler(this.listenerHandler);
        this.listenerHandler.setEmitters({
            commandHandler: this.commandHandler,
            listenerHandler: this.listenerHandler,
            inhibitorHandler: this.inhibitorHandler,
            process
        });
        this.commandHandler.loadAll();
        this.listenerHandler.loadAll();
        this.inhibitorHandler.loadAll();
        
        return this;
    }
    
    public async start(): Promise<string> {
        await this.init();
        return this.login(this.config.token);
    }
}