简体   繁体   English

有人能解释一下如何在本地单独的文件中有一些命令,然后是 Discord.js 的主要 index.js 吗?

[英]Can someone explain how to have some commands in a seperate file locally then the main index.js for Discord.js?

I have seen people being able to have a index.js and then other files where the commands are defined.我已经看到人们能够拥有一个 index.js 和其他定义命令的文件。 An example:一个例子:

Index.js索引.js

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

//Discord Bot Token
const token = 'token here'
client.login(token);


//Checks if the bot is online
client.once('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

and then a seperate local document "commands.js"然后是一个单独的本地文档“commands.js”

client.on('message', msg => {
 //Makes sure that the bot does not read it's own messages
  if(msg.author.bot) {return;}
  if(msg.content.toLowerCase() === prefix + 'test'){ //Sends the IP
    msg.author.send("test!");
    msg.delete();
}});

Hope someone can help me understand how to do it.希望有人可以帮助我了解如何做到这一点。

You can use modules to import and export files.您可以使用模块来导入和导出文件。 Since you are already using require (Node's way of doing this because ES modules are still experimental ) to import Discord.js, you can import and export like this:由于您已经在使用require (Node 这样做的方式,因为ES 模块仍处于实验阶段)来导入 Discord.js,因此您可以像这样导入和导出:

module1.js

module.exports = {
  a: 'Hello, world!',
  b: 3
};

module2.js

const module1 = require('./module1');
console.log(module1.a); // Hello, world!
console.log(module1.b); // 3

// You can also use destructuring:
const {a, b} = require('./module1');

For more information on Node.js modules you can read the documentation .有关 Node.js 模块的更多信息,您可以阅读文档


In your situation, I recommend reading this guide on setting up a command handler (where commands are stored in separate files).在您的情况下,我建议阅读本指南以设置命令处理程序(命令存储在单独的文件中)。 A basic example:一个基本的例子:

index.js

const {readdirSync} = require('fs');
const {Client, Collection} = require('discord.js');

const prefix = '!';

const client = new Client();
// Create a Collection for the commands
client.commands = new Collection(
  // Get the files in the commands folder
  readdirSync('./commands')
    // Only keep JS files
    .filter(file => file.endsWith('js')
    .map(file => {
      // Import the command
      const command = require(`./commands/${file}`);
      // When creating a collection, you can pass in an iterable (e.g. an array)
      // of [key, value]
      // The commands will be keyed by the command name
      return [command.name, command];
    })
);

client.on('message', message => {
  // Exit if it was sent by a bot
  if (message.author.bot) return;

  // Get the arguments and command name
  const args = message.content.slice(prefix.length).trim().split(/\s+/);
  const command = args.shift();
  // Exit if there isn't a command
  if (!client.commands.has(command)) return;

  // Execute the command
  client.commands.get(command).execute(message, args);
});

const token = 'token here';
client.login(token);

commands/test.js

module.exports = {
  name: 'test',
  execute(message) {
    // '!test' -> 'test'
    message.channel.send('test');
  }
};

commands/args.js

module.exports = {
  name: 'args',
  execute(message, args) {
    // '!args abc 123' -> 'Arguments: abc, 123'
    message.channel.send(`Arguments: ${args.join(', ')}`);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM