|
@ -6,9 +6,12 @@ import * as WelcomeBot from './welcome' |
|
|
import * as BitcoinBot from './btc' |
|
|
import * as BitcoinBot from './btc' |
|
|
import * as LoopBot from './loop' |
|
|
import * as LoopBot from './loop' |
|
|
import { models } from '../models' |
|
|
import { models } from '../models' |
|
|
|
|
|
import fetch from 'node-fetch' |
|
|
const msg_types = Sphinx.MSG_TYPE |
|
|
const msg_types = Sphinx.MSG_TYPE |
|
|
|
|
|
|
|
|
|
|
|
const env = process.env.NODE_ENV || 'development' |
|
|
const constants = require(path.join(__dirname, '../../config/constants.json')) |
|
|
const constants = require(path.join(__dirname, '../../config/constants.json')) |
|
|
|
|
|
const config = require(path.join(__dirname, '../../config/app.json'))[env] |
|
|
|
|
|
|
|
|
const builtinBots = [ |
|
|
const builtinBots = [ |
|
|
'welcome', 'btc', 'loopout' |
|
|
'welcome', 'btc', 'loopout' |
|
@ -42,12 +45,22 @@ export function init() { |
|
|
case 'install': |
|
|
case 'install': |
|
|
if (arr.length < 3) return |
|
|
if (arr.length < 3) return |
|
|
const botName = arr[2] |
|
|
const botName = arr[2] |
|
|
|
|
|
|
|
|
if(builtinBots.includes(botName)) { |
|
|
if(builtinBots.includes(botName)) { |
|
|
console.log("INSTALL", botName) |
|
|
console.log("INSTALL", botName) |
|
|
const chat = await models.Chat.findOne({where:{ |
|
|
const chat = await models.Chat.findOne({where:{ |
|
|
uuid: message.channel.id |
|
|
uuid: message.channel.id |
|
|
}}) |
|
|
}}) |
|
|
if(!chat) return |
|
|
if(!chat) return |
|
|
|
|
|
const existing = await models.ChatBot.findOne({where:{ |
|
|
|
|
|
chatId: chat.id, botPrefix: '/'+botName, |
|
|
|
|
|
}}) |
|
|
|
|
|
if (existing) { |
|
|
|
|
|
const embed = new Sphinx.MessageEmbed() |
|
|
|
|
|
.setAuthor('MotherBot') |
|
|
|
|
|
.setDescription(botName + ' already installed') |
|
|
|
|
|
return message.channel.send({ embed }) |
|
|
|
|
|
} |
|
|
const msgTypes = builtInBotMsgTypes[botName] || [ |
|
|
const msgTypes = builtInBotMsgTypes[botName] || [ |
|
|
constants.message_types.message |
|
|
constants.message_types.message |
|
|
] |
|
|
] |
|
@ -74,12 +87,42 @@ export function init() { |
|
|
.setDescription(theName+' has been installed!') |
|
|
.setDescription(theName+' has been installed!') |
|
|
message.channel.send({ embed }) |
|
|
message.channel.send({ embed }) |
|
|
} else { |
|
|
} else { |
|
|
// message.reply('No built-in bot by that name')
|
|
|
const bot = await getBotByName(botName) |
|
|
|
|
|
if(bot && bot.uuid) { |
|
|
|
|
|
console.log('=> FOUDN BOT', bot) |
|
|
|
|
|
// send msg to bot owner
|
|
|
|
|
|
// await models.ChatBot.create(chatBot)
|
|
|
|
|
|
} else { |
|
|
|
|
|
const embed = new Sphinx.MessageEmbed() |
|
|
|
|
|
.setAuthor('MotherBot') |
|
|
|
|
|
.setDescription('No bot with that name') |
|
|
|
|
|
message.channel.send({ embed }) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return true |
|
|
|
|
|
|
|
|
|
|
|
case 'search': |
|
|
|
|
|
if (arr.length < 2) return |
|
|
|
|
|
const query = arr[2] |
|
|
|
|
|
const bots = await searchBots(query) |
|
|
|
|
|
if(bots.length===0) { |
|
|
const embed = new Sphinx.MessageEmbed() |
|
|
const embed = new Sphinx.MessageEmbed() |
|
|
.setAuthor('MotherBot') |
|
|
.setAuthor('MotherBot') |
|
|
.setDescription('No bot with that name') |
|
|
.setDescription('No bots found') |
|
|
message.channel.send({ embed }) |
|
|
return message.channel.send({ embed }) |
|
|
} |
|
|
} |
|
|
|
|
|
const embed3 = new Sphinx.MessageEmbed() |
|
|
|
|
|
.setAuthor('MotherBot') |
|
|
|
|
|
.setTitle('Bots:') |
|
|
|
|
|
.addFields(bots.map(b=> { |
|
|
|
|
|
const maxLength = 35 |
|
|
|
|
|
const value = b.description.length>maxLength ? |
|
|
|
|
|
b.description.substr(0,maxLength)+'...' : |
|
|
|
|
|
b.description |
|
|
|
|
|
return {name:b.unique_name, value} |
|
|
|
|
|
})) |
|
|
|
|
|
.setThumbnail(botSVG) |
|
|
|
|
|
message.channel.send({ embed:embed3 }) |
|
|
return true |
|
|
return true |
|
|
|
|
|
|
|
|
default: |
|
|
default: |
|
@ -98,4 +141,26 @@ export function init() { |
|
|
|
|
|
|
|
|
const botSVG = `<svg viewBox="64 64 896 896" height="16" width="16" fill="white">
|
|
|
const botSVG = `<svg viewBox="64 64 896 896" height="16" width="16" fill="white">
|
|
|
<path d="M300 328a60 60 0 10120 0 60 60 0 10-120 0zM852 64H172c-17.7 0-32 14.3-32 32v660c0 17.7 14.3 32 32 32h680c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32zm-32 660H204V128h616v596zM604 328a60 60 0 10120 0 60 60 0 10-120 0zm250.2 556H169.8c-16.5 0-29.8 14.3-29.8 32v36c0 4.4 3.3 8 7.4 8h729.1c4.1 0 7.4-3.6 7.4-8v-36c.1-17.7-13.2-32-29.7-32zM664 508H360c-4.4 0-8 3.6-8 8v60c0 4.4 3.6 8 8 8h304c4.4 0 8-3.6 8-8v-60c0-4.4-3.6-8-8-8z" /> |
|
|
<path d="M300 328a60 60 0 10120 0 60 60 0 10-120 0zM852 64H172c-17.7 0-32 14.3-32 32v660c0 17.7 14.3 32 32 32h680c17.7 0 32-14.3 32-32V96c0-17.7-14.3-32-32-32zm-32 660H204V128h616v596zM604 328a60 60 0 10120 0 60 60 0 10-120 0zm250.2 556H169.8c-16.5 0-29.8 14.3-29.8 32v36c0 4.4 3.3 8 7.4 8h729.1c4.1 0 7.4-3.6 7.4-8v-36c.1-17.7-13.2-32-29.7-32zM664 508H360c-4.4 0-8 3.6-8 8v60c0 4.4 3.6 8 8 8h304c4.4 0 8-3.6 8-8v-60c0-4.4-3.6-8-8-8z" /> |
|
|
</svg>` |
|
|
</svg>` |
|
|
|
|
|
|
|
|
|
|
|
async function searchBots(q:string) { |
|
|
|
|
|
try { |
|
|
|
|
|
const r = await fetch(`https://${config.tribes_host}/search/bots/${q}`) |
|
|
|
|
|
const j = await r.json() |
|
|
|
|
|
return Array.isArray(j) ? j : [] |
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
return [] |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
async function getBotByName(name:string) { |
|
|
|
|
|
try { |
|
|
|
|
|
const r = await fetch(`https://${config.tribes_host}/bot/${name}`) |
|
|
|
|
|
const j = await r.json() |
|
|
|
|
|
if(j&&j.uuid&&j.owner_pub_key) { |
|
|
|
|
|
return j |
|
|
|
|
|
} |
|
|
|
|
|
return null |
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
return null |
|
|
|
|
|
} |
|
|
|
|
|
} |