chomens-bot-js/plugins/discord.js
2022-12-12 15:06:50 +07:00

120 lines
3.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.options.host])
let queue = ''
const queueInterval = setInterval(() => {
if (queue === '') return
channel.send({
content: '```ansi\n' + queue.substring(0, 1986) + '\n```',
allowedMentions: {
parse: []
}
})
queue = ''
}, 1000)
bot.on('message', (message) => {
const cleanMessage = escapeMarkdown(message.toAnsi(), true)
const discordMsg = cleanMessage
.replaceAll('@', '@\u200b')
.replaceAll('http', 'http\u200b')
.replaceAll('\u001b[9', '\u001b[3')
if (message.toMotd().startsWith('§8[§eChomeNS §9Discord§8] §c')) return
queue += '\n' + discordMsg
})
// handle discord messages!!!
async function handleDiscordMessages (message) {
// Ignore messages from the bot itself
if (message.author.id === dcclient.user.id) return
// Only handle messages in specified channel
if (message.channel.id !== channel.id) return
if (message.content.startsWith(config.discord.prefix)) return
try {
const attachmentsComponent = []
if (message.attachments) {
message.attachments.forEach((value) => {
attachmentsComponent.push({
text: message.content === '' ? '[Attachment]' : ' [Attachment]', // may not be the best fix
color: 'green',
clickEvent: {
action: 'open_url',
value: value.proxyURL
}
})
})
}
const component = [
{ text: '[', color: 'dark_gray' },
{
text: 'ChomeNS ',
color: 'yellow',
clickEvent: {
action: 'open_url',
value: 'https://discord.gg/xdgCkUyaA4'
}
},
{
text: 'Discord',
color: 'blue',
clickEvent: {
action: 'open_url',
value: 'https://discord.gg/xdgCkUyaA4'
}
},
{ text: '] ', color: 'dark_gray' },
{
text: `${message.member.displayName}`,
color: 'red',
clickEvent: {
action: 'copy_to_clipboard',
value: `${message.author.username}#${message.author.discriminator}`
},
hoverEvent: {
action: 'show_text',
value: [
{
text: message.author.username,
color: 'white'
},
{
text: '#',
color: 'dark_gray'
},
{
text: message.author.discriminator,
color: 'gray'
},
'\n',
{
text: 'Click here to copy the tag to your clipboard',
color: 'green'
}
]
}
},
{ text: ' ', color: 'dark_gray' },
chatMessage.MessageBuilder.fromString('&7' + message.content),
attachmentsComponent.length === 0 ? '' : attachmentsComponent
]
bot.tellraw('@a', component)
} catch (e) {
}
}
bot.on('end', () => {
clearInterval(queueInterval)
dcclient.off('messageCreate', handleDiscordMessages)
})
dcclient.on('messageCreate', handleDiscordMessages)
};
module.exports = { inject }