obama-bot/commands/eval.js
2021-02-18 15:22:32 +01:00

53 lines
1.8 KiB
JavaScript

const Discord = require('discord.js');
const { inspect } = require('util');
const { logger, client } = require('../');
module.exports.meta = {
name: 'eval',
aliases: ['e']
}
/**
* @param {Discord.Message} message
* @param {Array<string>} args
*/
module.exports.run = async (message, args) => {
const ownerID = process.env.OWNER_ID;
if (!ownerID) return message.channel.send('Error: Bot owner is not configured. Set `OWNER_ID` env var.');
if (ownerID != message.author.id) {
logger.warn(`${message.author.tag} ${message.author.id} => Not bot owner`);
return;
}
try {
const exec = args.join(' ');
if (!exec) return message.channel.send('What do you want me to do, dumbass');
eval(
`(async () => {` +
`${exec.replace(/(```\w*\n)|\n```$/g, '')}\n` +
`})()`
)
.then(res => {
if (res == undefined)
message.react('✅')
.catch(() => message.channel.send('```js\nundefined\n```'));
else
message.channel.send(
`\`\`\`js\n${
inspect(res)
.replace(new RegExp(client.token, 'g'), '[Token removed]')
.slice(0, 1989)
}\n\`\`\``
);
})
.catch(e => {
message.channel.send(`\`\`\`js\n${(e && e.name && e.message ? `${e.name}: ${e.message}` : `Error: ${e}`).slice(0, 1989)}\n\`\`\``)
.catch(a => message.channel.send(a));
});
} catch(e) {
message.channel.send(`\`\`\`js\n${(e && e.name && e.message ? `${e.name}: ${e.message}` : `Error: ${e}`).slice(0, 1989)}\n\`\`\``)
.catch(a => message.channel.send(a));
}
}