Skip to content

Commit cf0b069

Browse files
committed
Sent message to indicate the queue was cleared
1 parent 871774d commit cf0b069

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

src/index.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ import path from 'node:path'
1616
import qrcode from 'qrcode-terminal'
1717

1818
// ---------------------------------------------------------------------------
19-
// ENTRY POINT
19+
// GLOBAL SOCKET INSTANCE
2020
// ---------------------------------------------------------------------------
21+
let socket: WASocket
22+
23+
// ---------------------------------------------------------------------------
24+
// ENTRY POINT
25+
// ---------------------------------------------------------------------------
2126
;(async () => {
2227
Logger.setDebug(false)
2328
Logger.info(Color.Yellow, '🤖 Starting ...')
@@ -31,6 +36,13 @@ import qrcode from 'qrcode-terminal'
3136
await deleteQueueFile()
3237
GROUP_QUEUES.clear()
3338
PROCESSED_MESSAGES.clear()
39+
const groups = await socket.groupFetchAllParticipating()
40+
for (const groupId of Object.keys(groups)) {
41+
await postInGroup(
42+
groupId,
43+
'The queue was cleared. Send `/join` to join the queue, `/help` for the list of commands.',
44+
)
45+
}
3446
},
3547
{ timezone: 'Europe/Dublin' },
3648
)
@@ -67,7 +79,7 @@ async function start() {
6779
isLatest ? '(up-to-date)' : '(not latest?)',
6880
)
6981

