创建一个帮助命令,为每个其他命令发送不同的消息
Create a help command that sends a different message for every other command
我需要帮助来描述我的命令。
目前我正在为每个命令手动制作一个嵌入,如果你有 50 多个命令,这会花费很多时间。我见过这样的事情:
exports.help = {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
};
如何将其实现到嵌入中?它应该是这样的:
我输入什么得到它rhelp help
因此请确保您拥有正确的对象:
var help = {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
};
让我们把上面的变成一个嵌入。
可能最好的方法是:
function turnToEmbed(object) {
return new Discord.RichEmbed()
.setColor("RANDOM")
.setTitle("Some handy dandy info on: "+object.title)
.addField("Description:",object.description,true)
.addField("Usage:",object.usage,true)
.setFooter("And voila :P");
}
message.channel.send({ embed: turnToEmbed(help) });
注意:如果我误解了你想知道如何得到正确的对象,请告诉我。
看来我理解错了问题。
您想知道如何获得该对象。
好吧,有几种方法可以做到这一点。
假设 help.js
是您的示例对象
在 test.js
中是另一个对象 稍微 改变了。
这就是我要进行的方式。
方法“预加载”
当我使用这种方法时,是因为我有 15 个或更多的命令。
在每个命令中都有一个像这样的对象
module.exports = {
help: {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
},
run: function(args){
//Run the "help" command
}
}
并且每个命令都有上面的内容。所有信息和 .run()
;
那么在一个新文件中我通常称之为 command_manager.js
因为它管理所有命令。
所以在主文件中我通常检查消息是否以前缀开头,然后将其传递给command_manager
但有时我让命令管理器处理那。但重要的是 command_manager 有一个 .load()
会在机器人打开时被调用。
var prefix = "r";
var filesToLoad = ["help","test"];
module.exports = {
load:function(){
for(var i =0;i<filesToLoad.length;i++){
var file = require(fileToLoad[i]+".js");
//some code to make sure file is correct command.
this[fileToLoad[i]] = file;
}
}
runCommand:function(message){
var split = message.content.toLowerCase().split(" ");
split[0].substring(prefix.length,split[0].length);
var commandName = split.shift();
switch(commandName){
case "help": this.help.run(message,split,this);break;
case "test": this.test.run();break;
}
}
}
现在命令管理器在 help.js
中工作
.run
函数需要像这样:
function(message,args,cmdManager){
if(cmdManager[args[0]] != null){
//using my function from my old answer
turnToEmbed(cmdManager[args[0].help]);
}
}
我会提供其他方法。但在我看来,它们并没有那么好,这个答案变得很长。
我需要帮助来描述我的命令。
目前我正在为每个命令手动制作一个嵌入,如果你有 50 多个命令,这会花费很多时间。我见过这样的事情:
exports.help = {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
};
如何将其实现到嵌入中?它应该是这样的:
我输入什么得到它rhelp help
因此请确保您拥有正确的对象:
var help = {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
};
让我们把上面的变成一个嵌入。 可能最好的方法是:
function turnToEmbed(object) {
return new Discord.RichEmbed()
.setColor("RANDOM")
.setTitle("Some handy dandy info on: "+object.title)
.addField("Description:",object.description,true)
.addField("Usage:",object.usage,true)
.setFooter("And voila :P");
}
message.channel.send({ embed: turnToEmbed(help) });
注意:如果我误解了你想知道如何得到正确的对象,请告诉我。
看来我理解错了问题。 您想知道如何获得该对象。 好吧,有几种方法可以做到这一点。
假设 help.js
是您的示例对象
在 test.js
中是另一个对象 稍微 改变了。
这就是我要进行的方式。
方法“预加载”
当我使用这种方法时,是因为我有 15 个或更多的命令。
在每个命令中都有一个像这样的对象
module.exports = {
help: {
name: 'help',
description: 'Shows all the commands in the bot',
usage: 'rhelp',
inHelp: 'yes'
},
run: function(args){
//Run the "help" command
}
}
并且每个命令都有上面的内容。所有信息和 .run()
;
那么在一个新文件中我通常称之为 command_manager.js
因为它管理所有命令。
所以在主文件中我通常检查消息是否以前缀开头,然后将其传递给command_manager
但有时我让命令管理器处理那。但重要的是 command_manager 有一个 .load()
会在机器人打开时被调用。
var prefix = "r";
var filesToLoad = ["help","test"];
module.exports = {
load:function(){
for(var i =0;i<filesToLoad.length;i++){
var file = require(fileToLoad[i]+".js");
//some code to make sure file is correct command.
this[fileToLoad[i]] = file;
}
}
runCommand:function(message){
var split = message.content.toLowerCase().split(" ");
split[0].substring(prefix.length,split[0].length);
var commandName = split.shift();
switch(commandName){
case "help": this.help.run(message,split,this);break;
case "test": this.test.run();break;
}
}
}
现在命令管理器在 help.js
中工作
.run
函数需要像这样:
function(message,args,cmdManager){
if(cmdManager[args[0]] != null){
//using my function from my old answer
turnToEmbed(cmdManager[args[0].help]);
}
}
我会提供其他方法。但在我看来,它们并没有那么好,这个答案变得很长。