如何将这些命令放入命令处理程序?

时间:2020-09-11 09:01:01

标签: javascript node.js discord discord.js

我刚刚完成了该命令,并且想为此命令创建命令处理程序,但是我不确定如何执行。我只希望有人告诉我应该在命令处理程序文件和主文件中写什么。

这是我的代码:

const fs = require('fs');
const Discord = require('discord.js');

const prefix = 's!';

const { MessageEmbed } = require('discord.js');

const client = new Discord.Client();

client.on('ready', () => {
 console.log('ChatSlowMode is Online!');
 client.user.setActivity('s!help | s!invite', {
  type: 'PLAYING',
 });
});

client.on('message', (message) => {
 // Check if message doesn't starts with `prefix` instead, or it will stop your code.
 if (!message.content.startsWith(prefix) || message.author.bot) return;

 const command = message.content
  .slice(prefix.length)
  .toLowerCase()
  .split(' ')[0]
  .toLowerCase();

 const args = message.content
  .slice(prefix.length)
  .split(' ')
  .slice(1);

 const embedOne = new MessageEmbed()
  .setColor('RED')
  .setTitle("You don't have access to this command!")
  .setFooter("You can't access this Moderation Commands");

 const embedTwo = new MessageEmbed()
  .setColor('RED')
  .setTitle('Please specify a time in seconds')
  .setFooter('You need to specify a time.');

 const embedThree = new MessageEmbed()
  .setColor('RED')
  .setTitle(
   "That's not a number, please specify a time in seconds. `Example: 5`"
  )
  .setFooter("Only Type the number, don't add anything after.");

 const embedFour = new MessageEmbed()
  .setColor('RED')
  .setTitle('Invalid Number! Number must be less than `21600`')
  .setFooter('Time must be under 21600 seconds.');

 const embedFive = new MessageEmbed()
  .setColor('GREEN')
  .setTitle(`Success! Slowmode has been Set to **${args[0]}** seconds!`)
  .setFooter('ChatSlowMode Made By Vuke#0888.');

 if (command === 'slowmode' || command === 'sm') {
  if (!message.member.hasPermission('MANAGE_CHANNELS'))
   return message.channel.send(embedOne);

  // Checks if `args[0]` doesn't exist or isn't a number.
  if (!args[0]) return message.channel.send(embedTwo);
  if (isNaN(args[0])) return message.channel.send(embedThree);

  // Check if `args[0]` parsed into an interger is included in `validNumbers`
  if (args[0] > 21600) return message.channel.send(embedFour);

  // Set the slowmode
  if (args[0] < 21600)
   return (
    message.channel.setRateLimitPerUser(args[0]) &&
    message.channel.send(embedFive)
   );
 }
});

client.login('');

1 个答案:

答案 0 :(得分:0)

我将通过一个简单的示例向您展示

您的主文件可能看起来像这样(只需添加您在那里没有的文件即可):

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '&';

const version = '1.1';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);
 
    client.commands.set(command.name, command);
}

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    };
}

现在,您必须有一个名为commands的文件夹,不能将其放入任何其他文件夹中。 现在,您的命令文件应该位于commands文件夹中,并且看起来应该像这样:

module.exports = {
    name: 'ping',
    description: "The bot will respond with pong",
    execute(message, args){
        message.channel.send(`pong!`);
    }
}

对于每个新命令,您将添加如下内容:

if(command === '{name}'){ // so if you do &ping (& is the prefix here), it will execute the command named 'ping'
        client.commands.get('{commandName}').execute(message, args);
    };

{commandName}是这个东西:命令文件中的name: 'ping',。如果您不明白这一点,建议您基本上检查一下任何YouTube教程,我都使用过以下教程: https://www.youtube.com/watch?v=AUOb9_aAk7U

如果这篇文章对您有帮助,请告诉我:)