CampFirePlay/commands/pay.js
Degradin baa0b5f3a9 global refactoring
Все переведено в модули
2023-10-08 23:43:12 +03:00

35 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

const {
Telegraf
} = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
const {
UserModel,
WorldModel
} = require('../config')
module.exports = async (ctx) => {
ctx.args = ctx.update.message.text.split(' ')
if (!ctx.args[1] || !ctx.args[2]) return ctx.reply(`/pay [Никнейм] [Сумма]`)
if (!Number(ctx.args[2])) return ctx.reply(`❕ Сумма должна быть числовая.`)
let sender = await UserModel.findByPk(ctx.from.id);
let receiver = await UserModel.findOne({
where: {
username: ctx.args[1]
}
});
let world = await WorldModel.findByPk(1)
let fee = Number(Math.trunc(ctx.args[2] / 100 * world.transactionfee))
if (ctx.args[2] == 0 || ctx.args[2] < 100) return ctx.reply(`❕ Минимальная сумма перевода ₽100.`)
if (sender.money < ctx.args[2]) return ctx.reply(`❕ Недостаточно средств!`)
if (sender.money < Number(ctx.args[2]) + fee) return ctx.reply(`❕ Недостаточно средств для перевода с комиссией!`)
sender.money -= Number(ctx.args[2])
sender.money -= Number(fee)
world.balance += Number(fee)
await sender.save();
await world.save();
receiver.money += Number(ctx.args[2])
await receiver.save();
let pmToReceiver = `🔽 Входящий перевод.\n\n🔼 Отправитель: ${sender.username}\n🆔 Отправителя: ${sender.telegram_id}\n🔢 Сумма: ₽${ctx.args[2]}\n🆕 Ваш баланс: ₽${receiver.money}`
bot.telegram.sendMessage(receiver.telegram_id, pmToReceiver)
return ctx.reply(`🔼 Исходящий перевод.\n\n🔽 Получатель: ${receiver.username}\n🆔 Получателя: ${receiver.telegram_id}\n🔢 Сумма: ₽${ctx.args[2]}\n Комиссия: ₽${fee}\n🆕 Ваш баланс: ₽${sender.money}`)
}