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

114 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-05-14 16:29:52 +00:00
import { telegraf, server } from '../index.js'
import { Player } from '../models/player.js'
import crypto from 'crypto'
2022-05-14 16:29:52 +00:00
export const registerAllPlayerCommands = () => {
adminCreateInvitation()
adminBan()
adminRevokeBan()
2022-05-14 16:29:52 +00:00
userCreateInvitation()
}
2022-05-14 16:29:52 +00:00
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
2022-05-14 16:29:52 +00:00
}
const player = await Player.findOne({ 'binding.platform': 'telegram', 'binding.username': ctx.message.from.username })
2022-05-14 16:29:52 +00:00
if(!player || player.permissions.indexOf('admin') === -1) {
2022-07-13 20:06:35 +00:00
return ctx.reply('配钥匙吗?什么?你配?你配几把?')
2022-05-14 16:29:52 +00:00
}
2022-07-13 20:06:35 +00:00
const [invitation, v] = makeInvitation(args[0], args[2], args[1] || 'telegram')
ctx.replyWithMarkdownV2('邀请码:\n```' + invitation + '```\n\n验证码\n```' + v + "```")
})
}
2022-05-14 16:29:52 +00:00
const adminBan = () => {
telegraf.command('banplr', async (ctx) => {
const player = await Player.findOne({ 'binding.platform': 'telegram', 'binding.username': ctx.message.from.username })
2022-05-14 16:29:52 +00:00
if(!player || player.permissions.indexOf('admin') === -1) {
return ctx.reply('配钥匙吗?什么?你配?哦不你不配!')
2022-05-14 16:29:52 +00:00
}
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length < 1) {
ctx.reply('Usage: /banplr <yggdrasil-player-username>')
return
2022-05-14 16:29:52 +00:00
}
await Player.updateOne({ username: args[0] }, { $pull: { permissions: 'login' } })
2022-05-14 16:29:52 +00:00
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) {
2022-07-13 20:06:35 +00:00
return ctx.reply('配钥匙吗?什么?你配?你配几把?')
2022-05-14 16:29:52 +00:00
}
const args = ctx.update.message.text.split(' ').slice(1)
if(args.length < 1) {
ctx.reply('Usage: /revokeban <yggdrasil-player-username>')
return
2022-05-14 16:29:52 +00:00
}
await Player.updateOne({ username: args[0], permissions: { $nin: [ 'login' ] } }, { $push: { permissions: 'login' } })
2022-05-14 16:29:52 +00:00
ctx.reply("已复活玩家 " + args[0])
2022-05-14 16:29:52 +00:00
})
}
2022-05-14 16:29:52 +00:00
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
2022-05-14 16:29:52 +00:00
}
if(ctx.message.chat.id === -1001780498838) {
2022-07-04 17:48:48 +00:00
if(await Player.findOne({ 'email': args[0] })) {
return await ctx.reply('该邮箱已被注册')
}
const [i, v] = makeInvitation(ctx.message.from.username, args[0], 'telegram')
ctx.replyWithMarkdownV2('邀请码:\n```' + i + '```\n\n验证码将发送到私聊如果您未私聊启动过bot则无法接收到消息请 /start 后再次执行此指令。')
try {
2022-07-04 17:48:48 +00:00
await telegraf.telegram.sendMessage(ctx.message.from.id, '验证码(请不要发给任何人)\n```' + v + "```", { parse_mode: 'Markdown' })
} catch (_) { }
2022-05-14 16:29:52 +00:00
} else {
ctx.reply('您不符合自我邀请最低要求,无法获取邀请码')
2022-05-14 16:29:52 +00:00
}
})
}
/*
{
p(latform): "name",
n(ame): "name",
t(o): "email",
}
*/
2022-07-13 20:06:35 +00:00
const templete = "p=$0;n=$1;t=$2"
const makeInvitation = (username, platform, email) => {
2022-07-13 20:06:35 +00:00
let i = templete.replace('$0', platform)
.replace('$1', username)
.replace('$2', email)
i = i.padEnd(i.length + 3 - i.length % 3, ';')
const invitation = Buffer.from(i).toString('base64')
const v = crypto.createSign('RSA-SHA1').update(invitation).sign(server.keys.privateKey, 'hex')
return [invitation, v]
2022-05-14 16:29:52 +00:00
}