70-
const socket = makeWASocket({
82+
socket = makeWASocket({
7183
version: version,
7284
browser: Browsers.macOS('Chrome'),
7385
auth: state,
@@ -95,7 +107,7 @@ async function start() {
95107

96108
socket.ev.on('messages.upsert', async (m) => {
97109
try {
98-
await handleUserMessages(m, socket)
110+
await handleUserMessages(m)
99111
} catch (err) {
100112
Logger.error('Error while handling messages')
101113
finish(err)
@@ -112,7 +124,7 @@ async function start() {
112124
finish()
113125
}
114126
if (connection === 'open') {
115-
Logger.info('✅ Connected!')
127+
Logger.info('✅ Connected!')
116128
}
117129
})
118130

@@ -131,14 +143,11 @@ async function start() {
131143
// ---------------------------------------------------------------------------
132144
// MESSAGE HANDLING
133145
// ---------------------------------------------------------------------------
134-
async function handleUserMessages(
135-
m: {
136-
messages: WAMessage[]
137-
type: MessageUpsertType
138-
requestId?: string
139-
},
140-
socket: WASocket,
141-
) {
146+
async function handleUserMessages(m: {
147+
messages: WAMessage[]
148+
type: MessageUpsertType
149+
requestId?: string
150+
}) {
142151
if (!m.messages || m.messages.length === 0) {
143152
Logger.debug('No messages, ignoring')
144153
return
@@ -197,7 +206,7 @@ async function handleUserMessages(
197206
Logger.info(Color.Green, `User ID: ${userId}`)
198207
Logger.info(Color.Green, `Group ID: ${groupId}`)
199208
PROCESSED_MESSAGES.add(uniqueId)
200-
await handleCommand(groupId, msgKey, msg, socket, text, userId, username)
209+
await handleCommand(groupId, msgKey, msg, text, userId, username)
201210
}
202211
}
203212

@@ -208,7 +217,6 @@ async function handleCommand(
208217
groupId: string,
209218
msgKey: IMessageKey,
210219
msg: WAMessage,
211-
socket: WASocket,
212220
command: string,
213221
userId: string,
214222
username: string,
@@ -219,14 +227,13 @@ async function handleCommand(
219227
await replyInGroup(
220228
groupId,
221229
msg,
222-
socket,
223230
`Available commands:
224231
* \`/help\` (\`/h\`) → Display this menu
225232
* \`/join\` (\`/j\`) → Join the queue
226233
* \`/leave\` (\`/l\`) → Leave the queue
227234
* \`/check\` (\`/c\`) → Check the queue`,
228235
)
229-
await reactInGroup(groupId, msgKey, socket, '🆘')
236+
await reactInGroup(groupId, msgKey, '🆘')
230237
break
231238

232239
case '/join':
@@ -236,21 +243,19 @@ async function handleCommand(
236243
await replyInGroup(
237244
groupId,
238245
msg,
239-
socket,
240246
`${username}, you joined the queue:\n${formatQueueWithMentions(groupId)}`,
241247
getQueueMentions(groupId),
242248
)
243-
await reactInGroup(groupId, msgKey, socket, '👍')
249+
await reactInGroup(groupId, msgKey, '👍')
244250
await writeQueueFile()
245251
} else {
246252
await replyInGroup(
247253
groupId,
248254
msg,
249-
socket,
250255
`${username}, you're already in the queue:\n${formatQueueWithMentions(groupId)}`,
251256
getQueueMentions(groupId),
252257
)
253-
await reactInGroup(groupId, msgKey, socket, '❌')
258+
await reactInGroup(groupId, msgKey, '❌')
254259
}
255260
break
256261

@@ -260,21 +265,19 @@ async function handleCommand(
260265
await replyInGroup(
261266
groupId,
262267
msg,
263-
socket,
264268
`${username}, you're not in the queue:\n${formatQueueWithMentions(groupId)}`,
265269
getQueueMentions(groupId),
266270
)
267-
await reactInGroup(groupId, msgKey, socket, '❌')
271+
await reactInGroup(groupId, msgKey, '❌')
268272
} else {
269273
removeUserFromQueue(groupId, userId)
270274
await replyInGroup(
271275
groupId,
272276
msg,
273-
socket,
274277
`${username}, you left the queue:\n${formatQueueWithMentions(groupId)}`,
275278
getQueueMentions(groupId),
276279
)
277-
await reactInGroup(groupId, msgKey, socket, '👋')
280+
await reactInGroup(groupId, msgKey, '👋')
278281
await writeQueueFile()
279282
}
280283
break
@@ -284,21 +287,19 @@ async function handleCommand(
284287
await replyInGroup(
285288
groupId,
286289
msg,
287-
socket,
288290
`Queue:\n${formatQueueWithMentions(groupId)}`,
289291
getQueueMentions(groupId),
290292
)
291-
await reactInGroup(groupId, msgKey, socket, '👀')
293+
await reactInGroup(groupId, msgKey, '👀')
292294
break
293295

294296
default:
295297
await replyInGroup(
296298
groupId,
297299
msg,
298-
socket,
299-
`Unknown command. Send \`/help\` for the list of commands.`,
300+
'Unknown command. Send `/help` for the list of commands.',
300301
)
301-
await reactInGroup(groupId, msgKey, socket, '❌')
302+
await reactInGroup(groupId, msgKey, '❌')
302303
}
303304
}
304305

@@ -392,7 +393,7 @@ async function deleteQueueFile(): Promise<void> {
392393
// ---------------------------------------------------------------------------
393394
// WA MESSAGE HELPERS
394395
// ---------------------------------------------------------------------------
395-
const PROCESSED_MESSAGES = new Set()
396+
const PROCESSED_MESSAGES = new Set<string>()
396397

397398
function userIdToMention(userId: string): string {
398399
const numberPart = userId.split('@')[0]
@@ -415,7 +416,6 @@ function getQueueMentions(groupId: string): string[] {
415416
async function replyInGroup(
416417
groupId: string,
417418
msg: WAMessage,
418-
socket: WASocket,
419419
text: string,
420420
mentions: string[] = [],
421421
): Promise<void> {
@@ -429,10 +429,19 @@ async function replyInGroup(
429429
)
430430
}
431431

432+
async function postInGroup(groupId: string, text: string): Promise<void> {
433+
await socket.sendMessage(
434+
groupId,
435+
{ text: text },
436+
{
437+
ephemeralExpiration: 86400, // 24 hours
438+
},
439+
)
440+
}
441+
432442
async function reactInGroup(
433443
groupId: string,
434444
msgKey: IMessageKey,
435-
socket: WASocket,
436445
text: string,
437446
): Promise<void> {
438447
await socket.sendMessage(groupId, {
@@ -450,5 +459,5 @@ process.on('unhandledRejection', (reason) => {
450459
Logger.error('Unhandled rejection: ', reason)
451460
})
452461
process.on('uncaughtException', (error) => {
453-
Logger.error('Uncaught exception: ', error)
462+
Logger.error('Uncaught exception: ', error)
454463
})

0 commit comments

Comments
 (0)