UNPKG

1.94 kBPlain TextView Raw
1import { Client } from 'discord.js';
2import { Context } from './Services/Context';
3import { Processor } from './Services/Processor';
4import dotenv from 'dotenv';
5import { IConfig } from './Interfaces/IConfig';
6
7// load environment
8dotenv.config();
9
10export class Aidyn {
11 private ConnectionString: string;
12 private BotToken: string;
13 private Processor: Processor;
14 private Loaded: boolean;
15 private ReloadCustoms: boolean;
16 public Client: Client;
17 public Context: Context;
18
19 constructor(config?: IConfig) {
20 const {
21 BotToken,
22 ConnectionString,
23 Prefix,
24 Logging,
25 CustomProcessor,
26 Owner,
27 ReloadCustoms
28 } = config;
29
30 this.Client = new Client();
31 this.Loaded = false;
32 this.Context = new Context(this.Client, Owner);
33 this.BotToken = BotToken;
34 this.Processor = CustomProcessor || new Processor(this.Context, Prefix, Owner, Logging);
35 this.ConnectionString = ConnectionString;
36 this.Context.Prefix = Prefix;
37 this.ReloadCustoms = ReloadCustoms;
38
39 if (!this.ConnectionString) {
40 this.Context.UseDb = false;
41 this.Context.Loading = false;
42 }
43 }
44
45 public async LoadCommands(commands: any): Promise<any> {
46 if (this.Context.Loading !== false) {
47 await this.Context.Initialize(this.ConnectionString);
48 }
49
50 const results = await this.Context.LoadCommands(commands, this.ReloadCustoms);
51
52 this.Loaded = true;
53
54 return results;
55 }
56
57 public async Start(commands?: any): Promise<Aidyn> {
58 if (this.Loaded === false && !commands) {
59 throw new Error('[FAILURE] No Commands Loaded!');
60 } else if (this.Loaded === false && commands) {
61 await this.LoadCommands(commands);
62 }
63
64 const { Client, Processor, BotToken } = this;
65
66 Client.on('message', (m) => Processor.Handle(m));
67
68 await Client.login(BotToken || process.env.BOT_TOKEN).then((token) => {
69 console.log('[SUCCESS] Bot Online');
70
71 return token;
72 });
73
74 return this;
75 }
76
77 public async Stop(): Promise<any> {
78 return this.Client.destroy();
79 }
80}