You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

import {processBotMessage} from '../controllers/bots'
import {Msg} from './interfaces'
import { models } from '../models'
// const defaultPrefixes = [
// '/bot', '/welcome'
// ]
// return bool whether to skip forwarding to tribe
export async function isBotMsg(msg:Msg, sentByMe:boolean): Promise<boolean> {
const txt = msg.message.content
const chat = await models.Chat.findOne({where:{
uuid: msg.chat.uuid
}})
if(!chat) return false
if(txt.startsWith('/bot ')) {
const ok = processBotMessage(msg, chat, null)
return ok?true:false
}
const botsInTribe = await models.ChatMember.findAll({where:{
bot:true, chatId: chat.id
}})
if(!(botsInTribe && botsInTribe.length)) return false
let ok = false
await asyncForEach(botsInTribe, async botInTribe=>{
if(txt.startsWith(`${botInTribe.botPrefix} `)){
ok = await processBotMessage(msg, chat, botInTribe)
}
})
return ok
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}