Add loglevel, public setting and config env
* Log levels for less spam when running in production * Add 'public' setting that allows to not post the invite link for the /help command * config env variable that allows running multiple instances with different config
This commit is contained in:
parent
b0c22efc23
commit
a60291bec2
3 changed files with 35 additions and 29 deletions
|
@ -1,10 +1,12 @@
|
||||||
{
|
{
|
||||||
|
"loglevel": "info",
|
||||||
"http": {
|
"http": {
|
||||||
"port": 3002
|
"port": 3002
|
||||||
},
|
},
|
||||||
"discord": {
|
"discord": {
|
||||||
"token": "<discord bot token>",
|
"token": "<discord bot token>",
|
||||||
"client_id": "<discord client id>"
|
"client_id": "<discord client id>",
|
||||||
|
"public": true
|
||||||
},
|
},
|
||||||
"templates": {
|
"templates": {
|
||||||
"bee": {
|
"bee": {
|
||||||
|
|
59
index.js
59
index.js
|
@ -11,10 +11,13 @@ const config = require('./config.default.json');
|
||||||
|
|
||||||
const filters = require('./filters.js');
|
const filters = require('./filters.js');
|
||||||
|
|
||||||
|
const logger = require('loglevel');
|
||||||
|
logger.setLevel(config.loglevel || 'info');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
_.extend(config, require('./config')); // eslint-disable-line global-require
|
_.extend(config, require(`./${process.env.CONFIG || 'config'}`)); // eslint-disable-line global-require
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('No config.json found!');
|
logger.error('No config.json found!');
|
||||||
}
|
}
|
||||||
|
|
||||||
function all(x, c) {
|
function all(x, c) {
|
||||||
|
@ -61,9 +64,9 @@ function render(template, img, size, flipH) {
|
||||||
if (!size.height) imgHeight = imgHeight * size.width / img.width;
|
if (!size.height) imgHeight = imgHeight * size.width / img.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Drawing template: ', template);
|
logger.debug('Drawing template: ', template);
|
||||||
const anchor = getNumericAnchor(template.anchor, imgWidth, imgHeight);
|
const anchor = getNumericAnchor(template.anchor, imgWidth, imgHeight);
|
||||||
console.log('Numeric anchor: ', anchor);
|
logger.debug('Numeric anchor: ', anchor);
|
||||||
const xScale = imgWidth / anchor.x.size;
|
const xScale = imgWidth / anchor.x.size;
|
||||||
const yScale = imgHeight / anchor.y.size;
|
const yScale = imgHeight / anchor.y.size;
|
||||||
const templateScale = Math.max(0, Math.min(10, Math.max(xScale || 0, yScale || 0)));
|
const templateScale = Math.max(0, Math.min(10, Math.max(xScale || 0, yScale || 0)));
|
||||||
|
@ -73,10 +76,10 @@ function render(template, img, size, flipH) {
|
||||||
templateOffsetX = calculatePosition(templateScale, anchor.x, imgWidth);
|
templateOffsetX = calculatePosition(templateScale, anchor.x, imgWidth);
|
||||||
templateOffsetY = calculatePosition(templateScale, anchor.y, imgHeight);
|
templateOffsetY = calculatePosition(templateScale, anchor.y, imgHeight);
|
||||||
|
|
||||||
console.log('xScale', xScale);
|
logger.debug('xScale', xScale);
|
||||||
console.log('yScale', yScale);
|
logger.debug('yScale', yScale);
|
||||||
console.log('templateOffsetX', templateOffsetX);
|
logger.debug('templateOffsetX', templateOffsetX);
|
||||||
console.log('templateOffsetY', templateOffsetY);
|
logger.debug('templateOffsetY', templateOffsetY);
|
||||||
|
|
||||||
let imageOffsetX = 0;
|
let imageOffsetX = 0;
|
||||||
let imageOffsetY = 0;
|
let imageOffsetY = 0;
|
||||||
|
@ -129,13 +132,13 @@ function render(template, img, size, flipH) {
|
||||||
attributes: template.attributes,
|
attributes: template.attributes,
|
||||||
filter: filters[template.filter]
|
filter: filters[template.filter]
|
||||||
}].sort((u, v) => u.z - v.z);
|
}].sort((u, v) => u.z - v.z);
|
||||||
console.log('To draw:', toDraw);
|
logger.debug('To draw:', toDraw);
|
||||||
|
|
||||||
let canvas = new CanvasEx(resultingWidth, resultingHeight);
|
let canvas = new CanvasEx(resultingWidth, resultingHeight);
|
||||||
|
|
||||||
for (let i = 0; i < toDraw.length; ++i) {
|
for (let i = 0; i < toDraw.length; ++i) {
|
||||||
const subject = toDraw[i];
|
const subject = toDraw[i];
|
||||||
console.log(`Drawing ${subject.name}${subject.flipH ? ' (flipped)' : ''}`);
|
logger.debug(`Drawing ${subject.name}${subject.flipH ? ' (flipped)' : ''}`);
|
||||||
try {
|
try {
|
||||||
const transform = {};
|
const transform = {};
|
||||||
if (subject.flipH) {
|
if (subject.flipH) {
|
||||||
|
@ -152,7 +155,7 @@ function render(template, img, size, flipH) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
logger.error(err);
|
||||||
throw new Error(JSON.stringify({ status: 400, error: 'Invalid template' }));
|
throw new Error(JSON.stringify({ status: 400, error: 'Invalid template' }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,10 +168,10 @@ app.get('/debug/frame/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const img = new ImageEx(req.query.url);
|
const img = new ImageEx(req.query.url);
|
||||||
await img.loaded;
|
await img.loaded;
|
||||||
console.log(img.frames);
|
logger.debug(img.frames);
|
||||||
return img.frames[req.query.num].canvas.pngStream().pipe(res);
|
return img.frames[req.query.num].canvas.pngStream().pipe(res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
logger.error(err);
|
||||||
return res.status(400).end(err.message);
|
return res.status(400).end(err.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -181,11 +184,11 @@ app.get('/', async (req, res) => {
|
||||||
templateList.push(key);
|
templateList.push(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(templateList);
|
logger.debug(templateList);
|
||||||
res.setHeader('Content-Type', 'application/json')
|
res.setHeader('Content-Type', 'application/json')
|
||||||
return res.end(JSON.stringify(templateList));
|
return res.end(JSON.stringify(templateList));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
logger.error(err);
|
||||||
return res.status(400).end(err.message);
|
return res.status(400).end(err.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -197,7 +200,7 @@ app.get('/:templateName/', async (req, res) => {
|
||||||
return res.status(400).end('Invalid url!');
|
return res.status(400).end('Invalid url!');
|
||||||
}
|
}
|
||||||
let direction = req.query.reverse === 'true' ? '\\' : '/';
|
let direction = req.query.reverse === 'true' ? '\\' : '/';
|
||||||
console.log('Got command ', direction, req.params.templateName, direction === '\\' ? 'flipped' : 'not flipped', req.query.url);
|
logger.debug('Got command ', direction, req.params.templateName, direction === '\\' ? 'flipped' : 'not flipped', req.query.url);
|
||||||
result = new ImageEx(req.query.url);
|
result = new ImageEx(req.query.url);
|
||||||
await result.loaded; // eslint-disable-line no-await-in-loop
|
await result.loaded; // eslint-disable-line no-await-in-loop
|
||||||
const templateData = templates[req.params.templateName];
|
const templateData = templates[req.params.templateName];
|
||||||
|
@ -207,13 +210,13 @@ app.get('/:templateName/', async (req, res) => {
|
||||||
|
|
||||||
return result.export(res);
|
return result.export(res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
logger.error(err);
|
||||||
return res.status(400).end({'error': err.message});
|
return res.status(400).end({'error': err.message});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(config.http.port, () => {
|
app.listen(config.http.port, () => {
|
||||||
console.log(`Beebot app listening on port ${config.http.port}!`);
|
logger.info(`Beebot app listening on port ${config.http.port}!`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -228,11 +231,11 @@ const invitelink = `https://discordapp.com/oauth2/authorize?client_id=${
|
||||||
config.discord.client_id}&scope=bot&permissions=0`;
|
config.discord.client_id}&scope=bot&permissions=0`;
|
||||||
/* const authlink = `https://discordapp.com/oauth2/authorize?client_id=${
|
/* const authlink = `https://discordapp.com/oauth2/authorize?client_id=${
|
||||||
config.discord.client_id}&scope=email`; */
|
config.discord.client_id}&scope=email`; */
|
||||||
console.log(`Bot invite link: ${invitelink}`);
|
logger.info(`Bot invite link: ${invitelink}`);
|
||||||
|
|
||||||
client.login(config.discord.token).catch(error => {
|
client.login(config.discord.token).catch(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error("Couldn't login: ", error.toString());
|
logger.error("Couldn't login: ", error.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -294,8 +297,8 @@ function reverseString(str) {
|
||||||
const commands = Object.keys(templates).map(x => `/${x}`).join(', ');
|
const commands = Object.keys(templates).map(x => `/${x}`).join(', ');
|
||||||
const otherCommands = {
|
const otherCommands = {
|
||||||
invite: `Invite link: <${invitelink}>`,
|
invite: `Invite link: <${invitelink}>`,
|
||||||
help: `Available commands: ${commands}.\nUse \\\\<command> to flip the template horizontally.\nInvite link: <${invitelink}>`,
|
help: `Available commands: ${commands}.\nUse \\\\<command> to flip the template horizontally.${config.discord.public ? "\nInvite link: <" + invitelink + ">" : ""}`,
|
||||||
beebot: `Available commands: ${commands}.\nUse \\\\<command> to flip the template horizontally.\nInvite link: <${invitelink}>`
|
beebot: `Available commands: ${commands}.\nUse \\\\<command> to flip the template horizontally.${config.discord.public ? "\nInvite link: <" + invitelink + ">" : ""}`
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -323,7 +326,7 @@ client.on('message', async message => {
|
||||||
if (commandParsed) {
|
if (commandParsed) {
|
||||||
const [, direction, command] = commandParsed;
|
const [, direction, command] = commandParsed;
|
||||||
if (templates[command]) {
|
if (templates[command]) {
|
||||||
console.log('Got command ', direction, command, direction === '\\' ? 'flipped' : 'not flipped', emoji);
|
logger.debug('Got command ', direction, command, direction === '\\' ? 'flipped' : 'not flipped', emoji);
|
||||||
count++;
|
count++;
|
||||||
name += command;
|
name += command;
|
||||||
if (result === null) {
|
if (result === null) {
|
||||||
|
@ -344,20 +347,20 @@ client.on('message', async message => {
|
||||||
{ attachment, name: `${name}.${emoji.ext}` }
|
{ attachment, name: `${name}.${emoji.ext}` }
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
console.log('Sending message with result:', result);
|
logger.debug('Sending message with result:', result);
|
||||||
await message.channel.send('', messageOptions).then(() => {
|
await message.channel.send('', messageOptions).then(() => {
|
||||||
console.log('Message sent!');
|
logger.debug('Message sent!');
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.error('Message sending failed:', err);
|
logger.error('Message sending failed:', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
logger.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', exception => {
|
process.on('uncaughtException', exception => {
|
||||||
console.log(exception);
|
logger.error(exception);
|
||||||
});
|
});
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
"gifencoder": "^1.1.0",
|
"gifencoder": "^1.1.0",
|
||||||
"got": "^8.0.1",
|
"got": "^8.0.1",
|
||||||
"lodash": "^4.17.13",
|
"lodash": "^4.17.13",
|
||||||
|
"loglevel": "^1.7.1",
|
||||||
"mime-types": "^2.1.17",
|
"mime-types": "^2.1.17",
|
||||||
"omggif": "^1.0.9",
|
"omggif": "^1.0.9",
|
||||||
"request": "^2.88.0",
|
"request": "^2.88.0",
|
||||||
|
|
Reference in a new issue