-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
98 lines (82 loc) · 2.9 KB
/
index.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
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
/*
* "index.js"
* ----------
* i have no fucking idea what I'm doing lmao
*/
/* initalize dotenv, basically the same as reading from a text file tbh */
require('dotenv').config({path: __dirname + '/../.env'});
/* import external stuff */
import * as utils from './utilities';
import * as msgCmds from './message-commands';
import * as Discord from 'discord.js';
/* gonna be used to check for a log file */
const cmdArgs:Array<string> = process.argv.slice(2);
/* check for a logfile */
var logFile:string = '';
if(cmdArgs.includes('--logfile')) {
logFile = cmdArgs[cmdArgs.indexOf('--logfile') + 1];
}
else if (cmdArgs.findIndex(element => element.includes('--logfile=')) != -1) {
var index:number = cmdArgs.findIndex(element => element.includes('--logfile='));
logFile = cmdArgs[index].replace('--logfile=', '');
}
/* don't need all the features, just these */
const intents:Discord.Intents = new Discord.Intents();
intents.add(Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES);
/* create a new client */
const client:Discord.Client = new Discord.Client({intents: intents});
/* when client is connected, it will output a thing */
client.on('ready', () => {
var botUser = client.user;
if(botUser === null) return;
utils.log('==================================================', logFile);
utils.log('logged in as "' + botUser.tag + '"', logFile);
utils.log('-------------------------', logFile);
});
/* do a thing if the content of a user message matches */
client.on('messageCreate', async msg => {
/* if the message is from the bot, ignore it */
if(msg.author === client.user) return;
switch(msg.channel.type) {
case 'GUILD_TEXT':
if(msg.channel.name !== 'breaknbreak') return;
break;
default:
break;
}
var logOutput = undefined;
var content = msg.content.toLowerCase();
var msgItems:Array<string> = content.split(' ');
switch(msgItems[0]) {
case "!help":
var returns = msgCmds.helpCmd();
msg.channel.send({embeds: [returns.embed]});
logOutput = "OK";
break;
case "!hi":
msg.channel.send("Hello!");
logOutput = `OK`;
break;
case "!roll":
var vals = msgCmds.rollRNG(msgItems[1], msg.member?.displayName || msg.author.username);
console.log(msg.member?.displayName);
msg.channel.send({embeds: [vals.embed]});
logOutput = vals.logStr;
break;
default:
break;
}
if(logOutput != undefined) utils.log(msg.author.tag + ' used ' + msgItems[0] + '...' + logOutput, logFile);
});
/* handle exit signals */
process.on('SIGTERM', () => {handleExit()});
process.on('SIGINT', () => {handleExit()});
function handleExit() {
utils.log('received an exit signal, shutting down', logFile);
utils.log('==================================================', logFile);
client.destroy();
process.exit();
}
/* connect to the server */
client.login(process.env.TEST_TOKEN);