首页 > 解决方案 > 命令不是函数

问题描述

大家下午好

希望这里的人可以提供帮助,很多网络拖网,我无法弄清楚为什么我的应用程序崩溃了,

如果我在已知命令中执行命令,只要我执行不存在的命令,应用程序就会崩溃。

无论我尝试使用 else 语句做什么,应用程序都会崩溃。

任何帮助将不胜感激。

谢谢

// Register our event handlers (defined below):
client.on("message", onMessageHandler);
client.on("connected", onConnectedHandler);
client.on("disconnected", onDisconnectedHandler);

// Connect to Twitch:
client.connect();

 // Called every time a message comes in:
function onMessageHandler(target, context, msg, self) {
  if (self) {
    return;
  } // Ignore messages from the bot

    // This isn't a command since it has no prefix:
  if (msg.substr(0, 1) !== commandPrefix) {
    console.log(`[${target} (${context["message-type"]})] ${context.username}: ${msg}`)
    return
  }

  // Split the message into individual words:
  const parse = msg.slice(1).split(" ");
  // The command name is the first (0th) one:
  const commandName = parse[0];
  // The rest (if any) are the parameters:
  const params = parse.splice(1);
  
  // If the command is known, let's execute it:
  if (commandName in knownCommands || knownCommandsMod) {
    // Retrieve the function by its name:
    const command = knownCommands[commandName] || knownCommandsMod[commandName]
    // Then call the command with parameters:
    command(target, context, params)
    console.log(`* Executed ${commandName} command for ${context.username}`)
  } else {
    console.log(`* Unknown command ${commandName} from ${context.username}`)
  
  }
}

标签: javascriptchatbot

解决方案


推荐阅读