This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.ts
130 lines (123 loc) · 3.22 KB
/
features.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
import { getIdList, generateMessageList } from './utils'
import { readStackFromGroup, putStackToGroup } from './database'
import { sendMessage, pinMessage, getChatMember } from './telegram'
const BOT_KEY = process.env.TELEGRAM_TOKEN || ''
async function validateState(msg: any, chat_id: number): Promise<boolean> {
const my_perm = await getChatMember(BOT_KEY, chat_id, 1189170779)
if (!my_perm.can_pin_messages) {
console.log(`Pinning nothing for ${chat_id}`)
await sendMessage(
BOT_KEY,
msg.chat.id,
"I can't pin messages here. Please check the permissions.",
msg.message_id
)
return false
}
return true
}
export async function replyWithStack(msg: any, chat_id: number) {
console.log(`Viewing the pin stack for ${chat_id}`)
let stack = await readStackFromGroup(chat_id)
if (stack.length === 0) {
await sendMessage(
BOT_KEY,
msg.chat.id,
`Current pin stack is empty.`,
msg.message_id
)
} else {
await sendMessage(
BOT_KEY,
msg.chat.id,
`Current pin stack: ${generateMessageList(
stack,
msg.chat.id,
msg.chat.username
)}.`,
msg.message_id,
true
)
}
}
export async function pinFirst(
msg: any,
chat_id: number,
deleteTop: boolean = true
) {
if ((await validateState(msg, chat_id)) === false) {
return
}
let stack = await readStackFromGroup(chat_id)
if (stack.length === 0) {
await sendMessage(BOT_KEY, chat_id, `The stack is empty`, msg.message_id)
return
}
stack.pop()
let pinId = stack[stack.length - 1]
console.log(`Pinning ${pinId} for ${chat_id}`)
await pinMessage(
BOT_KEY,
chat_id,
pinId,
msg.text.includes('notify'),
msg.message_id
)
if (deleteTop) {
await putStackToGroup(chat_id, stack)
}
}
export async function clearmsg(msg: any, chat_id: number) {
console.log(`Cleaning for ${chat_id}`)
await Promise.all([
putStackToGroup(chat_id, []),
sendMessage(BOT_KEY, chat_id, `Done.`, msg.message_id),
])
}
export async function deltop(msg: any, chat_id: number) {
let stack = await readStackFromGroup(chat_id)
stack.pop()
console.log(`Removing top for ${chat_id}`)
await Promise.all([
putStackToGroup(chat_id, stack),
sendMessage(BOT_KEY, chat_id, `Done.`, msg.message_id),
])
}
export async function push(msg: any, chat_id: number) {
if ((await validateState(msg, chat_id)) === false) {
return
}
let pushIdList
let finalPinMessage = -1
if (msg.reply_to_message) {
pushIdList = [msg.reply_to_message.message_id]
} else {
pushIdList = getIdList(msg.text)
}
for (const pushId of pushIdList) {
console.log(`Pushing ${pushId} for ${chat_id}`)
let stack = await readStackFromGroup(chat_id)
if (stack[stack.length - 1] !== pushId) {
stack.push(pushId)
await putStackToGroup(chat_id, stack)
}
finalPinMessage = pushId
}
if (pushIdList.length === 0) {
console.log(`Pushing nothing for ${chat_id}`)
await sendMessage(
BOT_KEY,
chat_id,
`No valid message id found :(`,
msg.message_id
)
} else {
await pinMessage(
BOT_KEY,
chat_id,
finalPinMessage,
msg.text.includes('notify'),
msg.message_id
)
}
}