-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot_group.js
238 lines (167 loc) · 7.83 KB
/
bot_group.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as instance from './bot.js'
import dotenv from 'dotenv'
import assert from 'assert';
import * as utils from './utils.js';
dotenv.config()
const getWelcomeMessage = () => {
return `This is the official alphAi bot deployed by <a href="http://alphai.site/">${process.env.BOT_NAME}</a>.
Only group owner can manipulate this bot via the following commands:
/${instance.COMMAND_START}@${instance.myInfo.username} - welcome 😀
/${instance.COMMAND_STARTKICK}@${instance.myInfo.username}- remove users from this group who sold tokens or hold less than 2.5 M tokens ❌
/${instance.COMMAND_STOPKICK}@${instance.myInfo.username}- stop removing users from this group who sold tokens or hold less than 2.5 M tokens ❌
Tutorial: Coming Soon
Important:
- To receive notifications or stay in the group, users hold at least ${instance.MIN_COMUNITY_TOKEN_AMOUNT} community tokens. Here's the <a href="https://www.dextools.io/app/en/ether/pair-explorer/0x695051b0028d02172d0204c964b293d7b25b6710">link</a> to buy
- In order to use this bot, group owner need to create a Telegram ID if he or she haven't already.
More commands will be added as the community grows. Please stay tuned for updates.`;
}
function sendGroupAuthMessage(session) {
instance.sendMessage(session.from_chatid, `Please login <a href="${process.env.API_URI}/login?chatid=${session.chatid}">here</a> for the @${session.username} group subscription`)
}
const getGroupSuspensionMessage = () => {
return `Hello users! We are sorry to let you know that we can no longer support our bot in the group chat. Please add the bot in your private chat instead. Thank you for your understanding`
}
export const procMessage = async (msg, database) => {
const pub_chatid = msg?.chat?.id.toString();
const from_chatid = msg?.from?.id.toString();
let session = instance.sessions.get(pub_chatid)
if (msg.new_chat_title) {
// Changed the Group title
if (session) {
session.username = msg.new_chat_title
await database.updateUser(session)
}
return
}
if (msg.left_chat_participant) {
// This bot has been kicked out
if (msg.left_chat_participant.id.toString() === instance.myInfo.id.toString()) {
if (session) {
await database.removeUser(session);
instance.sessions.delete(session.chatid);
}
}
return
}
if (msg.new_chat_participant) {
const newParticipant = msg.new_chat_participant
const newParticipantId = newParticipant.id.toString()
if (newParticipant.is_bot === false && session) {
let newSession = await database.selectUser({chatid: newParticipantId})
if (newSession) {
let communityTokenBalance = await utils.getTokenBalanceFromWallet(utils.web3Inst, newSession.wallet, process.env.COMUNITY_TOKEN);
if (communityTokenBalance < Number(process.env.MIN_COMUNITY_TOKEN_AMOUNT) && newSession.vip !== 1) {
setTimeout(async () => {
try {
const res = await instance.bot.unbanChatMember(session.chatid, newSession.chatid)
if (res) {
const message = `Hi @${newParticipant.username}!
In order to keep stay in the group, possessing a minimum of ${process.env.MIN_COMUNITY_TOKEN_AMOUNT} units of the community's token in your wallet is requisite. You are removed from the group. Please login again to the @alphAI_Token_Bot.
Please login to @alphAI_Token_Bot. Thank you for understanding`
instance.sendMessage(newParticipantId, message)
instance.sendMessage(session.chatid, `New joinee, @${newParticipant.username} has been kicked out from this group due insufficient balance of community tokens.`)
console.log(`New joinee, @${newParticipant.username} has been kicked out from the group(${session.username})`)
}
} catch (err) {
}
}
, 10000)
}
} else {
instance.sendMessage(newParticipantId, 'Please do login to the alphAI bot using /login command')
const message = `@${newParticipant.username}! Welcome to the group! We've noticed you are new joinee. We require you login to the @alphAI_Token_Bot first`
instance.sendMessage(session.chatid, message)
console.log(`@${newParticipant.username} is looking around the group (${session.username})`)
}
}
}
let groupName = msg?.chat?.title;
if (!msg.text || !msg.entities) {
return;
}
let command = msg.text;
for (const entity of msg.entities) {
if (entity.type === 'bot_command') {
command = command.substring(entity.offset, entity.offset + entity.length);
break;
}
}
let params = ''
if (!command.includes('@')) {
return;
}
const parts = command.split('@');
if (parts.length !== 2) {
return;
}
command = parts[0];
if (parts[1] !== instance.myInfo.username) {
return;
}
if (!from_chatid || !groupName) {
return;
}
let chatMember = null;
try {
chatMember = await instance.bot.getChatMember(pub_chatid, from_chatid);
} catch (err) {
return;
}
let isOwner = (chatMember.status === 'creator' || chatMember.status === 'administrator');
let isGroupCreatedOrBotAdded = (msg?.group_chat_created || msg?.supergroup_chat_created || msg?.new_chat_member?.id === instance.myInfo.id );
let isBotLeftFromGroup = (msg.left_chat_member && msg.left_chat_member.id === bot.options.id) || (msg.banned_chat_member && msg.banned_chat_member.id === bot.options.id);
if (isBotLeftFromGroup && session) {
await database.removeGroup(session);
instance.sessions.delete(session.chatid);
instance.sendMessage(pub_chatid, 'Bot has been left the group chat');
return;
} else if (!session) {
if (!groupName) {
console.log(`Rejected anonymous group incoming connection. chatid = ${pub_chatid}`);
//instance.sendMessage(chatid, `Welcome to alphAI bot. We noticed that your group does not have a username. Please create it and try again. If you have any questions, feel free to ask the developer team at @PurpleDragon999. Thank you.`)
return;
}
// instance.sendMessage(pub_chatid, getGroupSuspensionMessage())
// return
// if (false && !await instance.checkWhitelist(groupName)) {
// //instance.sendMessage(from_chatid, `😇Sorry, but @${groupName} group does not have permission to use alphBot. If this group would like to use this bot, please contact the developer team at ${process.env.TEAM_TELEGRAM}. Thanks!`);
// console.log(`Rejected anonymous incoming connection. @${groupName}, ${pub_chatid}, ${from_chatid}`);
// return;
// }
console.log(`@${groupName} session has been permitted through whitelist`);
session = instance.createSession(pub_chatid, groupName, 'group');
session.permit = 1;
session.from_chatid = from_chatid;
await database.updateUser(session)
} else {
if (session.adminid !== from_chatid) {
session.adminid = from_chatid;
await database.updateUser(session)
}
}
if (isGroupCreatedOrBotAdded) {
//sendOptionMessage(pub_chatid, getWelcomeMessage(true), json_botSettings(from_chatid));
instance.sendMessage(pub_chatid, 'Dear members! I am here for removing users from the group who have less than 2.5 million alphAI tokens. Thank you for your understanding.')
// instance.sendMessage(pub_chatid, getGroupSuspensionMessage())
return;
}
if (!isOwner) {
//instance.sendMessage(from_chatid, 'Only group owner can control the bot.');
return;
}
//instance.sendMessage(pub_chatid, getGroupSuspensionMessage())
command = command.slice(1);
if (command === instance.COMMAND_START) {
instance.sendMessage(pub_chatid, getWelcomeMessage())
} else if (command === instance.COMMAND_STARTKICK) {
instance.sendMessage(pub_chatid, 'Kick mode enabled')
console.log(`Kick mode enabled in the group @${session.username}`)
session.kickmode = 1
await database.updateUser(session)
} else if (command === instance.COMMAND_STOPKICK) {
session.kickmode = undefined
await database.updateUser(session)
console.log(`Kick mode disabled in the group @${session.username}`)
instance.sendMessage(pub_chatid, 'Kick mode disabled')
}
}