-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.ts
52 lines (41 loc) · 1.78 KB
/
App.ts
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
import * as discord from 'discord.io';
import {config} from "dotenv";
import * as plugins from "./plugins";
config(); //initialize dotenv to load token form .env
let bot = new discord.Client({
token: process.env.TOKEN,
autorun: true
});
bot.on('ready', function (evt) {
let currentDate = new Date();
let currentDateLocal = currentDate.toLocaleDateString('en-GB');
let currentTime = currentDate.toLocaleTimeString(undefined, {hour12: false});
console.log('%s %s || Connected', currentDateLocal, currentTime);
console.log('%s %s || Logged in as: %s [%s]', currentDateLocal, currentTime, bot.username, bot.id);
});
bot.on('message', function (usr: string, usrID: string, cID: string, message: string, event) {
if (message.substring(0, 1) === '/') {
let args = message.substring(1).split(' ');
let cmd = args[0];
let func = Object.values(plugins).filter(plugin => plugin.invoker === cmd)[0];
if (func !== null && func !== undefined) {
args = args.splice(1);
logMsg(cmd, usr, args);
func.run(args).then(result => sendMsg(result, cID), reject => {sendMsg({'message': "An error has occurred during command execution"}, cID); logMsg(cmd, usr, reject)})
}
}
});
function sendMsg(sendable, cID: string) {
bot.sendMessage({
to: cID,
message: sendable.message,
embed: sendable.embed,
tts: sendable.tts,
nonce: sendable.nonce,
typing: sendable.typing
});
}
function logMsg(cmd: string, usr: string, args: Array<string>) {
let currentDate = new Date();
console.log(`${currentDate.toLocaleDateString('en-GB')} ${currentDate.toLocaleTimeString(undefined, {hour12: false})} || Command requested: \'${cmd}\'; Requested by: ${usr}; Args: [${args}]`);
}