mcTelegramBot/bot.js
2019-02-11 09:20:05 +03:00

86 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Telegraf from 'telegraf'
import TelegrafExtra from 'telegraf/extra'
import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount,
saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData
} from './database'
import {
EVENT_TYPE_JOIN, EVENT_TYPE_DEATH, EVENT_TYPE_QUIT, EVENT_TYPE_PLAYER_LEVEL_CHANGE,
EVENT_TYPE_PLAYER_KILLED_ENTITY
} from './types'
import { currentTime } from './utility'
import { getEntityDeathMessage, getEntityName } from './ru'
import stringTable from 'string-table'
const bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM')
const markdown = TelegrafExtra.markdown()
bot.start(({ message: { chat: { id } } }) => saveChat(id))
bot.command(
'players',
({ replyWithMarkdown }) => getPlayersData()
.then(getPlayersData => replyWithMarkdown(
stringTable.create(
getPlayersData
.map(({ displayName, level, online }) => ({
'имя ': `👤 *${displayName}*`,
'уровень': level,
'статус ': `${online ? '✅ *онлайн*' : '❔ оффлайн'}`
}))
)
))
)
bot.launch()
const sendMessageToAll = text => getChats()
.then(chats => chats.map(({ chatId }) => chatId)
.forEach(chatId => bot.telegram.sendMessage(chatId, text, markdown))
)
const JOIN_NOTIFICATION_TIME_DELTA = 60 * 60 // 1h
const LEVEL_NOFIFICATION_EACH_LEVELS = 5
const KILL_NOTIFICATION_EACH_KILLS = 50
export const sendEvent = ({ type, displayName, ...payload }) => {
switch (type) {
case EVENT_TYPE_JOIN:
setOnlineState(displayName)
getLastLogoutTime(displayName)
.then(lastLogoutTime => lastLogoutTime < currentTime() - JOIN_NOTIFICATION_TIME_DELTA &&
sendMessageToAll(`👤 *${displayName}* присоединился 😼`))
break
case EVENT_TYPE_QUIT:
setOfflineState(displayName)
saveLogoutTime(displayName)
break
case EVENT_TYPE_DEATH:
sendMessageToAll(`👤 *${displayName}* ☠️ ${getEntityDeathMessage(payload)} ☠️`)
break
case EVENT_TYPE_PLAYER_LEVEL_CHANGE:
const { newLevel, oldLevel } = payload
saveLevel(displayName, newLevel)
if (newLevel < oldLevel) {
return
}
const levelUpCount = getLevelUpCount(displayName)
if (newLevel % LEVEL_NOFIFICATION_EACH_LEVELS === 0 &&
levelUpCount >= LEVEL_NOFIFICATION_EACH_LEVELS
) {
sendMessageToAll(`👤 *${displayName}* теперь уровня *${newLevel}* 👍`)
saveLevelUp(displayName, 0)
} else {
saveLevelUp(displayName, levelUpCount + 1)
}
break
case EVENT_TYPE_PLAYER_KILLED_ENTITY:
getKillCount(displayName, payload.entityName)
.then(killCount => {
const newKillCount = killCount + 1
saveKillCount(displayName, payload.entityName, newKillCount)
if (newKillCount % KILL_NOTIFICATION_EACH_KILLS === 0) {
sendMessageToAll(`👻 *${getEntityName(payload)}* повержен(а) 👤 *${displayName}*. Убито ещё ${KILL_NOTIFICATION_EACH_KILLS}! Всего 💀: ${newKillCount} 👍`)
}
})
break
}
}