lsp-yggdrasil/src/telegram/player-commands.js

108 lines
3.8 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, server } from '../index.js'
import { Player } from '../models/player.js'
import crypto from 'crypto'
export const registerAllPlayerCommands = () => {
adminCreateInvitation()
adminBan()
adminRevokeBan()
userCreateInvitation()
}
const adminCreateInvitation = () => {
telegraf.command('invite', async (ctx) => {
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length < 2) {
ctx.reply('Usage: /invite <username> <email> [platform]')
return
}
const player = await Player.findOne({ 'binding.platform': 'telegram', 'binding.username': ctx.message.from.username })
if(!player || player.permissions.indexOf('admin') === -1) {
return ctx.reply('配钥匙吗?什么?你配?哦不你不配!')
}
const [invitation, v] = makeInvitation(args[0], args[1], args[2] || 'telegram')
ctx.replyWithMarkdownV2('邀请码:\n```' + invitation + '```\n\n验证码\n```' + v + "```")
})
}
const adminBan = () => {
telegraf.command('banplr', async (ctx) => {
const player = await Player.findOne({ 'binding.platform': 'telegram', 'binding.username': ctx.message.from.username })
if(!player || player.permissions.indexOf('admin') === -1) {
return ctx.reply('配钥匙吗?什么?你配?哦不你不配!')
}
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length < 1) {
ctx.reply('Usage: /banplr <yggdrasil-player-username>')
return
}
await Player.updateOne({ username: args[0] }, { $pull: { permissions: 'login' } })
ctx.reply("已枪毙玩家 " + args[0])
})
}
const adminRevokeBan = () => {
telegraf.command('revokeban', async (ctx) => {
const player = await Player.findOne({ 'binding.platform': 'telegram', 'binding.username': ctx.message.from.username })
if(!player || player.permissions.indexOf('admin') === -1) {
return ctx.reply('配钥匙吗?什么?你配?哦不你不配!')
}
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length < 1) {
ctx.reply('Usage: /revokeban <yggdrasil-player-username>')
return
}
await Player.updateOne({ username: args[0], permissions: { $nin: [ 'login' ] } }, { $push: { permissions: 'login' } })
ctx.reply("已复活玩家 " + args[0])
})
}
const userCreateInvitation = () => {
telegraf.command('inviteme', async (ctx) => {
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length === 0) {
ctx.reply('Usage: /inviteme <email>')
return
}
if(ctx.message.chat.id === -1001780498838) {
const [i, v] = makeInvitation(ctx.message.from.username, args[0], 'telegram')
ctx.replyWithMarkdownV2('邀请码:\n```' + i + '```\n\n验证码将发送到私聊如果您未私聊启动过bot则无法接收到消息请 /start 后再次执行此指令。')
try {
await telegraf.telegram.sendMessage(ctx.message.from.id, '验证码:\n```' + v + "```", { parse_mode: 'Markdown' })
} catch (_) { }
} else {
ctx.reply('您不符合自我邀请最低要求,无法获取邀请码')
}
})
}
/*
{
p(latform): "name",
n(ame): "name",
t(o): "email",
}
*/
const makeInvitation = (username, platform, email) => {
const invitation = crypto.publicEncrypt(server.keys.publicKey, Buffer.from(JSON.stringify({
p: platform,
n: username,
t: email,
}))).toString('hex')
const v = crypto.createSign('RSA-SHA1').update(invitation).sign(server.keys.privateKey, 'hex')
return [invitation, v]
}