obama-bot/util/eventHandler.js
2020-12-27 13:04:08 +01:00

74 lines
2.8 KiB
JavaScript

const Discord = require('discord.js');
const { client, logger } = require('../index');
const prefix = process.env.BOT_PREFIX || '-';
module.exports.run = () => {
}
const { commands } = require('./commandLoader');
client.on('message', message => {
if (message.author?.bot) return;
addToDatabase(message.author.id);
require('./wit').execute(message);
try {
logger.debug(`${message.author.tag} => ${message.content}`);
let msgContent = message.content;
let usedPrefix;
if (msgContent.startsWith(prefix)) usedPrefix = prefix;
else if (msgContent.startsWith(`<@${client.user.id}>`)) usedPrefix = `<@${client.user.id}>`;
else if (msgContent.startsWith(`<@!${client.user.id}>`)) usedPrefix = `<@!${client.user.id}>`;
if (!usedPrefix) return require('./69Reply').execute(message);
msgContent = msgContent.substr(msgContent.indexOf(usedPrefix) + 1, msgContent.length);
if (msgContent.startsWith(' ') && usedPrefix != prefix) msgContent = msgContent.substr(1, msgContent.length);
const args = msgContent.split(' ');
const cmdName = args.shift()?.toLowerCase();
const cmd = commands.find(cmd => cmd.meta?.name == cmdName || cmd.meta?.aliases?.indexOf(cmdName) > -1);
if (!cmd) return require('./69Reply').execute(message);
logger.info(`[CMD] ${message.author.id} ${message.author.tag} => ${cmdName} ${args.join(' ')}`);
if (cmd.meta?.staffOnly && !message.member.permissions.has('ADMINISTRATOR')) {
logger.warn(`${message.author.tag} => Refusing to run staff command`);
message.channel.send(
new Discord.MessageEmbed()
.setTitle('You are unworthy')
.setDescription('This command can only be used by this server\'s staff.')
.setImage('https://media1.tenor.com/images/1056e92668594b262d3338c897ce9bd3/tenor.gif?itemid=7706023')
.setColor('ff0000')
)
return;
}
try {
cmd.run(message, args);
} catch(e) {
console.error(e);
message.channel.send(
new Discord.MessageEmbed()
.setTitle('You broke Obama')
.setDescription(`Good job, you broke Obama. Debug info: \`\`\`js\n${e}\`\`\``)
.setColor('ff0000')
)
return;
}
} catch(e) {
console.error(e);
message.channel.send('An error has occurred. fuck');
return;
}
});
const karma = require('./karma').karma;
const coins = require('./karma').coins;
function addToDatabase(id) {
if (!id) return;
if (typeof karma.get(id) != 'number') karma.set(id, 0);
if (typeof coins.get(id) != 'number') coins.set(id, 0);
return;
}