Make a Discord bot reply when a user sends an image in chat (continued)

I have a discord bot up and running and working on slash commands. But I'm having trouble getting it to reply when someone sends an image. I posted this yesterday where I couldnt get the discord bot to respond if someone uploaded an image. A member of the discord helped me out(thank you again). however, Ive run into another issue. My bot cannot distinguish the type of attachment sent. If I tag someone in a message: i.e. "@someone what's up" the bot responds. I only want the bot to respond if a .png or a .jpeg is sent into the channel. currently my code looks like this: if (message.attachments){ message.channel.send('everyone It looks like someone is sending something you like 😉 '); } If you can help i'd be in debt to you forever!!
28 Replies
d.js toolkit
d.js toolkit•3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
like should i do message.attachments.size > 0
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
ok using essage.attachments.first().url works great however is I tag someone it causes the bot to crash
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
i'm very new to coding and stuff so i apologise
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
Cannot read properties of undefined (reading 'url')
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
TypeError: Cannot read properties of undefined (reading 'url') at Client.<anonymous> (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\src\index.js:38:34) at Client.emit (node:events:519:28) at MessageCreateAction.handle (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:28:14) at module.exports [as MESSAGE_CREATE] (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31) at WebSocketManager.<anonymous> (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12) at WebSocketManager.emit (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:282:31) at WebSocketShard.<anonymous> (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules@discordjs\ws\dist\index.js:1173:51) at WebSocketShard.emit (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:282:31) at WebSocketShard.onMessage (C:\Users\bmill\OneDrive\Documents\Code\DiscordBot\node_modules@discordjs\ws\dist\index.js:988:14)
d.js docs
d.js docs•3mo ago
Codeblocks: ```js const Discord = require("discord.js"); // further code ``` becomes
const Discord = require("discord.js");
// further code
const Discord = require("discord.js");
// further code
Inline Code: `console.log('inline!');` becomes console.log('inline!');
Arkst0rm
Arkst0rm•3mo ago
thats the full thing. If i tag someone that error happens
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
you wasnt the whole index.js or just this segment?
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
if (message.attachments.first().url){ message.channel.send('@everyone It looks like someone is sending pics again!'); } else{}; });
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
thats a little over my head. what does that mean? conditional chaining
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
i did this if (message.attachments.size && message.attachments.first().url){ message.channel.send('@everyone It looks like someone is sending pics again!'); } else{}; it works perfectly
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
lol i just did the other code the message.attachments.first()?.url it works too but i dont understand how. can you explain that? if you dont mind
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
thats why it was crashing when i was just tagging someone because tagging someone counts as an attachment for some reason
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View
Arkst0rm
Arkst0rm•3mo ago
well when i would @soandso the bot would send out the message fopr somereason
Unknown User
Unknown User•3mo ago
Message Not Public
Sign In & Join Server To View