Skip to content

Commit

Permalink
payments + users top
Browse files Browse the repository at this point in the history
  • Loading branch information
katsuhira02 committed Jun 22, 2024
1 parent cce7e6d commit b10ff15
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 3 deletions.
46 changes: 46 additions & 0 deletions supabase/functions/_shared/supabase/payments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { supabase } from "./index.ts";

export async function sendPaymentInfo(user_id: string, level: string): Promise<any> {
const { data, error } = await supabase
.from('payments')
.insert([
{ user_id: user_id, level: level }
]);

if (error) {
console.error('Error sending payment info:', error);
} else {
console.log('Payment info sent successfully:', data);
return data
}
}

export async function getPaymentsInfoByUsername(username: string): Promise<any> {
// Получаем user_id по username из таблицы users
const { data: userData, error: userError } = await supabase
.from('users')
.select('user_id')
.eq('username', username)
.single();

if (userError) {
console.error('Error fetching user ID:', userError);
return null;
}

const user_id = userData.user_id;

// Получаем все строчки с данным user_id из таблицы payments
const { data: paymentsData, error: paymentsError } = await supabase
.from('payments')
.select('*')
.eq('user_id', user_id);

if (paymentsError) {
console.error('Error fetching payments info:', paymentsError);
return null;
}

return paymentsData;
}

39 changes: 39 additions & 0 deletions supabase/functions/_shared/supabase/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,42 @@ export async function getCorrects(
throw new Error("Error getCorrects(254): " + error);
}
}

export async function getTop10Users(): Promise<any[]> {
try {
const { data: topUsers, error: topUsersError } = await supabase
.from("progress")
.select("user_id, all")
.order("all", { ascending: false })
.limit(10);

if (topUsersError) {
throw new Error("Error fetching top users: " + topUsersError.message);
}

const userIds = topUsers.map((user) => user.user_id);

const { data: userInfo, error: userInfoError } = await supabase
.from("users")
.select("username, user_id")
.in("user_id", userIds);

if (userInfoError) {
throw new Error("Error fetching user info: " + userInfoError.message);
}
console.log(userInfo, "userInfo")

const result = topUsers.map((user) => {
const userInfoData = userInfo.find((info) => info.user_id === user.user_id);
return {
username: userInfoData?.username,
all: user.all,
};
});

console.log(result)
return result;
} catch (error) {
throw new Error("Error getTop10Users: " + error);
}
}
87 changes: 84 additions & 3 deletions supabase/functions/ai-koshey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
getQuestion,
updateProgress,
updateResult,
getTop10Users
} from "../_shared/supabase/progress.ts";
import { pathIncrement } from "../path-increment.ts";

