114 lines
4.1 KiB
JavaScript
114 lines
4.1 KiB
JavaScript
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[2], args[1] || '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) {
|
||
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 {
|
||
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 templete = "p=$0;n=$1;t=$2"
|
||
const makeInvitation = (username, platform, email) => {
|
||
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]
|
||
} |