How do I make my discord bot send an attachment when certain word is sent

Viewed 1200

I'm new to working with discord bot and decided to try it out, to start I just wanted to make the bot send an attachment (image, video, etc) when for example, "sendpicture" is written in chat.

I've changed the code several times yet I get the same error everytime, "Attachment is not a constructor" or "Discord.Attachment is not a constructor".

My current code looks like this:


const client = new Discord.Client();

client.once(`ready`, () => {
    console.log("online");
});

const PREFIX = "!"

//replying with a text message
client.on('message', msg => {
    if (msg.content === 'test1') {
      msg.channel.send('working');
    }
  });
  
//replying with attachment
client.on("message", function(message){
   
    let args = message.content.substring(PREFIX.lenght).split(" ");

    switch(args[0]) { 
        case "test2": 
            message.channel.send(new Discord.Attachment(`.a/this/bestpic.png`, `bestpic.png`) )
            .then(msg => {
                //aaaaaaaa
            })
            .catch(console.error);
            break;
    }
})

tyia

4 Answers

Had you tried looking at the official documentation ?

I don't think you should use new Discord.Attachment(), try this instead :

switch(args[0]) { 
    case "test2": 
        message.channel.send({
            files: [{
                attachment: '.a/this/bestpic.png',
                name: 'bestpic.png'
            }]
        }).then(msg => {
                //aaaaaaaa
        }).catch(console.error);
    break;
}

Discord.Attachment() is not a thing, i believe the answer you are looking for is this :)

new Discord.MessageAttachment()

Try this code, short and simple to use.

if (message.content.toLowerCase() === 'word') { //only word, without prefix
        message.channel.send({ files: ['./path_to_your_file'] })
    }

You should try using MessageAttachment because Discord.Attachment is not a thing, then add the attachment into the files property

I would consider making the attachment a variable instead of putting it directly into the function, it is optional tho

const { MessageAttachment } = require("discord.js");

const attachment = new MessageAttachment("url", "fileName(optional)", data(optional));

message.channel.send({ files: [attachment] }).then(//code...)

Let me know if it works

Related