Heroku:如何让它访问多个 .js 文件

Posted

技术标签:

【中文标题】Heroku:如何让它访问多个 .js 文件【英文标题】:Heroku: how to have it access multiple .js files 【发布时间】:2021-12-04 15:10:55 【问题描述】:

我在 heroku 上收到此错误消息,我想我是因为 Procfile 引起的。

我目前正在使用 Worker,但我正在尝试弄清楚如何让 heroku 访问 index.js 和 ping.js。除非我完全错误地阅读错误消息,否则这可能是一个不同的问题。任何帮助表示赞赏!

编辑: 这是我的 index.js 代码

const Discord = require('discord.js');
const music = require('@koenie06/discord.js-music');
const fs = require('fs');
const  dir  = require('console');
const bot = new Discord.Client(
    shards: "auto",
    intents: [
        Discord.Intents.FLAGS.GUILDS,
        Discord.Intents.FLAGS.GUILD_MESSAGES,
        Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
        Discord.Intents.FLAGS.DIRECT_MESSAGES,
        Discord.Intents.FLAGS.GUILD_VOICE_STATES
    ]
);

bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();

//Command handler and aliases
fs.readdirSync('./commands/').forEach(dir => 
    //in the commands folder, we gonna check for the category
    fs.readdir(`./commands/$dir`, (err, files) => 
        //console log error(catch error)
        if(err)throw err;
        //checking if the files ends with .js if its a javascript file
        var jsFiles = files.filter(f => f.split('.').pop() === 'js');
        //if there is no commands in the file it will return
        if(jsFiles.length <= 0) 
            console.log("Can't find any commands");
            return;
        

        jsFiles.forEach(file => 
            //console the loaded commands
            var fileGet = require(`./commands/$dir/$file`);
            console.log(`[COMMAND HANDLER] - File $file was loaded`);
            //gonna let the commands run
            try 
                bot.commands.set(fileGet.help.name, fileGet);
                // it search in the commands folder if there is any aliases
                fileGet.help.aliases.forEach(alias => 
                    bot.aliases.set(alias, fileGet.help.name);
                )
             catch(err) 
                //catch error in console
                return console.log(err);
            
        )
    )
)


/**
 * ECHO STUFF
 */


//slash command to echo 
bot.on('ready', async () => 
    bot.user.setPresence( activities: [ name: "Tedi", type: "WATCHING"] );
    console.log("bye");
    const data = 
        name: 'echo',
        description: 'Echo your text',
        options: [
            name: 'text',
            type: 'STRING',
            description: 'The user input',
            required: true,
        ],
    ;
const command = await bot.guilds.cache.get('872986148681703444')?.commands.create(data);
)

bot.on('messageCreate', async message => 
    if(message.author.bot || message.channel.type == 'DM') return

    let prefix = '~'
    let messageArray = message.content.split(' ');
    let cmd = messsageArray[0];
    let args = messageArray.slice(1);

    //it will make the cmd work with his original name and his aliases
    let commands = bot.commands.get(cmd.slice(prefix.length)) || bot.commands.get(bot.aliases.get(cmd.slice(prefix.length)));
    if(commands) 
        if(!message.content.startsWith(prefix)) return
        commands.run(bot, message, args, prefix);
    
)

//interactionCreate for echo slash command
bot.on('interactionCreate', async interaction => 
    /**
     * isButton() used to check if its a button
     * isCommand() used to check if its a slash command
     * isSelectMenu() used to check if its a dropdown menu
     * isMessageComponent()
     */
    if(interaction.isCommand()) 
        if(interaction.commandName === 'echo') 
            const text = interaction.options.getString('text');
            await interaction.reply( content: text, ephemeral: false); //if ephemeral if true, it would make the slash command private
        
    
)

bot.login(process.env.token);

这是我的 ping.js

const Discord = require("discord.js");

module.exports.run = async (Client, message, args, prefix) => 
    message.channel.send("pong")


module.exports.help = 
    name: "ping",
    aliases: ["p"]

【问题讨论】:

这可能不是heroku问题,但如果您发布受影响文件的代码,它将帮助我们解决您的问题。 【参考方案1】:

这个错误不是因为 Heroku,它基本上是因为你的命令处理程序中有一个文件在处理它时没有名称,例如这里的代码:

const Discord = require("discord.js");

module.exports.run = async (Client, message, args, prefix) => 
    message.channel.send("pong")


module.exports.help = 
    // here the name isn't included
    aliases: ["p"]


// so just check if you have a file without a name while handling it and put a name, and if you don't want aliases make it `aliases: []`

此命令处理程序应如下所示Commands 文件夹 commands > 子文件夹,例如审核 > kick.js就是这样,也感谢您观看我的视频,我是 UltraX :)

【讨论】:

以上是关于Heroku:如何让它访问多个 .js 文件的主要内容,如果未能解决你的问题,请参考以下文章

多个帐户heroku键混淆

多个heroku帐户

如何访问上面多个目录的文件(node.js)?

Socket.io 在 Node.js + Redis + Heroku(多个测功机)环境中发出并不总是收到

在Heroku上部署多个模块java应用程序

姜戈。在 Heroku 中使用多个设置文件