首页 > 解决方案 > 我的 discord.js 命令加载器不工作

问题描述

我的命令加载器不起作用。基本上,它应该从文件夹 ./commands/info 加载所有 .js 文件它所做的是,它说它已经全部加载并准备好了,但是机器人不会在不和谐的情况下上线。我尝试更改令牌、重新混合项目甚至查看代码,但没有任何效果。我在 glitch.me 上托管它。

const express = require("express");
const app = express();

// make all the files in 'public' available
// https://expressjs.com/en/starter/static-files.html
app.use(express.static("public"));

// https://expressjs.com/en/starter/basic-routing.html
app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port + "\nBot is now ready!");
});

// LOAD COMMANDS
const { Client, Collection, Attachment } = require("discord.js");
const { config } = require("dotenv");
const discord = require("discord.js");


const client = new discord.Client();

// Collections
client.commands = new Collection();
client.aliases = new Collection();

config({
    path: __dirname + "/.env"
});

// Run the command loader
["command"].forEach(handler => {
    require(`./handlers/${handler}`)(client);
});

client.on('ready', () => {
  var guilds = [];
    client.guilds.cache.array().forEach( (guild) =>{
        guilds.push(guild.name);
    });

client.on('ready', () => {
  var guilds = [];
    client.guilds.cache.array().forEach( (guild) =>{
        guilds.push(guild.name);
    });

    if(guilds.length > 0){
        console.log("Servers:");
        console.log(guilds.join("\n"));
        console.log();
    }
})


    const prefix = "*";

client.on('message', async (message) => {
    if (message.author.bot) return;
    if (!message.guild) return;
    if (!message.content.startsWith(prefix)) return;

    // If message.member is uncached, cache it.
    if (!message.member) message.member = await message.guild.fetchMember(message);

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



    // Get the command
    let command = client.commands.get(cmd);
    // If none is found, try to find it by alias
    if (!command) command = client.commands.get(client.aliases.get(cmd));

    // If a command is finally found, run the command
    if (command)
        command.run(client, message, args);
});
// -----

client.login(process.env.SECRET);

})

任何帮助都会得到很大的帮助 注意:它没有说任何错误,它只是没有上线

标签: javascriptdiscord.js

解决方案


这就是问题所在。

您将所有代码放入client.on('ready')函数中。

})之后看到了client.login吗?放在})后面guilds.push()

由于 client.login 处于 ready 事件中,因此您在准备好后尝试登录,这是不可能的。


推荐阅读