chomens-bot-js/plugins/commands.js

81 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-08-14 05:51:45 -04:00
/* eslint-disable no-var */
/* eslint-disable max-len */
2022-08-18 22:36:34 -04:00
const config = require('../config');
2022-08-14 05:51:45 -04:00
const loadFiles = require('../util/load_files');
const path = require('path');
const {MessageEmbed} = require('discord.js');
function inject(bot, dcclient) {
bot.command_handler.commands = loadFiles(path.join(__dirname, '../commands'));
bot.command_handler.main = function(prefix, username, usernameraw, message, sender, channeldc) {
2022-08-18 20:15:05 -04:00
let raw;
if (typeof message.content!=='undefined') raw = message.content.substr(prefix.length);
if (typeof message.content==='undefined') raw = message.substr(prefix.length);
2022-08-14 05:51:45 -04:00
const [commandName, ...args] = raw.split(' ');
var command = bot.command_handler.commands.find((command) => command.name === commandName.toLowerCase());
try {
var alias = bot.command_handler.commands.find((command) => command.alias.includes(commandName.toLowerCase()));
if (alias !== undefined) {
var command = bot.command_handler.commands.find((command) => command.alias.includes(commandName.toLowerCase()));
}
if (command === undefined) {
throw new Error(`Unknown command: "${commandName}"`);
}
if (prefix==='!') {
if (typeof command.discordExecute==='undefined') throw new Error('This command is not yet supported on discord!');
2022-08-18 19:42:14 -04:00
command.discordExecute(bot, username, usernameraw, sender, prefix, args, channeldc, message);
2022-08-14 05:51:45 -04:00
} else {
command.execute(bot, username, usernameraw, sender, prefix, args);
}
} catch (e) {
if (prefix==='!') {
const Embed = new MessageEmbed()
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`${e}\`\`\``);
channeldc.send({embeds: [Embed]});
} else {
2022-08-16 08:15:11 -04:00
bot.core.run('minecraft:tellraw @a ' + JSON.stringify({text: String(e), color: 'red'}));
2022-08-14 05:51:45 -04:00
}
}
};
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 (username, message, sender) => {
// try catch cuz TypeError: Cannot read properties of undefined (reading 'replace')
try {
const usernameraw = username.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
2022-08-14 06:19:41 -04:00
var sender = bot.playersAddedPlayers[usernameraw];
var username = bot.getplayerusername[sender];
2022-08-14 05:51:45 -04:00
var message = message.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
2022-08-19 00:53:09 -04:00
bot.command_handler.run(username, usernameraw, message, sender);
2022-08-14 05:51:45 -04:00
} catch (e) {
return;
}
});
bot.on('cspy', async function(username, message) {
var username = username.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
var message = message.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
bot.command_handler.run(username, username, message, 'no uuid1!1!1!');
});
dcclient.on('messageCreate', async (message) => {
try {
// ignores the message that comes from the bot itself
if (message.author.id === dcclient.user.id) return;
const channelid = message.channel.id;
const channeldc = dcclient.channels.cache.get(channelid);
// only receive messages in SPECIFIC channel
if (message.channel.id != bot.channelId) return;
if (!message.content.startsWith(config.discord.prefix)) return;
2022-08-18 20:15:05 -04:00
bot.command_handler.main(config.discord.prefix, message.member.displayName, message.member.displayName, message, 'no sender for discord', channeldc);
2022-08-14 05:51:45 -04:00
} catch (e) {
return;
};
});
};
module.exports = {inject};