所以我用typeorm创建了语音机器人。当用户向特定通道发送命令时,机器人应在两个不同的通道中显示消息并设置反应表情符号。但是他没有添加。我不知道这是代码。
命令
import { CLIENT_RENEG_WINDOW } from 'tls';
export default class Voice implements iCommand {
args: String[];
message: any;
client: any;
config: any;
constructor(args: String[], message: any) {
this.args = args;
this.message = 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 => 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)
);
}
}
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): iCommand {
// to jest jakies spierdolone
// @ts-ignore
let command: iCommand = new Commands[key](args, message);
command.test();
command.setConfig(this.config);
command.setClient(this.client);
return command;
}
}
应用,核心应用
import ConfigLoader from './src/config-loader';
import CommandManager from './src/command-manager';
import iCommand from './src/command-manager/commands/i-command';
const Discord = require('discord.js');
const chalk = require('chalk');
const warning = chalk.keyword('orange');
export default class DiscordClient {
client: any;
configLoader: ConfigLoader;
commands: DiscordClient;
commandManager: CommandManager;
config: any;
constructor(){
this.client = new Discord.Client();
this.configLoader = new ConfigLoader();
this.commands = new Discord.Collection();
this.config = this.configLoader.getConfig();
this.commandManager = new CommandManager(this.config);
}
start() : void {
this.client.on('ready', () => {
console.log(warning(`Logged in as ${this.client.user.tag}!`))
this.commandManager.setClient(this.client);
});
this.client.login(this.config.token);
this.client.on('message', message => {
if (this.isAcessToCommandManager(message.content, message.author.bot)) {
let command = this.runCommandManager(message);
// @ts-ignore
command.runCommand();
}
// if (command === 'ping') {
// message.channel.send('Pong.');
// } else if (command === 'beep') {
// message.channel.send('Boop.');
// }
// other commands...
});
}
isAcessToCommandManager(command, isBot) : boolean {
return command.startsWith(this.config.prefix) || isBot;
}
runCommandManager(message) : iCommand {
const args = message.content.slice('.'.length).split(/ +/);
const command = args.shift().toLowerCase();
return this.commandManager.getCommand(command, args, message);
}
}
iCommand,是所有命令的接口。
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;
}
命令,当CommandManager使用键找到命令时
import Voice from './voice';
export const Commands = {
'voice': Voice
}