2022-08-14 05:51:45 -04:00
|
|
|
/* eslint-disable max-len */
|
|
|
|
const loadFiles = require('../util/load_files');
|
|
|
|
const path = require('path');
|
|
|
|
const {MessageEmbed} = require('discord.js');
|
2022-11-06 03:30:25 -05:00
|
|
|
function inject(bot, dcclient, config) {
|
2022-11-08 05:16:19 -05:00
|
|
|
bot.command_handler = {};
|
|
|
|
bot.command_handler.commands = {};
|
2022-10-22 06:38:24 -04:00
|
|
|
function reload() {
|
2022-11-06 03:30:25 -05:00
|
|
|
bot.command_handler.commands = loadFiles(path.join(__dirname, config.commandsDir));
|
2022-10-22 06:38:24 -04:00
|
|
|
}
|
|
|
|
reload();
|
2022-08-14 05:51:45 -04:00
|
|
|
bot.command_handler.main = function(prefix, username, usernameraw, message, sender, channeldc) {
|
2022-10-22 06:38:24 -04:00
|
|
|
reload();
|
2022-08-18 20:15:05 -04:00
|
|
|
let raw;
|
2022-10-17 23:05:27 -04:00
|
|
|
let command;
|
2022-11-07 07:22:42 -05:00
|
|
|
message.content ? raw = message.content.substring(prefix.length) : raw = message.substring(prefix.length);
|
2022-08-14 05:51:45 -04:00
|
|
|
const [commandName, ...args] = raw.split(' ');
|
2022-10-17 23:05:27 -04:00
|
|
|
command = bot.command_handler.commands.find((command) => command.name === commandName.toLowerCase());
|
2022-08-14 05:51:45 -04:00
|
|
|
try {
|
2022-11-07 07:22:42 -05:00
|
|
|
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()));
|
2022-10-17 23:05:27 -04:00
|
|
|
if (prefix === '*' && message.endsWith('*') && message !== '*') return;
|
2022-11-07 07:22:42 -05:00
|
|
|
if (!command) throw new Error(`Unknown command: "${commandName}"`);
|
2022-08-14 05:51:45 -04:00
|
|
|
|
2022-11-07 07:22:42 -05:00
|
|
|
if (prefix === config.discord.prefix) {
|
|
|
|
if (typeof command.discordExecute === 'undefined') throw new Error('This command is not yet supported on discord!');
|
2022-11-06 03:30:25 -05:00
|
|
|
command.discordExecute(bot, username, usernameraw, sender, prefix, args, channeldc, message, config);
|
2022-08-14 05:51:45 -04:00
|
|
|
} else {
|
2022-11-06 03:30:25 -05:00
|
|
|
command.execute(bot, username, usernameraw, sender, prefix, args, config);
|
2022-08-14 05:51:45 -04:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2022-11-07 07:22:42 -05:00
|
|
|
if (prefix === config.discord.prefix) {
|
2022-08-14 05:51:45 -04:00
|
|
|
const Embed = new MessageEmbed()
|
|
|
|
.setColor('#FF0000')
|
|
|
|
.setTitle('Error')
|
|
|
|
.setDescription(`\`\`\`${e}\`\`\``);
|
|
|
|
channeldc.send({embeds: [Embed]});
|
|
|
|
} else {
|
2022-11-07 06:30:37 -05:00
|
|
|
bot.tellraw('@a', {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);
|
|
|
|
}
|
|
|
|
};
|
2022-09-04 07:30:21 -04:00
|
|
|
bot.on('message', async (usernamee, messagee, senderr) => {
|
2022-08-14 05:51:45 -04:00
|
|
|
// try catch cuz TypeError: Cannot read properties of undefined (reading 'replace')
|
|
|
|
try {
|
2022-09-04 07:30:21 -04:00
|
|
|
const usernameraw = usernamee.replace(/§[a-f0-9rlonmk]/g, '').replace(/§/g, '');
|
2022-09-11 00:31:00 -04:00
|
|
|
const sender = bot.options.host === 'sus.shhnowisnottheti.me' && senderr !== '00000000-0000-0000-0000-000000000000' ? senderr : bot.playersAddedPlayers[usernameraw];
|
2022-09-04 08:18:39 -04:00
|
|
|
const username = bot.getplayerusername[sender];
|
2022-10-15 00:18:58 -04:00
|
|
|
const message = messagee.replace(/* /§r/g */ /§[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;
|
|
|
|
}
|
|
|
|
});
|
2022-10-21 04:01:09 -04:00
|
|
|
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);
|
2022-08-14 05:51:45 -04:00
|
|
|
});
|
|
|
|
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};
|