Javascript Discord.js机器人正在工作,添加了一个额外的命令,所有东西都坏了。有人能帮我找出原因吗?

Javascript Discord.js机器人正在工作,添加了一个额外的命令,所有东西都坏了。有人能帮我找出原因吗?,javascript,node.js,sqlite,discord,discord.js,Javascript,Node.js,Sqlite,Discord,Discord.js,试图冒险进入制造不和谐机器人的领域。接下来是一个相当简单的教程,一路上调整它以适应我试图制作的内容。这个机器人最初是工作的,但我回去添加了“错误”命令,突然它不工作了。我几乎在所有地方都添加了console.log,试图弄清楚一切进展如何 当我启动机器人时,它会弹出“机器人在线”日志。当我输入命令时,它会弹出“Commands”日志,但它根本不会注册命令。我试着寻找任何小的打字错误,缺少括号,等等。。。但我似乎不知道出了什么问题。我希望这里有人能帮忙!谢谢大家! const Discord =

试图冒险进入制造不和谐机器人的领域。接下来是一个相当简单的教程,一路上调整它以适应我试图制作的内容。这个机器人最初是工作的,但我回去添加了“错误”命令,突然它不工作了。我几乎在所有地方都添加了console.log,试图弄清楚一切进展如何

当我启动机器人时,它会弹出“机器人在线”日志。当我输入命令时,它会弹出“Commands”日志,但它根本不会注册命令。我试着寻找任何小的打字错误,缺少括号,等等。。。但我似乎不知道出了什么问题。我希望这里有人能帮忙!谢谢大家!

const Discord = require('discord.js');
const config = require('./config.json');

const client = new Discord.Client();

const SQLite = require('better-sqlite3');
const sql = new SQLite('./scores.sqlite');

client.on('ready', () => {
  console.log('Bot Online');
  const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'goals';").get();
  if (!table['count(*)']) {
    sql.prepare('CREATE TABLE goals (id TEXT PRIMARY KEY, user TEXT, guild TEXT, goals INTEGER);').run();
    sql.prepare('CREATE UNIQUE INDEX idx_goals_id ON goals (id);').run();
    sql.pragma('synchronous = 1');
    sql.pragma('journal_mode = wal');
  }

  //Statements to get and set the goal data
  client.getGoals = sql.prepare('SELECT * FROM goals WHERE user = ? AND guild = ?');
  client.setGoals = sql.prepare('INSERT OR REPLACE INTO goals (id, user, guild, goals) VALUES (@id, @user, @guild, @goals);');
});

client.on('message', (message) => {
  if (message.author.bot) return;
  let goalTotal;
  if (message.guild) {
    goalTotal = client.getGoals.get(message.author.id, message.guild.id);
    if (!goalTotal) {
      goalTotal = {
        id: `${message.guild.id}-${message.author.id}`,
        user: message.author.id,
        guild: message.guild.id,
        goals: 0,
      };
    }
  }

  if (message.content.indexOf(config.prefix) !== 0) return;

  const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  const command = args.shift().toLowerCase();
  console.log('Commands');

  if (command === 'Goals') {
    console.log('Goals');
    return message.reply(`You Currently Have ${goalTotal.goals} Own Goals.`);
  }

  if (command === 'OwnGoal') {
    console.log('Own Goal');
    const user = message.mentions.users.first() || client.users.cache.get(args[0]);
    if (!user) return message.reply('You must mention someone.');
    let userscore = client.getGoals.get(user.id, message.guild.id);

    if (!userscore) {
      userscore = {
        id: `${message.guild.id}-${user.id}`,
        user: user.id,
        guild: message.guild.id,
        goals: 0,
      };
    }
    userscore.goals++;
    console.log({ userscore });
    client.setGoals.run(userscore);

    return message.channel.send(`${user.tag} has betrayed his team and now has a total of ${userscore.goals} own goals.`);
  }

  if (command === 'Mistake') {
    console.log('Mistake');
    const user = message.mentions.users.first() || client.users.cache.get(args[0]);
    if (!user) return message.reply('You must mention someone.');
    let userscore = client.getGoals.get(user.id, message.guild.id);

    if (!userscore) {
      return message.reply('This person has no Rocket Bot activity.');
    }
    if (userscore === 0) {
      return message.reply('This player currently has no goals.');
    }
    if (userscore > 0) {
      userscore.goals--;
    }
    console.log({ userscore });
    client.setGoals.run(userscore);

    return message.channel.send(`${user.tag} was falsely accused and now has a total of ${userscore.goals} own goals.`);
  }

  if (command === 'Leaderboard') {
    console.log('Leaderboard');
    const leaderboard = sql.prepare('SELECT * FROM goals WHERE guild = ? ORDER BY goals DESC;').all(message.guild.id);
    const embed = new Discord.MessageEmbed()
      .setTitle('Rocket Bot Leaderboard')
      .setAuthor(client.user.username, client.user.avatarURL())
      .setDescription('Total Goals Scored Against Own Team:')
      .setFooter('Rocket Bot')
      .setThumbnail('https://imgur.com/a/S9HN4bT')
      .setColor('0099ff');

    for (const data of leaderboard) {
      embed.addFields({
        name: client.users.cache.get(data.user).tag,
        value: `${data.goals} goals`,
        inline: true,
      });
    }
    return message.channel.send({ embed });
  }

  if (command === 'RocketHelp') {
    console.log('Help');
    return message.reply(
      'Rocket Bot Commands:' +
        '\n' +
        '!Goals - See How Many Goals You Have Scored Against Your Own Team' +
        '\n' +
        '!OwnGoal - Tag Another Player With @ To Add One To Their Total' +
        '\n' +
        '!Mistake - Tag Another Player With @ To Subtract One From Their Total' +
        '\n' +
        '!Leaderboard - Show The Current Leaderboard'
    );
  }
});

client.login(config.token);

您未正确拆分邮件内容。您意外地将
g
添加到正则表达式中。 正确的行:

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

由于不正确的参数拆分,它根本找不到任何命令,因此在
命令

我做了您建议的更改后,没有调用控制台日志,但我仍然有同样的问题,它不会注册任何命令,但每次我尝试时它仍然会吐出控制台.log('Commands')。@KirkForADay啊,我明白了。您的命令是
const command=args.shift().toLowerCase(),尽管您正在将其与具有大写字母的字符串进行比较。将if语句修改为完全小写。如果有帮助,请通知我。这完全是问题所在。和往常一样,有些事情很容易被忽视。我真是太感谢你了,我已经浪费了太多的时间试图弄明白为什么这不起作用。