// import * as SphinxBot from '../../../sphinx-bot' import * as Sphinx from 'sphinx-bot' import { finalAction } from '../controllers/actions' import * as path from 'path' import * as WelcomeBot from './welcome' import * as BitcoinBot from './btc' import { models } from '../models' const msg_types = Sphinx.MSG_TYPE const constants = require(path.join(__dirname, '../../config/constants.json')) const builtinBots = [ 'welcome', 'btc' ] const builtInBotMsgTypes = { 'welcome':[ constants.message_types.message, constants.message_types.group_join ], 'btc':[ constants.message_types.message, ] } const builtInBotNames = { welcome:'WelcomeBot', btc:'BitcoinBot' } export function init() { const client = new Sphinx.Client() client.login('_', finalAction) client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => { const arr = message.content.split(' ') if (arr.length < 2) return if (arr[0]!=='/bot') return const cmd = arr[1] switch (cmd) { case 'install': if (arr.length < 3) return const botName = arr[2] if(builtinBots.includes(botName)) { console.log("INSTALL", botName) const chat = await models.Chat.findOne({where:{ uuid: message.channel.id }}) if(!chat) return const chatBot = { chatId: chat.id, botPrefix: '/'+botName, botType:constants.bot_types.builtin, msgTypes:JSON.stringify(builtInBotMsgTypes[botName]) } await models.ChatBot.create(chatBot) if(botName==='welcome') { WelcomeBot.init() } if(botName==='btc') { BitcoinBot.init() } const theName = builtInBotNames[botName] || 'Bot' const embed = new Sphinx.MessageEmbed() .setAuthor('MotherBot') .setDescription(theName+' has been installed!') message.channel.send({ embed }) } else { // message.reply('No built-in bot by that name') const embed = new Sphinx.MessageEmbed() .setAuthor('MotherBot') .setDescription('No bot with that name') message.channel.send({ embed }) } return true default: const embed = new Sphinx.MessageEmbed() .setAuthor('MotherBot') .setTitle('Bot Commands:') .addFields([ { name: 'Install a new bot', value: '/bot install {BOTNAME}' }, { name: 'Help', value: '/bot help' } ]) .setThumbnail(botSVG) message.channel.send({ embed }) } }) } const botSVG = ` `