discord.js - 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
解决方案
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
推荐阅读
- laravel - laravel 通知插入错误的表列
- c++ - 错误:使用复制和交换习语的交换函数中“operator=”的重载不明确
- javascript - 创建要分享的列表?
- google-cloud-platform - 解压 Google Storage Bucket 中的文件
- amazon-web-services - AWS Glue - 将 fileld 添加到结构字段
- php - 如何在laravel中获取双重请求名称到数组?
- google-people-api - Google people API 我无法获取电子邮件地址
- java - 对于我们重新分配变量的循环和片段
- facebook - DialogFlow - 来自 Messenger Facebook 的用户共享位置
- powershell - 如何获取包含 system.array 类型数据的文件并使用 PowerShell 对其进行排序?