forked from FrogeBot/frogeBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
179 lines (149 loc) · 6.59 KB
/
client.js
File metadata and controls
179 lines (149 loc) · 6.59 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require("dotenv").config() // Get .env
// Init discord.js
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('error', (error) => {
console.log(error);
});
process.on('uncaughtException', function (err) {
console.log(err);
})
const { parseMsg, isCmd } = require("./modules/parse.js")
const fs = require("fs")
const YAML = require('yaml')
const commands = YAML.parse(fs.readFileSync('./commands.yml', 'utf8'))
let { exec, execGM, getFormat } = require("@frogebot/image")({ imageMagick: process.env.USE_IMAGEMAGICK, maxGifSize: process.env.MAX_GIF_SIZE, maxImageSize: process.env.MAX_IMAGE_SIZE })
let { findImage, sendImage } = require("./modules/utils.js")
client.on('message', async msg => {
if(msg.author.bot || !await isCmd(msg)) return // If not command or called by bot
let parsed = await parseMsg(msg); // Parses message, returns [0: Prefix, 1: Command, 2: Args string]
let cmd = commands[parsed[1]]
let args = parsed[2]
let startTime = new Date().getTime()
if(cmd.type == 'script') { // If command is set as script type
let { cmdFunc } = require('./'+cmd.path) // Gets function of command
setImmediate(async () => {
cmdFunc(msg, args, startTime) // Runs command function
});
} else if (cmd.type == 'image') { // If command is set as image type
let imageUrl = await findImage(msg); // Find image in channel
try {
procMsg = await msg.channel.send(process.env.MSG_PROCESSING);
msg.channel.startTyping()
let r;
if(cmd.r) { // Handle replacement of input args in "r" val of command
for(let i = cmd.r.split('||').length-1; i >= 0; i--) {
newR = cmd.r.split('||')[i].trim()
if(newR.match(/\(input\)/) && args.length > 0) {
newR = newR.replace(/\(input\)/, args)
if(cmd.r_type == 'int' && Number.isInteger(Number(newR))) r = newR
if(cmd.r_type == 'num' && !Number.isNaN(Number(newR))) r = newR
} else if(newR.match(/\(input\:[0-9]+\)/) && args.length > 0) {
newR = newR.replace(/\(input:[0-9]+\)/, args.split(' ')[newR.replace(')','').split(':')[1]])
if(cmd.r_type.startsWith('int') && Number.isInteger(Number(newR))) r = newR
if(cmd.r_type.startsWith('num') && !Number.isNaN(Number(newR))) r = newR
} else if(!newR.match(/\(input\)/) && !newR.match(/\(input\:[0-9]+\)/)) {
r = newR;
}
}
}
// Parse "r" as a number if required
if(cmd.r_type == 'int') r = parseInt(r)
if(cmd.r_type == 'num') r = Number(r)
if(cmd.r_type == 'int>0') r = Math.max(0, parseInt(r))
if(cmd.r_type == 'num>0') r = Math.max(0, Number(r))
// Replace "(r)" with the variable r in params
let list = cmd.list.map(l => { if(typeof l == "object") { return [ Object.keys(l)[0], l[Object.keys(l)[0]].params.map(p => { if(p == '(r)') { return r } else { return p } }) ] } else { return [ l, [] ] } })
// Execute command
let img;
if(cmd.library == 'jimp') img = await exec(imageUrl, list);
if(cmd.library == 'magick') img = await execGM(imageUrl, list);
let extension = await getFormat(imageUrl)
// Send image
sendImage(msg, cmd.title, startTime, img, extension, procMsg)
} catch(e) {
// If error, catch it and let the user know
console.log(e)
msg.channel.stopTyping()
msg.channel.send({
embed: {
"title": "Error",
"description": `<@${msg.author.id}> - ${ imageUrl != undefined ? process.env.MSG_ERROR : process.env.MSG_NO_IMAGE}`,
"color": Number(process.env.EMBED_COLOUR),
"timestamp": new Date(),
"author": {
"name": process.env.BOT_NAME,
"icon_url": msg.client.user.displayAvatarURL()
}
}
})
procMsg.delete();
}
} else if (cmd.type == 'music' && process.env.MUSIC_ENABLED == "true") { // If command is set as music type
musicWorker.postMessage({ msgId: msg.id, channelId: msg.channel.id, args, cmd })
}
});
let musicWorker;
if(process.env.MUSIC_ENABLED == "true") {
const { Worker } = require('worker_threads');
const musicWorkerPath = '/modules/music-worker.js'
musicWorker = new Worker(__dirname+musicWorkerPath)
}
var path = require('path');
if(process.env.WEB_ENABLED == "true") {
fs.mkdir("web_images", { recursive: true }, (err) => {
if (err) {
return console.error(err);
}
console.log('Created web_images directory.');
});
}
function gracefulShutdown() {
client.destroy();
console.log("Destroyed client")
if(process.env.WEB_ENABLED == "true") {
fs.rmdir("web_images", { recursive: true }, () => {
console.log('Removed web_images directory.');
process.exit();
})
} else {
process.exit();
}
}
if(process.env.WEB_ENABLED == "true") {
const express = require('express')
const app = express()
// set up rate limiter: maximum of five requests per minute
var RateLimit = require('express-rate-limit');
var limiter = new RateLimit({
windowMs: 1*60*1000, // 1 minute
max: 50
});
// apply rate limiter to all requests
app.use(limiter);
app.use(express.static("web/public"))
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname,"web/index.html"))
})
app.get("/images/*", async (req, res) => {
let imgPath = path.join(__dirname,"web_images/"+req.path.split("/")[2])
if (fs.existsSync(imgPath)) {
const r = fs.createReadStream(imgPath)
r.pipe(res)
} else {
const r = fs.createReadStream("assets/imageExpired.png")
r.pipe(res)
}
})
app.listen(process.env.WEB_PORT, () => {
console.log(`Web host listening at http${process.env.WEB_SECURE == "true" ? "s" : ""}://${process.env.WEB_HOSTNAME}:${process.env.WEB_PORT}`)
})
}
// e.g. kill
process.on('SIGTERM', gracefulShutdown);
// e.g. Ctrl + C
process.on('SIGINT', gracefulShutdown);
client.login(process.env.TOKEN); // discord.js connect to discord bot