首页 > 解决方案 > Error when I try to make a !ping command in discord.js

问题描述

I'm trying to make a !ping command but i keep getting this error when I do "node ." in vscode

const client = new Discord.Client(); ^

ReferenceError: Discord is not defined
    at Object.<anonymous> (C:\Users\Aly\Desktop\SpidBot\src\index.js:7:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
    at node:internal/main/run_main_module:17:47

It says "Discord is not defined" which I dont know the meaning of, heres my code:

console.clear();

const Client = require("./Structures/Client.js");

const config = require("./Data/config.json");

const client = new Discord.Client();

client.commands = new Discord.Collection();

const fs = require("fs");

fs.readdirSync("./src/Commands")
    .filter((file) => file.endsWith(".js"))
    .forEach((file) => {
        /**
         * @type {Command}
         */
        const commands = require(`./Commands/${file}`);
        console.log(`Command ${command.name} loaded`);
        client.commands.set(command.name, command);
    });

client.on("ready", () => console.log("SpidBot is online!"));

client.on("messageCreate", (message) => {
    //console.log(message.content);
    //if (message.content == "!Spid") message.reply("Hello!");

    if (!message.content.startsWith(config.prefix)) return;

    const args = message.content.substring(config.prefix.length).split(/ +/);

    const command = client.commands.find((cmd) => cmd.name == args[0]);
});

client.login(config.token);

can someone please explain why this happens and how I can fix it? btw I;m using node v16 (latest rn)

(heres my full folder, if you need it, and yes I DID change token after sending this: https://www.mediafire.com/folder/yrayh49wqgjfv/SpidBot

and the config.json https://www.mediafire.com/file/9gbhb5bazara5mp/config.json/file )

If I should send it using something else please tell me in the replies, Ty in advance

标签: discord.js

解决方案


You need to import discord.js as it is an external library. In your console first you need to do

npm install discord.js@latest

The @latest is to make sure it is v13 as it seems that’s what you want to use

Then to import it, use require.

const Discord = require('discord.js')
const client = new Discord.Client()

Keep in mind you also need to import intents for v13


推荐阅读