I have a unique method that implements an interface.
This is My Command.
import iCommand from './i-command';
export default class Voice implements iCommand {
args: String[];
message: any;
client: any;
config: any;
constructor(args: String[], message: any) {
this.args = [];
this.message = {};
this.client = {};
this.config = {};
}
test() {
console.log('run');
}
setClient(client: any) {
this.client = client;
}
setConfig(config: any) {
this.config = config;
}
runCommand(): void {
const emoji = this.message.guild.emojis.find(emoji => emoji.name === 'alpha_flag');
this.message.channel.send('test').then(newMsg => {console.log(emoji); newMsg.react(emoji)});
this.message.channel.send('test').then(newMsg => {console.log(emoji); newMsg.react(emoji)});
this.client.channels.get(this.config.infoChannelId).send(`:fire: **Voice voting for player ${this.args[0]} started at** #voicing :fire:`);
this.client.channels.get(this.config.voiceChannelId).send(`:fire: **Voting for player ${this.args[0]}** :fire:`).then(
message => message.react(emoji)
);
}
}
This is My interface:
export default interface iCommand {
args: Array<String>;
message: any;
client: any;
config: any;
runCommand(): void;
test(): void;
setClient(client: any): void;
setConfig(config: any): void;
}
Dictionary:
import Voice from './voice';
export const Commands = {
'voice': Voice
}
and This is CommandManager:
import {Commands} from './commands/commands';
import iCommand from './commands/i-command';
export default class CommandManager {
client: any;
config: any;
constructor(config: any) {
this.config = config;
this.client = {};
}
setClient(client: any) {
this.client = client;
}
getCommand(key: any, args: String[], message: any): void {
let command: iCommand = new Commands[key](args, message);
command.test();
// @ts-ignore
command.setConfig(this.config);
// @ts-ignore
command.setClient(this.client);
// @ts-ignore
return command;
}
}
How does this work? When a user uses a command like .voice
, the CommandManager
uses the key to return the command. However, no matter what I do, Commands[key]()
does not act as a constructor. Interestingly, the test method is functional, but a typeError
prevents my promises from working. I tried to disable the TypeScript error, but it was ineffective. Where is the error in my code? Should I use typeof
with the key?