chomens-bot-js/plugins/discord.js

115 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-11-16 06:41:30 -05:00
/* eslint-disable require-jsdoc */
2022-11-11 07:03:44 -05:00
/* eslint-disable max-len */
const {escapeMarkdown} = require('../util/escapeMarkdown');
2022-11-11 07:03:44 -05:00
async function inject(bot, dcclient, config, rl) {
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host]);
let queue = '';
const queueInterval = setInterval(() => {
if (queue === '') return;
2022-11-11 07:03:44 -05:00
channel.send({
content: '```ansi\n' + queue.substring(0, 1986) + '\n```',
allowedMentions: {
parse: [],
},
});
queue = '';
}, 1000);
bot.on('parsed_chat', (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;
});
2022-11-11 07:03:44 -05:00
// handle discord messages!!!
async function handleDiscordMessages(message) {
2022-11-11 07:03:44 -05:00
// 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) {
return;
}
}
bot.once('end', () => {
clearInterval(queueInterval);
dcclient.off('messageCreate', handleDiscordMessages);
2022-11-11 07:03:44 -05:00
});
dcclient.on('messageCreate', handleDiscordMessages);
};
module.exports = {inject};