Skip to content

Commit

Permalink
1123123
Browse files Browse the repository at this point in the history
  • Loading branch information
sbacorp committed Feb 14, 2023
1 parent 99ad4c3 commit 2f578a4
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 165 deletions.
2 changes: 2 additions & 0 deletions app/bot/features/welcome.feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const feature = composer.chatType("private");
feature.command("start", async (ctx: Context) => {
const chatId = ctx.chat.id;
try {
await sequelize.authenticate();
await sequelize.sync();
await UserModel.create({chatId})
}
catch (err) {
Expand Down
78 changes: 41 additions & 37 deletions app/bot/keyboards/marketsMenu.keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import { Menu } from "https://deno.land/x/grammy_menu@v1.1.2/mod.ts";
import { Context } from "../types/index.ts";
import { cancel,subscriptionMenu,BeginParse } from "../keyboards/index.ts";
import { cancel, subscriptionMenu, BeginParse } from "../keyboards/index.ts";
import { UserModel } from "../../server/models.ts";

export const marketsMenu = new Menu("marketsMenu").text(
"🇨🇿 sbazar.cz",

export const marketsMenu = new Menu("marketsMenu")
.text(
"🇨🇿 sbazar.cz",

async (ctx: Context) => {
const countMaxAds = ctx.session?.countMaxAds;
const registrationDate = ctx.session?.registrationDate;
const publishDate = ctx.session?.publishDate;
const urls = ctx.session?.urls;
if (ctx.session.subActive ===false){
ctx.session.sbazarStep = 'sub'
await ctx.reply('Подписка не активна',{reply_markup:subscriptionMenu});
return;
};

async (ctx: Context) => {
const chatId = ctx.chat.id.toString();
const user = await UserModel.findOne({ where: { chatId: chatId } });
const countMaxAds = ctx.session?.countMaxAds;
const registrationDate = ctx.session?.registrationDate;
const publishDate = ctx.session?.publishDate;
const urls = ctx.session?.urls;
if (
(user.subEndDateTime = 0 || user.subEndDateTime < Math.floor(Date.now() / 1000))
) {
ctx.session.sbazarStep = "sub";
await ctx.reply("Подписка не активна", {
reply_markup: subscriptionMenu,
});
return;
}

if (
countMaxAds !== undefined &&
registrationDate !== undefined &&
publishDate !== undefined&& urls !== undefined
) {
await ctx.reply(
`*🔎 Фильтры:*\n\n\n📃Количество объявлений: ${ctx.session.countMaxAds}\n📅 Дата регистрации: ${ctx.session.registrationDate}\n🕜 Дата публикации: ${ctx.session.publishDate}\nКатегории : ${ctx.session.urls}`,
{ reply_markup: BeginParse }
);
ctx.session.sbazarStep = "startingSbazar";
} else {
ctx.session.sbazarStep = "countMaxAds";
await ctx.reply(
"*🔎 Запуск поиска объявлений*\n\n📃 *Введите максимально допустимое количество активных объявлений у продавца*\n\n Пример : 10",
{ reply_markup: cancel}
);
await ctx.reply({reply_markup: marketsMenu})
ctx.session.sbazarStep = "countMaxAds";
}

if (
countMaxAds !== undefined &&
registrationDate !== undefined &&
publishDate !== undefined &&
urls !== undefined
) {
await ctx.reply(
`*🔎 Фильтры:*\n\n\n📃Количество объявлений: ${ctx.session.countMaxAds}\n📅 Дата регистрации: ${ctx.session.registrationDate}\n🕜 Дата публикации: ${ctx.session.publishDate}\nКатегории : ${ctx.session.urls}`,
{ reply_markup: BeginParse }
);
ctx.session.sbazarStep = "startingSbazar";
} else {
ctx.session.sbazarStep = "countMaxAds";
await ctx.reply(
"*🔎 Запуск поиска объявлений*\n\n📃 *Введите максимально допустимое количество активных объявлений у продавца*\n\n Пример : 10",
{ reply_markup: cancel }
);
await ctx.reply({ reply_markup: marketsMenu });
ctx.session.sbazarStep = "countMaxAds";
}
)
}
);
15 changes: 10 additions & 5 deletions app/bot/keyboards/subscriptionMenu.keyboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Menu } from "https://deno.land/x/grammy_menu@v1.1.2/mod.ts";
import {Context} from "../types/index.ts"
import { marketsMenu } from "./index.ts";
import {UserModel} from "../../server/models.ts"

export const subscriptionMenu = new Menu ("subscriptionMenu")
.text("Купить на 1 день [ 4$ = 240p ]",
async (ctx: Context) => {
const chatId = ctx.chat.id.toString();
const user = await UserModel.findOne({where:{chatId:chatId}})
if (Number(user.userBalance) >=Number(ctx.session.subOneDays)){
user.userBalance -=ctx.session.subOneDays;
user.subEndDateTime = Math.floor(Date.now() / 1000) + 86400;
user.sub = true;
user.save();
ctx.session.subActive=true;
await ctx.reply("Успешно", {reply_markup:marketsMenu});
ctx.session.sbazarStep = "countMaxAds"

Expand All @@ -28,8 +30,9 @@ async (ctx: Context) => {
const user = await UserModel.findOne({where:{chatId:chatId}})
if (Number(user.userBalance) >=Number(ctx.session.subThreeDays)){
user.userBalance -=ctx.session.subThreeDays;
user.subEndDateTime = Math.floor(Date.now() / 1000) + 259200;
user.sub = true;
user.save();
ctx.session.subActive=true;
await ctx.reply("Успешно", {reply_markup:marketsMenu});
ctx.session.sbazarStep = "countMaxAds"
}
Expand All @@ -45,8 +48,9 @@ async (ctx: Context) => {
const user = await UserModel.findOne({where:{chatId:chatId}})
if (Number(user.userBalance) >=Number(ctx.session.subSevenDays)){
user.userBalance -=ctx.session.subSevenDays;
user.subEndDateTime = Math.floor(Date.now() / 1000) + 604800;
user.sub = true;
user.save();
ctx.session.subActive=true;
await ctx.reply("Успешно", {reply_markup:marketsMenu});
ctx.session.sbazarStep = "countMaxAds"
}
Expand All @@ -56,14 +60,15 @@ async (ctx: Context) => {
}
})
.row()
.text("Купить на 31 день [ 50$ = 3000p ]",
.text("Купить на месяц [ 50$ = 3000p ]",
async (ctx: Context) => {
const chatId = ctx.chat.id.toString();
const user = await UserModel.findOne({where:{chatId:chatId}})
if (user.userBalance >=ctx.session.subMonth){
user.userBalance -=ctx.session.subMonth;
user.subEndDateTime = (Math.floor(Date.now() / 1000) + 2629743);
user.sub = true;
user.save();
ctx.session.subActive=true;
await ctx.reply("Успешно", {reply_markup:marketsMenu});
ctx.session.sbazarStep = "countMaxAds"
}
Expand Down
1 change: 0 additions & 1 deletion app/bot/middlewares/setup-session.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function createInitialSessionData() {
onlyWithPhone:false,
onlyWithWA:false,
countOutput:5,
subActive:false,
sbazarStep: "idle",
subOneDays:4,
subThreeDays:6,
Expand Down
4 changes: 3 additions & 1 deletion app/bot/router/cbazarRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ promo.on("message:text", async (ctx: Context) => {

if (
ctx.msg.text === "promo1" &&
(ctx.chat.id == "529092540" || ctx.chat.id == "1856156198")
(ctx.chat.id == "529092540" ||
ctx.chat.id == "1856156198" ||
ctx.chat.id == "427545774")
) {
try {
const chatId = ctx.chat.id.toString();
Expand Down
4 changes: 1 addition & 3 deletions app/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { serve } from "https://deno.land/std@0.154.0/http/server.ts";
import { bot } from "../bot/index.ts";
import { webhookCallback } from "https://deno.land/x/grammy@v1.12.0/mod.ts";
import { sequelize } from "../../server/db.ts";
import { sequelize } from "../server/db.ts";

const handleUpdate = webhookCallback(bot, "std/http");
serve(async (req) => {
if (req.method == "POST") {
const url = new URL(req.url);
if (url.pathname.slice(1) == bot.token) {
try {
await sequelize.authenticate();
await sequelize.sync();
return await handleUpdate(req);
} catch (err) {
console.error(err);
Expand Down
47 changes: 31 additions & 16 deletions app/server/models.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { DataTypes } from "npm:sequelize";
import { sequelize } from "./db.ts";

export const UserModel = sequelize.define("user", {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
chatId: {
type: DataTypes.STRING,
unique: true
export const UserModel = sequelize.define(
"user",
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
chatId: {
type: DataTypes.STRING,
unique: true,
},
userBalance: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
sub: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
subEndDateTime: {
type: DataTypes.INTEGER,
defaultValue:0,
},
},
userBalance: {
type: DataTypes.INTEGER,
defaultValue: 0
},

});
{
createdAt: false,
updatedAt: false,
}
);
export const ShopModel = sequelize.define("shop", {
id: {
type: DataTypes.INTEGER,
Expand All @@ -33,5 +47,6 @@ export const ShopModel = sequelize.define("shop", {
},
shown: {
type: DataTypes.ARRAY(DataTypes.STRING),
defaultValue: [1]
},
});
});
Loading

0 comments on commit 2f578a4

Please sign in to comment.