需要定义消息参数的帮助,不确定出什么问题吗?

时间:2019-04-08 20:45:33

标签: javascript node.js

我一直在研究一个简单的帮助命令,当放置命令的参数时,该命令应该对某些命令提供深入的帮助,如果不放置该命令,则应该抛出一个常规命令列表。

功能片段:

function help(helpcomm) {

	if (helpcomm == "say") {
		message.channel.send("Say command makes me repeat the words you type. EG. if you type `p!say Hello World!` I'm going to delete your message and say `Hello world!`.");
	} else
	if (helpcomm == "ping") {
		message.channel.send("Ping command checks the latency between me, the node I'm hosted on and the API latency.");
	} else
	if (helpcomm == "purge") {
		message.channel.send("Purge command deletes a number of latest messages you choose from 2 to 99. EG. if you type `p!purge 20` I will delete the last 20 messages.");
	} else
	if (helpcomm == "joke") {
		message.channel.send("Joke sends a random joke from my database.");
	} else
	if (helpcomm == "roll") {
		message.channel.send("Roll makes me roll a dice from 1 to 20 sides. Basically a random number generator.");
	} else
	if (helpcomm == "creator") {
		message.channel.send("Gives info about my creator. Currently outdated.");
	} else
	message.channel.send("For specific command help type: `p!help [command]` \nHere are my commands: `p!say [words], p!ping, p!purge [number], p!joke, p!roll [number], p!creator`");
}

应该带有参数的命令:

if (command === "help") {
  	let text = args.join(' ');
  	await message.channel.send(help(text));
  }

我可以在不引发任何错误的情况下对机器人进行结点操作,但是当我实际在命令中输入带或不带参数的前缀时,会引发错误,提示“消息未定义”。

如果您能解释我做错了什么以及如何解决它,那真是太棒了,不需要用汤匙喂食,这将非常有帮助。 如果我需要提供任何其他信息,我会将其添加到评论中。

1 个答案:

答案 0 :(得分:0)

该错误表明帮助功能范围内不存在message变量。如果要在帮助功能内调用message.channel.send,则需要将消息传递给它:

function help (helpComm, message) {
  if (helpcomm == "say") {
    return message.channel.send("Say command makes me repeat the words you type. [etc]');
  }
  //...
  return message.channel.send("For specific command help [etc]")
}

// called like:

client.on('message', async function(message) {
  //...
  if (command === "help") {
    let text = args.join(' ');
    await help(text, message);
  }
});

尽管如此,但我不清楚这就是您要执行的操作,因为此行已经在调用message.channel.send,并且显然希望得到帮助以返回字符串:

await message.channel.send(help(text));

如果只需要帮助来生成字符串,则无需传递消息给它,因为只有主代码需要与消息对象进行交互:

function help (helpComm) {
  if (helpcomm == "say") {
    return "Say command makes me repeat the words you type. [etc]';
  }
  // ...
  return "For specific command help [etc]";
}

// called like:

client.on('message', async function(message) {
  //...
  if (command === "help") {
    let text = args.join(' ');
    await message.channel.send(help(text));
  }
});