-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
150 lines (130 loc) · 4.8 KB
/
config.ts
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
import { LowSync, JSONFileSync } from 'lowdb';
import { InlineKeyboard, Keyboard } from "grammy";
import { amountToHumanString, bigNumberComparison } from "./tools/utils.js";
import User, { IUser } from "./src/models/user.js";
import mongoose from "mongoose";
import { CustomContext } from "./types/CustomContext.js";
import { ApiPromise } from "@polkadot/api";
import { KeyringPair } from "@polkadot/keyring/types";
import { Bot } from "grammy";
import { PinataClient } from "@pinata/sdk";
import { RunnerHandle } from '@grammyjs/runner';
type BotParams = {
api: ApiPromise,
remarkStorage: LowSync,
account: KeyringPair,
settings: any,
bot: Bot,
runnerHandle: RunnerHandle,
pinata: PinataClient;
};
export const botParams: BotParams = {
api: null,
remarkStorage: null,
account: null,
settings: null,
bot: null,
runnerHandle: null,
pinata: null
};
export const cancelSetupInlineKeyboard = new InlineKeyboard()
.text("❌ Cancel Setup");
export const cancelCollectInlineKeyboard = new InlineKeyboard()
.text("❌ Cancel Collection");
export const locationKeyboard = new Keyboard()
.requestLocation("📍 Send Location");
const creatorKeyboard = new Keyboard()
.text("🗞️ Create treasure 🗞️").row()
.text("✏️ Edit treasures").text("🚦 View stats").row()
.text("⬅️ Back to main menu");
const finderKeyboard = new Keyboard()
.text("📷 Collect treasure").requestLocation("🔍 Find treasures").row()
.text("🛍️ My treasures").row()
.text("⬅️ Back to main menu");
const accountLinkedKeyboard = new Keyboard()
.text("📪 Edit address").text("🧾 Withdraw").row()
.text("⬅️ Back to main menu");
const accountNoLinkedBalanceKeyboard = new Keyboard()
.text("🔗 Link address").text("🧾 Withdraw").row()
.text("📪 Edit address").row()
.text("⬅️ Back to main menu");
const accountNoLinkedKeyboard = new Keyboard()
.text("🔗 Link address").text("📪 Edit address").row()
.text("⬅️ Back to main menu");
const accountNoAddressKeyboard = new Keyboard()
.text("📪 Add address").row()
.text("⬅️ Back to main menu");
const mainKeyboard = new Keyboard()
.text("🧙🏻♀️ Creator Mode").row()
.text("🕵🏾♂️ Finder Mode").row()
.text("🛠️ Account Settings");
const getMainLinkedKeyboard = (userBalance: string): Keyboard => {
return new Keyboard()
.text("🧙🏻♀️ Creator Mode").row()
.text("🕵🏾♂️ Finder Mode").row()
.text(`🛠️ Account Settings \u2705 (${userBalance})`);
};
const getMainNoLinkedKeyboard = (userBalance: string): Keyboard => {
return new Keyboard()
.text("🧙🏻♀️ Creator Mode").row()
.text("🕵🏾♂️ Finder Mode").row()
.text(`🛠️ Account Settings \u274C (${userBalance})`);
};
const getMainRewardBalanceKeyboard = (userBalance: string): Keyboard => {
return new Keyboard()
.text("🧙🏻♀️ Creator Mode").row()
.text("🕵🏾♂️ Finder Mode").row()
.text(`🛠️ Account Settings (${userBalance})`);
};
export const getKeyboard = async (ctx: CustomContext): Promise<Keyboard> => {
const session = await ctx.session;
const user: IUser = await User.findOne({ chatId: ctx.chat.id });
const userBalance = user.getBalance();
switch (session.menu) {
case "finder":
return finderKeyboard;
case "creator":
return creatorKeyboard;
case "account":
if (user.wallet && user.wallet.address && user.wallet.linked) {
return accountLinkedKeyboard;
}
else if (user.wallet && user.wallet.address && !user.wallet.linked &&
userBalance === "0") {
return accountNoLinkedKeyboard;
}
else if (user.wallet && user.wallet.address && !user.wallet.linked &&
bigNumberComparison(userBalance, "0", ">")) {
return accountNoLinkedBalanceKeyboard;
}
return accountNoAddressKeyboard;
case "main":
if (user.wallet && user.wallet.address && user.wallet.linked) {
return getMainLinkedKeyboard(amountToHumanString(
userBalance, 4));
}
else if (user.wallet && user.wallet.address && !user.wallet.linked) {
return getMainNoLinkedKeyboard(amountToHumanString(
userBalance, 4));
}
else if (!user.wallet && bigNumberComparison(user.rewardBalance, "0", ">")) {
return getMainRewardBalanceKeyboard(amountToHumanString(user.rewardBalance, 4));
}
return mainKeyboard;
default:
return mainKeyboard;
}
};
export const getDb = async (): Promise<void> => {
const uri = process.env.MONGO_URI;
try {
await mongoose.connect(uri);
console.log('MongoDB Connected...');
} catch (err) {
console.log(err);
}
};
export const getRemarkStorage = (): LowSync => {
const db = new LowSync(new JSONFileSync(process.env.REMARK_STORAGE_DB_FILE_PATH));
return db;
};