mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
111 lines
4.8 KiB
JavaScript
111 lines
4.8 KiB
JavaScript
|
|
const path = require('path')
|
|
const { EmbedBuilder } = 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, message, sender, channeldc, hash, ownerhash, selector, proxy) {
|
|
bot.command_handler.reload()
|
|
let raw
|
|
let command
|
|
const discord = !!message.content
|
|
|
|
discord
|
|
? 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 (command.name !== 'validate' && command.trusted === 1) {
|
|
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) {
|
|
if (discord && !message.member?.roles?.cache?.some((role) => role.name === 'ChomeNS')) throw new Error('You\'re not in the owner of this bot!')
|
|
else if (!discord && args[0] !== ownerhash) throw new Error('Invalid OwnerHash')
|
|
}
|
|
|
|
if (prefix === config.discord.prefix) {
|
|
if (!command.discordExecute) throw new Error('This command is not yet supported on discord!')
|
|
command.discordExecute(bot, username, sender, prefix, args, channeldc, message, config)
|
|
} else {
|
|
command.execute(bot, username, sender, prefix, args, config, hash, ownerhash, selector, proxy)
|
|
}
|
|
} catch (e) {
|
|
if (prefix === config.discord.prefix) {
|
|
const Embed = new EmbedBuilder()
|
|
.setColor(config.discord.embedsColors.error)
|
|
.setTitle('Error')
|
|
.setDescription(`\`\`\`${e}\`\`\``)
|
|
channeldc.send({ embeds: [Embed] })
|
|
} else {
|
|
bot.tellraw(selector, { text: String(e), color: 'red' })
|
|
}
|
|
}
|
|
}
|
|
bot.command_handler.run = function (username, message, sender, channeldc, hash, ownerhash, selector = '@a', proxy) {
|
|
for (const prefix of config.prefixes) {
|
|
if (!message.startsWith(prefix)) continue
|
|
bot.command_handler.main(prefix, username, message, sender, channeldc, hash, ownerhash, selector, proxy)
|
|
}
|
|
}
|
|
let previousMessage = { username: '', message: '' }
|
|
let ratelimit = 0
|
|
bot.on('chat', async (_username, _message) => {
|
|
// try catch cuz TypeError: Cannot read properties of undefined (reading 'replace')
|
|
try {
|
|
if (
|
|
previousMessage.username === _username &&
|
|
previousMessage.message === _message
|
|
) {
|
|
if (ratelimit === 3) ratelimit = 0
|
|
ratelimit++
|
|
return
|
|
}
|
|
|
|
previousMessage = { username: _username, _message }
|
|
|
|
const username = _username.replace(/§.?/g, '')
|
|
const sender = bot.players.list.find((val) => val.name === username)?.UUID
|
|
const message = _message.replace(/* /§r/g */ /§.?/g, '')/* .replace(/§/g, '') */
|
|
bot.command_handler.run(username, message, sender, channeldc, bot.hash, bot.ownerHash)
|
|
} catch (e) {
|
|
bot.console.error(e)
|
|
}
|
|
})
|
|
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, message, sender, channeldc, bot.hash, bot.ownerHash, username)
|
|
})
|
|
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, 'no sender for discord', channeldc)
|
|
} catch (e) {
|
|
return
|
|
};
|
|
}
|
|
bot.on('end', () => {
|
|
dcclient.off('messageCreate', handleDiscordMessages)
|
|
})
|
|
dcclient.on('messageCreate', handleDiscordMessages)
|
|
};
|
|
module.exports = { inject }
|