Skip to content

Commit

Permalink
🐛 Fixed Translate and Send using the admin's pfp instead of the OP
Browse files Browse the repository at this point in the history
  • Loading branch information
-ˏˋ Maple ˊˎ committed Nov 29, 2023
1 parent 85aa2a0 commit a6c7627
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 64 deletions.
54 changes: 3 additions & 51 deletions src/bot/events/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion src/bot/interactions/commands/TranslateSend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bot/interactions/commands/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions src/helpers/sendMessageAsUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}));
}

Expand Down

0 comments on commit a6c7627

Please sign in to comment.