Skip to content

Commit

Permalink
fix(attachments): use existing attachment for images
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Sep 3, 2023
1 parent caf38ef commit bc58fb2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
12 changes: 7 additions & 5 deletions src/Events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export default {
if (!await checks.execute(message, channelInDb)) return;

message.censored_content = censor(message.content);
const attachment = messageContentModifiers.getAttachment(message);
const attachmentURL = !attachment ? await messageContentModifiers.getAttachmentURL(message) : undefined;
const attachment = message.attachments.first();
const attachmentURL = attachment
? `attachment://${attachment.name}`
: await messageContentModifiers.getAttachmentURL(message);

let replyInDb: messageData | null;
let referredAuthor: User | undefined; // author of the message being replied to
Expand All @@ -54,7 +56,7 @@ export default {
// define censored embed after reply is added to reflect that in censored embed as well
const embed = new EmbedBuilder()
.setDescription(message.content || null) // description must be null if message is only an attachment
.setImage(attachment ? `attachment://${attachment.name}` : attachmentURL || null)
.setImage(attachmentURL)
.setColor(colors('random'))
.setFields(
referredContent
Expand Down Expand Up @@ -104,9 +106,9 @@ export default {
webhookMessage = {
avatarURL: message.author.avatarURL() || message.author.defaultAvatarURL,
username: message.author.username,
files: attachment ? [attachment] : undefined,
content: connection?.profFilter ? message.censored_content : message.content,
embeds: replyEmbed ? [replyEmbed] : undefined,
files: attachment ? [attachment] : [],
threadId: connection.parentId ? connection.channelId : undefined,
allowedMentions: { parse: [] },
};
Expand All @@ -115,9 +117,9 @@ export default {
webhookMessage = {
components: replyButton ? [replyButton] : undefined,
embeds: [connection.profFilter ? censoredEmbed : embed],
files: attachment ? [attachment] : undefined,
username: `${channelInDb.hub?.name}`,
avatarURL: channelInDb.hub?.iconUrl,
files: attachment ? [attachment] : [],
threadId: connection.parentId ? connection.channelId : undefined,
allowedMentions: { parse: [] },
};
Expand Down
13 changes: 2 additions & 11 deletions src/Scripts/message/messageContentModifiers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AttachmentBuilder, Message } from 'discord.js';
import { Message } from 'discord.js';
import { NetworkMessage } from '../../Events/messageCreate';
import 'dotenv/config';

Expand All @@ -14,16 +14,6 @@ export default {
return referredContent;
},

getAttachment(message: NetworkMessage) {
// Attached Images (uploaded without url)
const attachment = message.attachments.first();
if (attachment) {
if (message.attachments.size > 1) message.reply('Due to Discord\'s Embed limitations, only the first attachment will be sent.');

const newAttachment = new AttachmentBuilder(`${attachment.url}`, { name: `${attachment.name}` });
return newAttachment; // return the new attachment buffer so we can send in the network
}
},
async getAttachmentURL(message: NetworkMessage) {
// Tenor Gifs / Image URLs
const imageURLRegex = /(?:(?:(?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)(?:(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)(?:\.jpg|\.jpeg|\.gif|\.png|\.webp)/;
Expand All @@ -42,5 +32,6 @@ export default {

return gifJSON.results[0].media[0].gif.url as string;
}
return null;
},
};

0 comments on commit bc58fb2

Please sign in to comment.