如何使用 discord.js 机器人用文件回复消息?

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

提问人:Maxime Kupke 提问时间:10/12/2023 最后编辑:Mr. PolywhirlMaxime Kupke 更新时间:10/12/2023 访问量:43

问:

我尝试制作一个机器人,当您键入“!roll”时,它会选择数组中的随机视频并用它响应消息。

JS 部分有效,但文件未发送。

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.");
    }
});

我期望的是机器人对任何键入“!roll”的人的这种响应:What I expect from the bot

如果您有任何想法,可能会非常有帮助!

javascript 节点 .js 数组 不和谐 不和谐 .js

评论


答:

1赞 Darshan B 10/12/2023 #1

您正在以文件形式发送。 只接受 Attachments 的数组stringfiles

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