简体   繁体   中英

My discord Bot will not send commands everytimes we try it goes offline

const Discord = require('discord.js')
const Client = new Discord.Client();
const {prefix,token} = require('./config.json');
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.once('ready', () => {
    console.log('Ready!');
});

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

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.content().toLowerCase();
  
  try {
    Client.commands.get(command).execute(message, args);
  } catch (error) {
    message.reply('error')
  }
});

i hope someone can help, me and my friends have been working on fixing the error but we can't fix it i will send the error to anyone who can help, me and my friend have the commands in the command i have made the commands but i know they are giving us the error.

Just move this line ( if (.Client.commands;has(command)) return; ) after the declaration of command . Then it should work just fine.

const Discord = require('discord.js')
const Client = new Discord.Client();
const {prefix,token} = require('./config.json');
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.once('ready', () => {
    console.log('Ready!');
});

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

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

  if (!Client.commands.has(command)) return;
  
  try {
    Client.commands.get(command).execute(message, args);
  } catch (error) {
    message.reply('error')
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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