34 lines
1.5 KiB
JavaScript
34 lines
1.5 KiB
JavaScript
const {
|
||
Telegraf
|
||
} = require('telegraf')
|
||
const bot = new Telegraf(process.env.BOT_TOKEN)
|
||
const {
|
||
UserModel,
|
||
PromocodeModel,
|
||
adminList
|
||
} = require('/dev/Telegram/CampFireGameBot/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)}`)
|
||
} |