Discord.js V14 斜杠命令处理器使用教程
1、项目的目录结构及介绍
v14-slash-command-handler/
├── src/
│ ├── commands/
│ │ └── info/
│ │ └── ping.js
│ ├── index.js
│ ├── config.js
│ ├── package.json
│ ├── .gitignore
│ ├── LICENSE
│ └── README.md
src/
:项目的主要源代码目录。commands/
:存放所有命令的目录。info/
:示例命令目录。ping.js
:示例命令文件。
index.js
:项目的入口文件。config.js
:项目的配置文件。package.json
:项目的依赖和脚本配置文件。.gitignore
:Git忽略文件配置。LICENSE
:项目的许可证文件。README.md
:项目的说明文档。
2、项目的启动文件介绍
index.js
index.js
是项目的入口文件,负责启动和初始化 Discord 机器人。以下是 index.js
的基本结构:
const { Client, GatewayIntentBits } = require('discord.js');
const config = require('./config.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login(config.token);
- 引入
discord.js
库和配置文件。 - 创建一个新的
Client
实例,并设置必要的 intents。 - 监听
ready
事件,当机器人成功登录时输出日志。 - 使用配置文件中的 token 登录 Discord。
3、项目的配置文件介绍
config.js
config.js
是项目的配置文件,包含必要的配置信息,如 Discord 机器人的 token。以下是 config.js
的基本结构:
module.exports = {
token: 'YOUR_DISCORD_BOT_TOKEN',
prefix: '!',
};
token
:Discord 机器人的 token,用于登录。prefix
:命令前缀,用于区分普通消息和命令。
请确保在实际使用时将 YOUR_DISCORD_BOT_TOKEN
替换为你的 Discord 机器人 token。