diff --git a/src/bot/events/messageCreate.js b/src/bot/events/messageCreate.js index 3f6fc2dd..467447a4 100644 --- a/src/bot/events/messageCreate.js +++ b/src/bot/events/messageCreate.js @@ -2,6 +2,7 @@ const { Events } = require('discord.js'); const { getFlagEmoji } = require("../../helpers/getFlagEmoji.js"); const { splitString } = require("../../helpers/splitString.js"); +const { sendMessageAsUser } = require("../../helpers/sendMessageAsUser.js") let sourceChannelIDs = []; @@ -30,9 +31,6 @@ async function replicaHandler(message, replicaChannel) { // if the source and target channels are equals, check if the message is the same as the original or if it's coming from the same language if (replicaChannel.source_channel_id === replicaChannel.target_channel_id && (translated.text === message.content || translated.from === replicaChannel.target_language_code)) return; - // split the message into multiple messages if it's too long - const messages = splitString(translated.text, 2000); - // if possible use webhooks, otherwise use the bot // if the channel is a thread the webhook needs to be created in the parent channel const destinationChannel = message.client.channels.cache.get(replicaChannel.target_channel_id) || await message.client.channels.fetch(replicaChannel.target_channel_id); @@ -42,52 +40,6 @@ async function replicaHandler(message, replicaChannel) { await message.client.db.removeReplicaChannel(replicaChannel.source_channel_id, replicaChannel.target_channel_id, replicaChannel.target_language_code); return; } - - let webhook; - try { - // send a message using the user name and pfp using webhooks - //check if there's a webhook that can be used - webhook = await destinationChannel.fetchWebhooks().then(webhooks => { - return webhooks.find(webhook => webhook.owner.id === message.client.user.id); - }); - - //check if the message webhookid is the same as the bot's - if (webhook && message.webhookId === webhook.id) return; - - if (!webhook) { - //create a webhook - webhook = await destinationChannel.createWebhook({ - name: message.client.user.username, - avatar: message.client.user.displayAvatarURL({format: 'png', dynamic: true}), - reason: "needed for /send to use webhooks instead of the bot" - }); - } - - // send the message - for (let i = 0; i < messages.length; i++) { - await webhook.send({ - content: messages[i], - embeds: message.embeds, - files: message.files, - components: message.components, - username: getFlagEmoji(replicaChannel.target_language_code) + " " + (message.member?.nickname || message.author.globalName || message.author.username), - avatarURL: message.member?.displayAvatarURL({format: 'png', dynamic: true}) || message.user.avatarURL({format: 'png', dynamic: true}), - }); - } - } catch (e) { - try { - // fallback method - for (let i = 0; i < messages.length; i++) { - await destinationChannel.send({ - content: messages[i], - embeds: message.embeds, - files: message.files, - components: message.components, - }); - } - } catch (e) { - // ignore - } - - } + + sendMessageAsUser(message.client, destinationChannel, message.author, translated) } diff --git a/src/bot/interactions/commands/TranslateSend.js b/src/bot/interactions/commands/TranslateSend.js index 49e416c1..150bda10 100644 --- a/src/bot/interactions/commands/TranslateSend.js +++ b/src/bot/interactions/commands/TranslateSend.js @@ -27,7 +27,7 @@ module.exports = { const translated = await interaction.client.translate(interaction.targetMessage.content, interaction.guildLocale); // send the translated message - sendMessageAsUser(interaction, translated); + sendMessageAsUser(interaction.client, interaction.channel, interaction.targetMessage.author, translated); // cancel the interaction response if not already responded diff --git a/src/bot/interactions/commands/send.js b/src/bot/interactions/commands/send.js index 8a075feb..99d3cef0 100644 --- a/src/bot/interactions/commands/send.js +++ b/src/bot/interactions/commands/send.js @@ -41,7 +41,7 @@ module.exports = { const translated = await interaction.client.translate(text, to, from); - const {webhook, sentMessages} = await sendMessageAsUser(interaction, translated); + const {webhook, sentMessages} = await sendMessageAsUser(interaction.client, interaction.channel, interaction.member, translated); // If sentMessages is empty, this means that the messages were sent via text and not webhook so we can end the function there. if (sentMessages.length == 0) { diff --git a/src/helpers/sendMessageAsUser.js b/src/helpers/sendMessageAsUser.js index d87070fd..498715ef 100644 --- a/src/helpers/sendMessageAsUser.js +++ b/src/helpers/sendMessageAsUser.js @@ -2,47 +2,48 @@ const { getFlagEmoji } = require("./getFlagEmoji.js") const { splitString } = require("./splitString.js") const { ChannelType } = require("discord-api-types/v10") -async function sendMessageAsUser(interaction, translation) { +async function sendMessageAsUser(client, channel, member, translation) { const messages = splitString(translation.text, 2000); // if possible use webhooks, otherwise use the bot // if the channel is a thread the webhook needs to be created in the parent channel - const isThread = interaction.channel.type === ChannelType.PublicThread || interaction.channel.type === ChannelType.PrivateThread; - const webhookChannel = isThread ? interaction.channel.parent : interaction.channel; + const isThread = channel.type === ChannelType.PublicThread || channel.type === ChannelType.PrivateThread; + const webhookChannel = isThread ? channel.parent : channel; let webhook; try { // send a message using the user name and pfp using webhooks //check if there's a webhook that can be used webhook = await webhookChannel.fetchWebhooks().then(webhooks => { - return webhooks.find(webhook => webhook.owner.id === interaction.client.user.id); + return webhooks.find(webhook => webhook.owner.id ===client.user.id); }); if (!webhook) { //create a webhook webhook = await webhookChannel.createWebhook({ - name: interaction.client.user.username, - avatar: interaction.client.user.displayAvatarURL({format: 'png', dynamic: true}), + name: client.user.username, + avatar: client.user.displayAvatarURL({format: 'png', dynamic: true}), reason: "needed for /send to use webhooks instead of the bot" }); } } catch (e) { // fallback method - await interaction.reply(messages[0]); + await reply(messages[0]); // loop through the other messages (if any) and send them as follow ups for (let i = 1; i < messages.length; i++) { - await interaction.followUp(messages[i]); + await followUp(messages[i]); } return; } + // send the message let sentMessages = []; for (let i = 0; i < messages.length; i++) { sentMessages.push(await webhook.send({ content: messages[i], - username: getFlagEmoji(translation.to) + " " + (interaction.member.nickname || interaction.user.globalName), - avatarURL: interaction.member.displayAvatarURL({format: 'png', dynamic: true}), - threadId: isThread ? interaction.channel.id : null + username: getFlagEmoji(translation.to) + " " + (member?.nickname || member?.user?.globalName || member?.globalName || member?.username), + avatarURL: member.displayAvatarURL({format: 'png', dynamic: true}), + threadId: isThread ? channel.id : null })); }