chomens-bot-js/plugins/discord.js

123 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-11-27 02:35:28 -05:00
const { escapeMarkdown } = require('../util/escapeMarkdown')
async function inject (bot, dcclient, config) {
const chatMessage = require('prismarine-chat')(bot.version)
const channel = dcclient.channels.cache.get(config.discord.servers[`${bot.server.host}:${bot.server.port}`])
2022-11-11 07:03:44 -05:00
2022-11-27 02:35:28 -05:00
let queue = ''
const queueInterval = setInterval(() => {
2022-11-27 02:35:28 -05:00
if (queue === '') return
2022-11-11 07:03:44 -05:00
channel.send({
content: '```ansi\n' + queue.substring(0, 1986) + '\n```',
allowedMentions: {
2022-11-27 02:35:28 -05:00
parse: []
}
})
queue = ''
}, 1000)
2022-12-12 03:06:50 -05:00
bot.on('message', (message) => {
2022-11-27 02:35:28 -05:00
const cleanMessage = escapeMarkdown(message.toAnsi(), true)
const discordMsg = cleanMessage
2022-11-27 02:35:28 -05:00
.replaceAll('@', '@\u200b')
.replaceAll('http', 'http\u200b')
.replaceAll('\u001b[9', '\u001b[3')
if (message.toMotd().startsWith('§8[§eChomeNS §9Discord§8] §c')) return
queue += '\n' + discordMsg
})
2022-11-11 07:03:44 -05:00
// handle discord messages!!!
2022-11-27 02:35:28 -05:00
async function handleDiscordMessages (message) {
2022-11-11 07:03:44 -05:00
// Ignore messages from the bot itself
2022-11-27 02:35:28 -05:00
if (message.author.id === dcclient.user.id) return
2022-11-11 07:03:44 -05:00
// Only handle messages in specified channel
2022-11-27 02:35:28 -05:00
if (message.channel.id !== channel.id) return
if (message.content.startsWith(config.discord.prefix)) return
2022-11-11 07:03:44 -05:00
try {
2022-11-27 02:35:28 -05:00
const attachmentsComponent = []
2022-11-11 07:03:44 -05:00
if (message.attachments) {
2023-03-13 00:00:06 -04:00
for (const __attachment of message.attachments) {
const _attachment = [...__attachment]
const attachment = _attachment[1] // BEST WAY REAL!?/1?!
2022-11-11 07:03:44 -05:00
attachmentsComponent.push({
text: message.content === '' ? '[Attachment]' : ' [Attachment]', // may not be the best fix
color: 'green',
clickEvent: {
action: 'open_url',
2023-03-13 00:00:06 -04:00
value: attachment.proxyURL
2022-11-27 02:35:28 -05:00
}
})
}
2022-11-11 07:03:44 -05:00
}
const component = [
2022-11-27 02:35:28 -05:00
{ text: '[', color: 'dark_gray' },
{
text: 'ChomeNS ',
color: 'yellow',
2022-11-11 07:03:44 -05:00
clickEvent: {
action: 'open_url',
2022-11-27 02:35:28 -05:00
value: 'https://discord.gg/xdgCkUyaA4'
}
2022-11-11 07:03:44 -05:00
},
2022-11-27 02:35:28 -05:00
{
text: 'Discord',
color: 'blue',
2022-11-11 07:03:44 -05:00
clickEvent: {
action: 'open_url',
2022-11-27 02:35:28 -05:00
value: 'https://discord.gg/xdgCkUyaA4'
}
2022-11-11 07:03:44 -05:00
},
2022-11-27 02:35:28 -05:00
{ text: '] ', color: 'dark_gray' },
{
text: message.member.displayName,
2022-11-27 02:35:28 -05:00
color: 'red',
2022-11-11 07:03:44 -05:00
clickEvent: {
action: 'copy_to_clipboard',
2022-11-27 02:35:28 -05:00
value: `${message.author.username}#${message.author.discriminator}`
2022-11-11 07:03:44 -05:00
},
hoverEvent: {
action: 'show_text',
value: [
{
text: message.author.username,
2022-11-27 02:35:28 -05:00
color: 'white'
2022-11-11 07:03:44 -05:00
},
{
text: '#',
2022-11-27 02:35:28 -05:00
color: 'dark_gray'
2022-11-11 07:03:44 -05:00
},
{
text: message.author.discriminator,
2022-11-27 02:35:28 -05:00
color: 'gray'
2022-11-11 07:03:44 -05:00
},
'\n',
{
text: 'Click here to copy the tag to your clipboard',
2022-11-27 02:35:28 -05:00
color: 'green'
}
]
}
2022-11-11 07:03:44 -05:00
},
2022-11-27 02:35:28 -05:00
{ text: ' ', color: 'dark_gray' },
2022-11-11 07:03:44 -05:00
chatMessage.MessageBuilder.fromString('&7' + message.content),
2022-11-27 02:35:28 -05:00
attachmentsComponent.length === 0 ? '' : attachmentsComponent
]
bot.tellraw('@a', component)
2022-11-11 07:03:44 -05:00
} catch (e) {
2022-11-27 02:35:28 -05:00
2022-11-11 07:03:44 -05:00
}
}
2022-11-30 04:45:20 -05:00
bot.on('end', () => {
2022-11-27 02:35:28 -05:00
clearInterval(queueInterval)
dcclient.off('messageCreate', handleDiscordMessages)
})
2022-11-27 02:35:28 -05:00
dcclient.on('messageCreate', handleDiscordMessages)
};
2022-11-27 02:35:28 -05:00
module.exports = { inject }