Search code examples
javascriptnode.jsarraysdiscorddiscord.js

How to reply to a message with a file using discord.js bot?


I try to make a bot that when you type '!roll' it selects a random videos in an array and respond to the message with it.

The JS part works but the files is not send.

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
    ],
});

const prefix = '!';
const jets = [
    "assets/01.mp4",
    "assets/02.mp4",
    "ETC..."
]

client.on("ready", () => {
    console.log("Prêt à lancer les dés !");
});

client.on("messageCreate", message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    console.log(message.author.username + " a utilisé : " + prefix+command);
    if(command  === "roll") {
        const random = Math.floor(Math.random() * jets.length);
        message.reply("Dés lancés :", {files: [jets[random]]});
    }
    else if(command === "help") {
        message.reply("Utilise " + prefix + "roll pour lancer les dés.");
    }
});

What I expected is this type of response from the bot to anyone who type '!roll' :What I expect from the bot

If you have any idea, it might be very helpful!


Solution

  • You're sending a string as a file. files only accepts an array of Attachments

    message.reply({ 
        content: "Dés lancés :", 
        files: [{ 
            name: 'file.mp4', 
            attachment: jets[random] // path or buffer
        }] 
    });