Download src/controllers/tagController.ts from ItsMa8di/idbot: direct link, hf CLI and curl.
- Browser
- Download file 5.8 kB
-
https://huggingface.co/spaces/ItsMa8di/idbot/resolve/main/src/controllers/tagController.ts
- Command line
-
hf download hf://spaces/ItsMa8di/idbot/src/controllers/tagController.ts
-
curl -L -o tagController.ts https://huggingface.co/spaces/ItsMa8di/idbot/resolve/main/src/controllers/tagController.ts
5.8 kB
| import { BotContext, FetchError } from "../../types"; | |
| import { IUser } from "../models/User"; | |
| import PlayerSevices from "../services/playerSevices"; | |
| import playerToString from "../utils/playerToString"; | |
| import tagValidator from "../utils/tagValidator"; | |
| import { UserController } from "./userController"; | |
| class TagController { | |
| static async sendTutorial(ctx: BotContext) { | |
| try { | |
| const messageId: number | undefined = ctx.message?.message_id; | |
| if (!messageId) return; | |
| const sentTutorial = await ctx.reply( | |
| `برای استفاده از ربات به صورت زیر عمل کنید:\n\nid <#ABC123>\n\nتگ خود را به صورت درست وارد کنید و صبور باشید.\nتگ شما در قسمت پروفایل اکانت قابل مشاهده می باشد\nمیخوای تگ اکانتت رو ذخیره کنی تا هر دفعه نخوای بنویسی؟؟\nبا کامند زیر این کار انجام میشه:\n\nid #ABC123 save\nبه جای تگ پیشفرض تگ اکانت خودتو بنویس و بعدش هم کلمه ی "save" رو بنویس.\n\nاگر هم میخوای اکانتی که سیو کردی رو ببینی کامند زیر رو بفرست\nme$ یا account$\n\nبرای حذف کردن اکانتی که سیو کردی از این کامند استفاده کن:\nme$ remove یا account$ remove`, | |
| { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }, | |
| ); | |
| setTimeout(() => { | |
| ctx.api.deleteMessage(ctx.chat?.id!!, sentTutorial.message_id); | |
| ctx.deleteMessage(); | |
| }, 2000); | |
| } catch (error) { | |
| console.error(error); | |
| //ctx.reply(`یک خطای غیر منتظره رخ داد`) | |
| return; | |
| } | |
| } | |
| private static async errorHandler(ctx: BotContext, error: FetchError) { | |
| try { | |
| const messageId = ctx.message?.message_id; | |
| if (!messageId) return; | |
| const status = error.status; | |
| switch (status) { | |
| case 404: | |
| ctx.reply(`اکانت مورد نظر یافت نشد`, { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }); | |
| return; | |
| default: | |
| ctx.reply(`یک خطای داخلی رخ داد`, { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }); | |
| } | |
| } catch (error) { | |
| console.error(error); | |
| return; | |
| } | |
| } | |
| private static async fetch(ctx: BotContext, tag: string) { | |
| try { | |
| const messageId = ctx.message?.message_id; | |
| if (!messageId) return; | |
| const response = await PlayerSevices.fetch(tag); | |
| if ("error" in response) { | |
| this.errorHandler(ctx, response); | |
| return; | |
| } | |
| const toString = playerToString(response); | |
| ctx.reply(toString, { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }); | |
| } catch (error) { | |
| console.error(error); | |
| return; | |
| } | |
| } | |
| static async checkTag(ctx: BotContext, tag: string | undefined) { | |
| try { | |
| const text = ctx.message?.text; | |
| const trimedText = tag?.trim().replace("#", "").replace(/o/g, "0"); | |
| const messageId: number | undefined = ctx.message?.message_id; | |
| if (!messageId) return; | |
| if (!trimedText) return; | |
| const isValidTag = tagValidator(trimedText); | |
| if (!isValidTag) { | |
| const sentError = await ctx.reply( | |
| `کاربر گرامی تگ ارسالی معتبر نمی باشد.\n\nتگ ارسالی حتما باید حروف انگلیسی باشد.\nنکته: تگ شما در قسمت پروفایل قابل مشاهده می باشد.`, | |
| { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }, | |
| ); | |
| setTimeout(() => { | |
| ctx.api.deleteMessage(ctx.chat?.id!!, sentError.message_id); | |
| ctx.deleteMessage(); | |
| }, 2000); | |
| return; | |
| } | |
| ctx.session.nextTime = Date.now() + 60 * 1000; | |
| tag && this.fetch(ctx, tag); | |
| } catch (error) { | |
| console.error(error); | |
| return; | |
| } | |
| } | |
| static async saveTag(ctx: BotContext, userid: number, tag: string) { | |
| try { | |
| const trimedText: string = tag.replace("#", "").replace(/o/g, "0").trim(); | |
| const messageId: number | undefined = ctx?.message?.message_id; | |
| if (!messageId) return; | |
| const isValidTag = tagValidator(trimedText); | |
| if (!isValidTag) { | |
| const sentError = await ctx.reply( | |
| `کاربر گرامی تگ ارسالی معتبر نمی باشد.\n\nتگ ارسالی حتما باید حروف انگلیسی باشد.\nنکته: تگ شما در قسمت پروفایل قابل مشاهده می باشد.`, | |
| { | |
| reply_parameters: { | |
| message_id: messageId, | |
| }, | |
| }, | |
| ); | |
| setTimeout(() => { | |
| ctx.api.deleteMessage(ctx.chat?.id!!, sentError.message_id); | |
| ctx.deleteMessage(); | |
| }, 2000); | |
| return; | |
| } | |
| ctx.session.nextTime = Date.now() + 60 * 1000; | |
| //TODO : SAVE THE TAG TO USER | |
| // | |
| const user: IUser | undefined = await UserController.getByUserid(userid); | |
| console.log(user); | |
| if (user) { | |
| const updatedUser: IUser = { | |
| ...(user as IUser), | |
| savedAccountTag: trimedText, | |
| }; | |
| await UserController.updateByUserid(userid, updatedUser); | |
| await ctx.reply(`اکانت شما با موفقیت ذخیره شد\n${trimedText}`, { | |
| reply_parameters: { message_id: ctx.msgId as number }, | |
| }); | |
| // SEND MESSAGE | |
| //tag && this.fetch(ctx, trimedText); | |
| } | |
| } catch (e) { | |
| console.error(e); | |
| return; | |
| } | |
| } | |
| } | |
| export default TagController; | |