我会先说我是 Javascript 新手,我的大部分经验都是 C# 和 C++,但总的来说我还是有点生疏。我正在学习 Javascript 和 Discord js,尝试制作一个可以存储和列出恐龙统计数据的机器人,以便可以跟踪它们的繁殖情况,这些数据可以通过斜杠命令输入,也可以通过附件输入(如果可能)。 (《方舟:生存登高》发布时的个人小项目哈哈)
我正在关注 https://discordjs.guide/sequelize/#syncing-the-model ,它解释了将我的所有事件和命令移动到它们自己的单独的 .js 以保留我的主索引是一个很好的做法.js 更有组织性。所以我也跟着这么做了。然而,在使用 Sequelize 时,它在同一个 index.js 中演示了所有这些,而不是利用我现在为每个命令和事件设置的模块或其他 .js 文件。
如果我完全遵循并在同一个 index.js 中执行此操作,它就会起作用。但是,如果我尝试将其集成到其他 .js 中,它没有定义,并且我不确定需要包含或要求什么才能使其正常工作。
这是我的index.js
const fs = require('node:fs');
const Sequelize = require('sequelize');
const path = require('node:path');
const keep_alive = require("./keep_alive.js");
const { Client, Collection, Events, GatewayIntentBits, EmbedBuilder, PermissionsBitField, Permissions, SlashCommandBuilder} = require('discord.js');
const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages]});
client.cooldowns = new Collection();
client.commands = new Collection();
// Secret values
const token = process.env['token']
const CLIENT_ID = process.env['client_id']
const GUILD_ID = process.env['guild_id']
// Setting up database
const sequelize = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: false,
// SQLite only
storage: 'database.sqlite',
});
// Setting up tags in database
const Tags = sequelize.define('tags', {
name: {
type: Sequelize.STRING,
unique: true,
},
... (other tags) ...
});
// Reads the commands in the commands folder.
client.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Reads the events in the events folder.
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
Tags.sync(); //This should be inside the ready.js event, not called here.
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
client.login(token);
这是ready.js,当机器人准备好时应该只调用一次事件。
const { Client, Events, GatewayIntentBits } = require('discord.js');
//What am I missing??
module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
Tags.sync(); //This is causing an error. This should be inside the ready.js event, but it keeps saying Tags is not defined.
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
这是错误:
/home/runner/BreedBotjs/events/ready.js:10
Tags.sync(); //This is causing an error. This should be inside the ready.js event, but it keeps saying Tags is undefined.
^
ReferenceError: Tags is not defined
at Object.execute (/home/runner/BreedBotjs/events/ready.js:10:5)
at Client.<anonymous> (/home/runner/BreedBotjs/index.js:93:48)
at Object.onceWrapper (node:events:628:26)
at Client.emit (node:events:513:28)
at WebSocketManager.triggerClientReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:388:17)
at WebSocketManager.checkShardsReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:371:10)
at WebSocketShard.<anonymous> (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:201:16)
at WebSocketShard.emit (node:events:513:28)
at WebSocketShard.checkReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketShard.js:181:12)
at WebSocketShard.gotGuild (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketShard.js:155:10)
Node.js v18.16.1
我想我错过了一些非常简单的东西。某种导入或要求。
我尝试过做类似的事情 const { 标签 } = require('index.js');
但是我遇到了另一个错误:
/home/runner/BreedBotjs/events/ready.js:10
this.Tags.sync(); //This is causing an error. This should be inside the ready.js event, but it keeps saying Tags is undefined.
^
TypeError: Cannot read properties of undefined (reading 'sync')
at Object.execute (/home/runner/BreedBotjs/events/ready.js:10:15)
at Client.<anonymous> (/home/runner/BreedBotjs/index.js:93:48)
at Object.onceWrapper (node:events:628:26)
at Client.emit (node:events:513:28)
at WebSocketManager.triggerClientReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:388:17)
at WebSocketManager.checkShardsReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:371:10)
at WebSocketShard.<anonymous> (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketManager.js:201:16)
at WebSocketShard.emit (node:events:513:28)
at WebSocketShard.checkReady (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketShard.js:181:12)
at WebSocketShard.gotGuild (/home/runner/BreedBotjs/node_modules/discord.js/src/client/websocket/WebSocketShard.js:155:10)
Node.js v18.16.1
您正在尝试在尚未声明或要求的其他文件中访问
Tags
。
您需要将其从您的
index.js
导出,以便您可以在其他文件中使用它。
有几种方法可以解决这个问题,例如将其添加到index.js中的
client.once
中:
client.once(event.name, (...args) => event.execute(...args, Tags));
// in ready.js add Tags to your execute()
module.exports = {
name: Events.ClientReady,
once: true,
execute(client, Tags) {
// rest of your code
}
或将其添加到您的
client
:
// in your index.js
client.Tags = Tags;
// in your ready.js
const Tags = client.Tags;
或导出它:
// in index.js
module.exports = {
Tags
}
// in ready.js
module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
const tagsData = require("path_to_your_index.js");
const Tags = tagsData.Tags;
Tags.sync();
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};