import * as Sphinx from 'sphinx-bot' import { finalAction } from '../controllers/actions' import { models } from '../models' import * as path from 'path' const msg_types = Sphinx.MSG_TYPE const constants = require(path.join(__dirname, '../../config/constants.json')) let initted = false export function init() { if(initted) return initted = true const client = new Sphinx.Client() client.login('_', finalAction) client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => { const arr = message.content.split(' ') console.log('message.type',message.type) const isGroupJoin = message.type===constants.message_types.group_join if (arr.length < 2 && !isGroupJoin) return if (arr[0]!=='/welcome' && !isGroupJoin) return const cmd = arr[1] if(isGroupJoin) { const chat = await models.Chat.findOne({where:{uuid:message.channel.id}}) if(!chat) return const chatBot = await models.ChatBot.findOne({where:{ chatId: chat.id, botPrefix:'/welcome', botType:constants.bot_types.builtin }}) if(!(chatBot && chatBot.meta)) return const resEmbed = new Sphinx.MessageEmbed() .setAuthor('WelcomeBot') .setDescription(chatBot.meta) message.channel.send({ embed:resEmbed }) return } switch (cmd) { case 'setmessage': if (arr.length < 3) return console.log("setmsg", arr[2]) const chat = await models.Chat.findOne({where:{uuid:message.channel.id}}) if(!chat) return const chatBot = await models.ChatBot.findOne({where:{ chatId: chat.id, botPrefix:'/welcome', botType:constants.bot_types.builtin }}) if(!chatBot) return const meta = arr.slice(2, arr.length).join(' '); await chatBot.update({meta}) const resEmbed = new Sphinx.MessageEmbed() .setAuthor('WelcomeBot') .setDescription('Your welcome message has been updated') message.channel.send({ embed:resEmbed }) return default: const embed = new Sphinx.MessageEmbed() .setAuthor('WelcomeBot') .setTitle('Bot Commands:') .addFields([ { name: 'Set welcome message', value: '/welcome setmessage {MESSAGE}' }, { name: 'Help', value: '/welcome help' } ]) .setThumbnail(botSVG) message.channel.send({ embed }) return } }) } const botSVG = ` `