chomens-bot-js/plugins/commands.js
2022-11-13 13:54:34 +07:00

85 lines
4 KiB
JavaScript

/* eslint-disable max-len */
const loadFiles = require('../util/load_files');
const path = require('path');
const {MessageEmbed} = require('discord.js');
function inject(bot, dcclient, config) {
const channeldc = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);
bot.command_handler = {};
bot.command_handler.commands = {};
function reload() {
bot.command_handler.commands = loadFiles(path.join(__dirname, config.commandsDir));
}
reload();
bot.command_handler.main = function(prefix, username, usernameraw, message, sender, channeldc) {
reload();
let raw;
let command;
message.content ? raw = message.content.substring(prefix.length) : raw = message.substring(prefix.length);
const [commandName, ...args] = raw.split(' ');
command = bot.command_handler.commands.find((command) => command.name === commandName.toLowerCase());
try {
const alias = bot.command_handler.commands.find((command) => command.alias.includes(commandName.toLowerCase()));
if (alias) command = bot.command_handler.commands.find((command) => command.alias.includes(commandName.toLowerCase()));
if (prefix === '*' && message.endsWith('*') && message !== '*') return;
if (!command) throw new Error(`Unknown command: "${commandName}"`);
if (prefix === config.discord.prefix) {
if (typeof command.discordExecute === 'undefined') throw new Error('This command is not yet supported on discord!');
command.discordExecute(bot, username, usernameraw, sender, prefix, args, channeldc, message, config);
} else {
command.execute(bot, username, usernameraw, sender, prefix, args, config);
}
} catch (e) {
if (prefix === config.discord.prefix) {
const Embed = new MessageEmbed()
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`${e}\`\`\``);
channeldc.send({embeds: [Embed]});
} else {
bot.tellraw('@a', {text: String(e), color: 'red'});
}
}
};
bot.command_handler.run = function(username, usernameraw, message, sender, channeldc) {
for (const prefix of config.prefixes) {
if (!message.startsWith(prefix)) continue;
bot.command_handler.main(prefix, username, usernameraw, message, sender, channeldc);
}
};
bot.on('message', async (usernamee, messagee, senderr) => {
// try catch cuz TypeError: Cannot read properties of undefined (reading 'replace')
try {
const usernameraw = usernamee.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
const sender = bot.options.host === 'sus.shhnowisnottheti.me' && senderr !== '00000000-0000-0000-0000-000000000000' ? senderr : bot.playersAddedPlayers[usernameraw];
const username = bot.getplayerusername[sender];
const message = messagee.replace(/* /§r/g */ /§[a-f0-9rlonmk]/g, '')/* .replace(/§/g, '')*/;
bot.command_handler.run(username, usernameraw, message, sender);
} catch (e) {
bot.console.error(e);
}
});
bot.on('cspy', async function(usernamee, messagee) {
const username = usernamee.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
const message = messagee.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
const sender = bot.playersAddedPlayers[username];
bot.command_handler.run(username, username, message, sender);
});
function handleDiscordMessages(message) {
try {
// ignores the message that comes from the bot itself
if (message.author.id === dcclient.user.id) return;
// only receive messages in SPECIFIC channel
if (message.channel.id !== channeldc.id) return;
if (!message.content.startsWith(config.discord.prefix)) return;
bot.command_handler.main(config.discord.prefix, message.member.displayName, message.member.displayName, message, 'no sender for discord', channeldc);
} catch (e) {
return;
};
}
bot.once('end', () => {
dcclient.off('messageCreate', handleDiscordMessages);
});
dcclient.on('messageCreate', handleDiscordMessages);
};
module.exports = {inject};