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
- 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•9mo ago
Message Not Public
Sign In & Join Server To View
like should i do message.attachments.size > 0
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
ok using essage.attachments.first().url works great however is I tag someone it causes the bot to crash
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
i'm very new to coding and stuff so i apologise
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Cannot read properties of undefined (reading 'url')
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
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)
Codeblocks:
```js
const Discord = require("discord.js");
// further code
```
becomes
Inline Code:
`console.log('inline!');` becomes
console.log('inline!');
thats the full thing. If i tag someone that error happens
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
you wasnt the whole index.js or just this segment?
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
if (message.attachments.first().url){
message.channel.send('@everyone It looks like someone is sending pics again!');
} else{};
});
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
thats a little over my head. what does that mean?
conditional chaining
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
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•9mo ago
Message Not Public
Sign In & Join Server To View
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•9mo ago
Message Not Public
Sign In & Join Server To View
thats why it was crashing when i was just tagging someone
because tagging someone counts as an attachment for some reason
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
well when i would @soandso the bot would send out the message fopr somereason
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View