This repository has been archived on 2024-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
QRQuest/index.js

62 lines
2.6 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 Database = require('./Database');
const session = require('telegraf/session');
let database = new Database(process.env.DATABASE_URL);
let bot = new Telegraf.Telegraf(process.env.TOKEN);
bot.use(session());
bot.start(async (ctx) => {
let code = ctx.message.text.split(" ")[1];
if (!code) {
await ctx.reply("Добро пожаловать на QR квест! Для участия используйте QR коды");
return;
}
await database.checkAndRegisterUser(ctx.from.id, ctx.from.first_name);
bot.telegram.getChatMember("@ks54_op5", ctx.from.id).then(async (member) => {
if(member.status === "left") {
await ctx.reply("Вы не состоите в @ks54_op5");
return;
}
bot.telegram.getChatMember("@studsovet_ks54", ctx.from.id).then(async (mem) => {
if (mem.status === "left") {
await ctx.reply("Вы не состоите в @studsovet_ks54");
return;
}
let exists = await database.codeExists(code);
if (!exists) {
await ctx.reply("Код не найден, возможно вы криво отсканировали QR код или пытаете его подделать");
return;
}
let used = await database.useCode(code, ctx.from.id);
if (used) {
let userCodes = await database.getUserCodes(ctx.from.id);
let totalCodes = await database.getTotalCodes();
await ctx.reply("Код успешно использован, собрано " + userCodes + " из " + totalCodes + " кодов");
await bot.telegram.sendMessage(-879242326, `<a href="tg://user?id=${ctx.from.id}">${ctx.from.first_name}</a> отсканировал код ${userCodes} из ${totalCodes}`, {parse_mode: "HTML"});
} else {
await ctx.reply("Код уже использован");
}
}).catch(async (err) => {
await ctx.reply("Вы не состоите в @studsovet_ks54");
});
}).catch(async (err) => {
await ctx.reply("Вы не состоите в @ks54_op5");
});
});
bot.command("top", async (ctx) => {
if (ctx.chat.id !== -879242326) return;
let top = await database.getTop();
let message = "Топ игроков:\n";
for (let i = 0; i < top.length; i++) {
message += `${(i + 1)}. <a href="tg://user?id=${top[i].id}">${top[i].name}</a> - ${top[i].count} кодов\n`;
}
await ctx.reply(message, {parse_mode: "HTML"});
});
bot.startPolling();