chomens-bot-js/plugins/commands.js

103 lines
4.8 KiB
JavaScript
Raw Normal View History

2022-11-16 00:05:04 -05:00
/* eslint-disable require-jsdoc */
2022-08-14 05:51:45 -04:00
/* eslint-disable max-len */
2022-11-27 02:35:28 -05:00
const path = require('path')
const { MessageEmbed } = require('discord.js')
function inject (bot, dcclient, config) {
const loadFiles = require('../util/load_files')
const channeldc = dcclient.channels.cache.get(config.discord.servers[bot.options.host])
bot.command_handler = {}
bot.command_handler.commands = {}
bot.command_handler.reload = async function () {
bot.command_handler.commands = await loadFiles(path.join(__dirname, config.commandsDir))
}
bot.command_handler.reload()
bot.command_handler.main = function (prefix, username, usernameraw, message, sender, channeldc, hash, ownerhash, selector) {
bot.command_handler.reload()
let raw
let command
const discord = !!message.content
2022-11-27 02:35:28 -05:00
discord
? raw = message.content.substring(prefix.length)
: raw = message.substring(prefix.length)
2022-11-27 02:35:28 -05:00
const [commandName, ...args] = raw.split(' ')
command = bot.command_handler.commands.find((command) => command.name === commandName.toLowerCase())
2022-08-14 05:51:45 -04:00
try {
2022-11-27 02:35:28 -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()))
if (prefix === '*' && message.endsWith('*') && message !== '*') return
if (!command) throw new Error(`Unknown command: "${commandName}"`)
2022-08-14 05:51:45 -04:00
if (command.name !== 'validate' && command.trusted === 1) {
2022-11-27 02:35:28 -05:00
if (discord && !message.member?.roles?.cache?.some((role) => role.name === 'Trusted')) throw new Error('You\'re not in the trusted role!')
else if (!discord && args[0] !== hash) throw new Error('Invalid hash')
} else if (command.name !== 'validate' && command.trusted === 2) {
2022-11-27 02:35:28 -05:00
if (discord && !message.member?.roles?.cache?.some((role) => role.name === 'Trusted')) throw new Error('You\'re not in the trusted role!')
else if (!discord && args[0] !== ownerhash) throw new Error('Invalid OwnerHash')
}
2022-11-07 07:22:42 -05:00
if (prefix === config.discord.prefix) {
2022-11-27 02:35:28 -05:00
if (!command.discordExecute) throw new Error('This command is not yet supported on discord!')
command.discordExecute(bot, username, usernameraw, sender, prefix, args, channeldc, message, config)
2022-08-14 05:51:45 -04:00
} else {
2022-11-27 02:35:28 -05:00
command.execute(bot, username, usernameraw, sender, prefix, args, config, hash, ownerhash, selector)
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()
2022-11-27 02:35:28 -05:00
.setColor('#FF0000')
.setTitle('Error')
.setDescription(`\`\`\`${e}\`\`\``)
channeldc.send({ embeds: [Embed] })
2022-08-14 05:51:45 -04:00
} else {
2022-11-27 02:35:28 -05:00
bot.tellraw(selector, { text: String(e), color: 'red' })
2022-08-14 05:51:45 -04:00
}
}
2022-11-27 02:35:28 -05:00
}
bot.command_handler.run = function (username, usernameraw, message, sender, channeldc, hash, ownerhash, selector = '@a') {
2022-08-14 05:51:45 -04:00
for (const prefix of config.prefixes) {
2022-11-27 02:35:28 -05:00
if (!message.startsWith(prefix)) continue
bot.command_handler.main(prefix, username, usernameraw, message, sender, channeldc, hash, ownerhash, selector)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
}
2022-11-16 00:05:04 -05:00
bot.on('message', async (_username, _message, _sender) => {
2022-08-14 05:51:45 -04:00
// try catch cuz TypeError: Cannot read properties of undefined (reading 'replace')
try {
2022-11-27 02:35:28 -05:00
const usernameraw = _username.replace(/§.?/g, '')
const sender = _sender !== '00000000-0000-0000-0000-000000000000' ? _sender : bot.playersAddedPlayers[usernameraw]
let username
if (!bot.getplayerusername[sender]) username = usernameraw
else username = bot.getplayerusername[sender]
const message = _message.replace(/* /§r/g */ /§.?/g, '')/* .replace(/§/g, '') */
bot.command_handler.run(username, usernameraw, message, sender, channeldc, bot.hash, bot.ownerHash)
2022-08-14 05:51:45 -04:00
} catch (e) {
2022-11-27 02:35:28 -05:00
bot.console.error(e)
2022-08-14 05:51:45 -04:00
}
2022-11-27 02:35:28 -05:00
})
bot.on('cspy', async function (_username, _message) {
const username = _username.replace(/§.?/g, '')
const message = _message.replace(/§.?/g, '')
const sender = bot.playersAddedPlayers[username]
bot.command_handler.run(username, username, message, sender, channeldc, bot.hash, bot.ownerHash, username)
})
function handleDiscordMessages (message) {
2022-08-14 05:51:45 -04:00
try {
// ignores the message that comes from the bot itself
2022-11-27 02:35:28 -05:00
if (message.author.id === dcclient.user.id) return
2022-08-14 05:51:45 -04:00
// only receive messages in SPECIFIC channel
2022-11-27 02:35:28 -05:00
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)
2022-08-14 05:51:45 -04:00
} catch (e) {
2022-11-27 02:35:28 -05:00
return
2022-08-14 05:51:45 -04:00
};
}
2022-11-30 04:45:20 -05:00
bot.on('end', () => {
2022-11-27 02:35:28 -05:00
dcclient.off('messageCreate', handleDiscordMessages)
})
dcclient.on('messageCreate', handleDiscordMessages)
2022-08-14 05:51:45 -04:00
};
2022-11-27 02:35:28 -05:00
module.exports = { inject }