所以我正在开发一个不和谐的机器人。现在我试图找到一种方法来实现它,以便我可以将别名传递给命令。我几乎拥有它,但现在我遇到了数组需要参数的地方,现在我卡住了。
import { CommandResponse } from "./commands/command";
import { commands } from "./commands";
import { DiscordBot } from "../server";
import * as Discord from "discord.js";
interface CommandMap {
[command: string]: Function;
}
/**
* Handle command interactions
*
* @export
* @class Command
*/
export class Command {
private commands: CommandMap = {};
/**
* Creates an instance of Command.
* @param {DiscordBot} bot
* @memberof Command
*/
constructor(private bot: DiscordBot) {
commands.forEach(command => {
this.commands[command.commandName, command.commandAlias] = command.buildResponse;
})
console.log(commands);
}
/**
* Run a command by name
*
* @param {string} command the command to run
* @param {Discord.Message} message send the raw command message
* @returns {Promise<CommandResponse>} the command's response
* @memberof Command
*/
public async runCommand(command: string, message: Discord.Message): Promise<CommandResponse> {
if (this.commands[command] === undefined) return null;
return this.commands[command](message.author.id, message, this.bot);
}
}
我最初的问题是 command.commandAlias[] 参数不能用作索引。这里也是对命令结构本身的引用:
export interface CommandResponse {
response: any;
channelId: number;
mention: boolean;
}
export interface ICommand {
commandName: string;
commandAlias: string[];
buildResponse(user: number, message: any, bot: DiscordBot): Promise<CommandResponse>;
}