Expand Down Expand Up @@ -413,17 +414,17 @@ botAiKoshey.command("start", async (ctx: AiKosheyContext) => {
console.log(await isRu(ctx), "isRu")
const lang = await isRu(ctx)


const chatIdSubscription = lang ? "-1002012841987" : "-1002015840738"
const isSubscription = await checkSubscription(
ctx,
ctx.from?.id,
"-1002228291515"
chatIdSubscription
);
if (!isSubscription) {
await ctx.reply(lang ? "Вы не подписаны на канал. Чтобы продолжить тест, нужно подписаться 👁‍🗨" : "You are not subscribed to the channel. To continue the test, you need to subscribe to the channel 👁‍🗨",
{
reply_markup: { inline_keyboard: [
[{ text: lang ? "👁‍🗨 Подписаться" : "👁‍🗨 Subscribe", url: "https://t.me/ai_koshey999nft" }],
[{ text: lang ? "👁‍🗨 Подписаться" : "👁‍🗨 Subscribe", url: lang ? "https://t.me/ai_koshey999nft" : "https://t.me/ai_koshey_en" }],
] }
}
);
Expand Down Expand Up @@ -624,6 +625,28 @@ botAiKoshey.command("start", async (ctx: AiKosheyContext) => {
}
});

botAiKoshey.command("buy", async (ctx) => {
const lang = await isRu(ctx)
ctx.reply(lang ? "🤝 Выберите уровень подписки, который выхотите приобрести" : "🤝 Select the level of subscription you want to purchase", {
reply_markup: {
inline_keyboard: [[{ text: lang ? "🔥 Огонь" : "🔥 Fire", callback_data: "buy_fire" }], [{ text: lang ? "🌊 Вода" : "🌊 Water", callback_data: "buy_water" }], [{ text: lang ? "🎺 Медные трубы" : "🎺 Copper pipes", callback_data: "buy_copper_pipes" }]],
},
})
return;
});

botAiKoshey.on("pre_checkout_query", (ctx) => {
ctx.answerPreCheckoutQuery(true)
return;
});

botAiKoshey.on("message:successful_payment", async (ctx) => {
const lang = await isRu(ctx)
console.log("ctx 646(succesful_payment)", ctx)
ctx.reply(lang ? "🤝 Спасибо за покупку!" : "🤝 Thank you for the purchase!");
return;
});

botAiKoshey.command("language", async (ctx) => {
await ctx.replyWithChatAction("typing");
if (!ctx.from) throw new Error("User not found");
Expand Down Expand Up @@ -732,6 +755,21 @@ botAiKoshey.command("brain", async (ctx) => {
ctx.reply(lang ? "Чтобы использовать данную функцию, необходимо приобрести уровень water 🌊" : "To use this function, you need to purchase the water level 🌊")
})

botAiKoshey.command("top", async (ctx) => {
console.log("top");
await ctx.replyWithChatAction("typing");
if (!ctx.from) throw new Error("User not found");
const lang = await isRu(ctx)
const top10Users = await getTop10Users();
console.log(top10Users, "top10Users");
const leaderboardText = top10Users.map((user, index) => {
return `${index + 1}. ${user.username} - ${user.all} $IGLA`;
}).join('\n');

await ctx.reply(lang ? `Топ 10 пользователей:\n${leaderboardText}` : `Top 10 users:\n${leaderboardText}`);

})

botAiKoshey.on("message:voice", async (ctx) => {
// const voice = ctx.msg.voice;
// console.log(voice, "voice");
Expand Down Expand Up @@ -1053,6 +1091,41 @@ botAiKoshey.on("callback_query:data", async (ctx) => {
console.log(ctx);
const isHaveAnswer = callbackData.split("_").length === 4;

if (callbackData.startsWith("buy")) {
if (callbackData.endsWith("fire")) {
await ctx.replyWithInvoice(
lang ? "🔥 Огонь" : "🔥 Fire",
"Вы получить подписку уровня 'Огонь'",
"fire",
"", // Оставьте пустым для цифровых товаров
"XTR", // Используйте валюту Telegram Stars
[{ label: "Цена", amount: 1170 }], // Цена в центах (10.00 Stars)
);
return
}
if (callbackData.endsWith("water")) {
await ctx.replyWithInvoice(
lang ? "🌊 Вода" : "🌊 Water",
"Вы получить подписку уровня 'Вода'",
"water",
"", // Оставьте пустым для цифровых товаров
"XTR", // Используйте валюту Telegram Stars
[{ label: "Цена", amount: 12870 }], // Цена в центах (10.00 Stars)
);
return
}
if (callbackData.endsWith("copper_pipes")) {
await ctx.replyWithInvoice(
lang ? "🎺 Медные трубы" : "🎺 Copper pipes",
"Вы получить подписку уровня 'Медные трубы'",
"copper_pipes",
"", // Оставьте пустым для цифровых товаров
"XTR", // Используйте валюту Telegram Stars
[{ label: "Цена", amount: 129870 }], // Цена в центах (10.00 Stars)
);
return
}
}
if (callbackData === "select_russian") {
if (ctx.callbackQuery.from.id) {
console.log("editMessageReplyMarkup")
Expand Down Expand Up @@ -1639,6 +1712,14 @@ await botAiKoshey.api.setMyCommands([
command: "/voice",
description: "🎤 Add avatar's voice",
},
{
command: "/top",
description: "🏆 Top 10 users",
},
// {
// command: "/buy",
// description: "🛒 Buy subscription",
// },
// {
// command: "/reset_voice",
// description: "Reset voice ai-avatar",
Expand Down

0 comments on commit b10ff15

Please sign in to comment.