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

34 lines
1.5 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.

const {
Telegraf
} = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
const {
UserModel,
PromocodeModel,
adminList
} = global.config
const {
spaces
} = require('../utils')
const sequelize = require('../db');
module.exports = async (ctx) => {
let user = await UserModel.findByPk(ctx.from.id)
ctx.args = ctx.update.message.text.split(' ')
if (!ctx.args[1]) return ctx.reply(`/promocode [Code]`)
let promocode = await PromocodeModel.findOne({
where: {
code: ctx.args[1]
}
})
if(promocode === null) return await ctx.reply(` Нет такого кода.`)
if(promocode.users.length-1 >= promocode.activations && promocode.users != null) return await ctx.reply(`⚠️ Промокод уже активировали максимальное количество раз.`)
if(promocode.users.includes(ctx.from.id.toString())) return await ctx.reply(`⚠️ Вы уже активировали этот промокод.`)
console.log(promocode.users)
promocode.users = sequelize.fn('array_append', sequelize.col('users'), ctx.from.id.toString());
user.money += promocode.money
user.save()
promocode.save()
await bot.telegram.sendMessage(adminList[0], `${user.username} активировал промокод на ₽${spaces(promocode.money)}`)
return await ctx.reply(`✅ Вы активировали промокод на ₽${spaces(promocode.money)}`)
}