markdown + smiles test 2

This commit is contained in:
smsteel 2019-02-10 18:58:35 +03:00
parent b4639e4e51
commit 66c7fbbe41

14
bot.js
View File

@ -1,4 +1,5 @@
import Telegraf from 'telegraf' import Telegraf from 'telegraf'
import TelegrafExtra from 'telegraf/extra'
import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount, import { saveChat, getChats, saveLogoutTime, getLastLogoutTime, saveKillCount, getKillCount,
saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData saveLevelUp, getLevelUpCount, saveLevel, setOnlineState, setOfflineState, getPlayersData
} from './database' } from './database'
@ -10,6 +11,7 @@ import { currentTime } from './utility'
import { getEntityDeathMessage, getEntityName } from './ru' import { getEntityDeathMessage, getEntityName } from './ru'
const bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM') const bot = new Telegraf('643297173:AAGuqfZx3GhiiARwvY7AtWTTFw1T-2FiwCM')
const markdown = TelegrafExtra.markdown()
bot.start(({ message: { chat: { id } } }) => saveChat(id)) bot.start(({ message: { chat: { id } } }) => saveChat(id))
bot.command( bot.command(
@ -17,7 +19,7 @@ bot.command(
({ replyWithMarkdown }) => getPlayersData() ({ replyWithMarkdown }) => getPlayersData()
.then(getPlayersData => replyWithMarkdown( .then(getPlayersData => replyWithMarkdown(
getPlayersData getPlayersData
.map(({ displayName, level, online }) => `:bust_in_silhouette: *${displayName}* уровень ${level} ${online ? '*онлайн*' : 'оффлайн'}`) .map(({ displayName, level, online }) => `👤 *${displayName}* уровень ${level} ${online ? '*онлайн*' : 'оффлайн'}`)
.join('\n') .join('\n')
)) ))
) )
@ -25,7 +27,7 @@ bot.launch()
const sendMessageToAll = text => getChats() const sendMessageToAll = text => getChats()
.then(chats => chats.map(({ chatId }) => chatId) .then(chats => chats.map(({ chatId }) => chatId)
.forEach(chatId => bot.telegram.sendMessage(chatId, text)) .forEach(chatId => bot.telegram.sendMessage(chatId, text, markdown))
) )
const JOIN_NOTIFICATION_TIME_DELTA = 60 * 60 // 1h const JOIN_NOTIFICATION_TIME_DELTA = 60 * 60 // 1h
@ -38,14 +40,14 @@ export const sendEvent = ({ type, displayName, ...payload }) => {
setOnlineState(displayName) setOnlineState(displayName)
getLastLogoutTime(displayName) getLastLogoutTime(displayName)
.then(lastLogoutTime => lastLogoutTime < currentTime() - JOIN_NOTIFICATION_TIME_DELTA && .then(lastLogoutTime => lastLogoutTime < currentTime() - JOIN_NOTIFICATION_TIME_DELTA &&
sendMessageToAll(`Игрок ${displayName} присоединился!`)) sendMessageToAll(`👤 *${displayName}* присоединился 😼`))
break break
case EVENT_TYPE_QUIT: case EVENT_TYPE_QUIT:
setOfflineState(displayName) setOfflineState(displayName)
saveLogoutTime(displayName) saveLogoutTime(displayName)
break break
case EVENT_TYPE_DEATH: case EVENT_TYPE_DEATH:
sendMessageToAll(`Игрок ${displayName} ${getEntityDeathMessage(payload)} :(`) sendMessageToAll(`👤 *${displayName}* ☠️ ${getEntityDeathMessage(payload)} ☠️`)
break break
case EVENT_TYPE_PLAYER_LEVEL_CHANGE: case EVENT_TYPE_PLAYER_LEVEL_CHANGE:
const { newLevel, oldLevel } = payload const { newLevel, oldLevel } = payload
@ -57,7 +59,7 @@ export const sendEvent = ({ type, displayName, ...payload }) => {
if (newLevel % LEVEL_NOFIFICATION_EACH_LEVELS === 0 && if (newLevel % LEVEL_NOFIFICATION_EACH_LEVELS === 0 &&
levelUpCount >= LEVEL_NOFIFICATION_EACH_LEVELS levelUpCount >= LEVEL_NOFIFICATION_EACH_LEVELS
) { ) {
sendMessageToAll(`Игрок ${displayName} прокачался до уровня ${newLevel}!`) sendMessageToAll(`👤 *${displayName}* теперь уровня *${newLevel}* 👍`)
saveLevelUp(displayName, 0) saveLevelUp(displayName, 0)
} else { } else {
saveLevelUp(displayName, levelUpCount + 1) saveLevelUp(displayName, levelUpCount + 1)
@ -69,7 +71,7 @@ export const sendEvent = ({ type, displayName, ...payload }) => {
const newKillCount = killCount + 1 const newKillCount = killCount + 1
saveKillCount(displayName, payload.entityName, newKillCount) saveKillCount(displayName, payload.entityName, newKillCount)
if (newKillCount % KILL_NOTIFICATION_EACH_KILLS === 0) { if (newKillCount % KILL_NOTIFICATION_EACH_KILLS === 0) {
sendMessageToAll(`${getEntityName(payload)} повержен(а) игроком ${displayName}. Убито ещё ${KILL_NOTIFICATION_EACH_KILLS}! Всего: ${newKillCount}`) sendMessageToAll(`👻 *${getEntityName(payload)}* повержен(а) 👤 ${displayName}. Убито ещё ${KILL_NOTIFICATION_EACH_KILLS}! Всего 💀: ${newKillCount} 👍`)
} }
}) })
break break