File size: 5,800 Bytes
a8ce9cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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;