CampFirePlay/commands/organization/materialsBuy.js
2024-12-21 20:01:15 +03:00

29 lines
1.6 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 {
UserModel,
WorldModel,
BusinessModel
} = global.config
module.exports = async (ctx) => {
ctx.args = ctx.update.message.text.split(' ')
let user = await UserModel.findByPk(ctx.from.id)
if (user.business.id == 0) return await ctx.reply(`🚫 Вы не владеете организацией.`)
let world = await WorldModel.findByPk(1)
let business = await BusinessModel.findOne({
where: {
owner: ctx.from.id.toString()
}
})
if (business === null) return await ctx.reply(`🚫 У вас нет организации.`)
if (!ctx.args[1]) return ctx.reply(`🚫 Не указан аргумент.`)
if (!Number(ctx.args[1])) return ctx.reply(` Количество должно быть числом.`)
if (ctx.args[1] < 1) return ctx.reply(` Минимальное количество для покупки - 1`)
if (ctx.args[1]*world.matPrice > business.balance) return ctx.reply(`🚫 Недостаточно средств на балансе организации.`)
if (business.materials >= 700) return ctx.reply(`🚫 Склады полны.`)
if (business.materials + Number(ctx.args[1]) > 700) return ctx.reply(`🚫 Нет столько места на складах.\n Можно закупить еще ${700 - business.materials}`)
let count = Number(ctx.args[1])
business.balance -= world.matPrice*count
business.materials += count
business.save()
return await ctx.reply(` Закуплено ${ctx.args[1]} материалов для организации на сумму ₽${world.matPrice*count}`)
}