Bot 循环遍历 mp4 特定文件的目录

时间:2021-03-29 04:41:38

标签: node.js discord.js

我正在尝试制作一个机器人,它可以使用特定命令上传 mp4 附件。我想知道的是,是否有一种方法可以让机器人在特定文件的目录中循环,而不是我编写 600 行代码。

const fs = require('fs');
const { Client, MessageAttachment } = require('discord.js');
const config = require('./config.json');
const { prefix, token } = require('./config.json');
const client = new Client();
const { MessageEmbed } = require('discord.js');
client.commands = new Discord.Collection();



const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}


client.on('ready', () => {
  console.log(`${client.user.tag} is now online!`);
  client.user.setPresence({status: 'idle'});
  client.user.setActivity('My Stokeley Playlist ?', { type: 'LISTENING' })
});

client.on('message', (message) => {
  const { content, author, channel } = message
  if (author.bot) {
    return
  }

  const embeds = {
    [`${prefix}snip kratos`]: {
      title: 'Kratos',
      attachmentPath: './snippets/kratos/Kratos.mp4',
    },
    [`${prefix}snip johnny bravo`]: {
      title: 'Johnny Bravo',
      attachmentPath: './snippets/Johnny_Bravo/Johnny_Bravo.mp4',
    },
  }

  const embed = embeds[content]
  if (embed) {
    const { title, attachmentPath } = embed

    channel.send(
      new MessageEmbed()
        .setColor('#ffb638')
        .setTitle(title)
        .setDescription('Sending 1 snippet...')
        .setTimestamp()
        .setFooter('SkiBot')
    )
    channel.send(new MessageAttachment(attachmentPath))
  }
})

here's my current code, I'm trying to find an easier and cleaner way to go about this.

1 个答案:

答案 0 :(得分:0)

我想你已经在代码的一部分中使用了这个

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

试试这个

const musicFiles = fs.readdirSync('./snippets/kratos').filter(file => file.endsWith(".mp4"));