-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.ts
68 lines (62 loc) · 2.08 KB
/
process.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
import Queue from "@supercharge/queue-datastructure";
import wait from "waait";
import { logger } from "./logger.js";
import { handleMessage } from "./handler.js";
import { sendTokens } from "./faucet.js";
import { EncodeObject } from "@cosmjs/proto-signing";
import { Message } from "discord.js";
import {SyncedQueue} from "./queue.js";
export const processing = async (queue: SyncedQueue<Message>, faucetAddress: string) => {
while(true) {
const currentQueue = await queue.getAll()
const clonedQueue = new Queue<Message >(...currentQueue)
const queueSize = clonedQueue.size()
for (let i = 0; i < queueSize; i++) {
await queue.dequeue()
}
logger.log({
level: 'info',
message: `queueSize: ${queueSize}`
})
if (queueSize > 0) {
const parsedList = []
while(!clonedQueue.isEmpty()) {
const queuedDiscordMsg = clonedQueue.dequeue()
if (queuedDiscordMsg === null) {
logger.log({
level: 'error',
message: 'discord msg is null'
})
}
const handleResult = handleMessage(queuedDiscordMsg!, faucetAddress)
if (handleResult.status === 'success') {
parsedList.push({
data: handleResult.msg,
discordMsg: queuedDiscordMsg
})
} else {
queuedDiscordMsg?.reply(`:x: you cannot get faucet until 24hours.`)
}
}
const msgSends: EncodeObject[] = []
parsedList.forEach((parsed) => {
parsed.data.forEach((single) => {
msgSends.push(single)
})
})
if (msgSends.length > 0) {
const txResult = await sendTokens(msgSends)
Promise.all(
parsedList.map((parsed) => new Promise((resolve) => {
parsed.discordMsg?.reply(`:ballot_box_with_check: send 10NOVA and 10ibc tokens: <https://explorer.dev-supernova.xyz/supernova/transactions/${txResult.transactionHash}>`)
}))
).then((res) => console.log('finish...'))
}
}
logger.log({
level: 'info',
message: 'wait 10s'
})
await wait(10_000)
}
}