-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
140 lines (123 loc) · 4.52 KB
/
utils.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
const TelegramApi = require('node-telegram-bot-api')
const { Client, Collection, Events, GatewayIntentBits, userMention } = require('discord.js');
const { UserTg } = require('./models');
const getTxRs = require('./getTxResult');
const { faucetsToken } = require('./api');
const sendToken = async (address, callBack) => {
try {
const originRes = await faucetsToken({
address: address
})
const res = originRes.data;
if (res.code === '20000') {
callBack({
status: 'pending'
})
const transactionId = res.data.transactionId
await getTxRs(transactionId)
callBack({
status: 'success',
data: res.data
})
} else {
callBack({
status: 'error',
message: res.message
})
}
} catch (error) {
callBack({
status: 'networkError'
})
}
}
const insertItem = async (chatId, address, platForm, txId, txStatus) => {
try {
await UserTg.create({
id: null,
address: address,
chat_id: chatId,
chat_platform: platForm,
tx_id: txId,
status: txStatus,
tx_date: Date.now()
})
} catch (e) {
throw Error('insert data fail')
}
}
const updateItem = async (chatId, address) => {
try {
await UserTg.update({ status: 'mined' }, {
where: {
chat_id: chatId,
address: address
}
});
} catch (e) {
throw Error('update data fail')
}
}
const prefix = `!faucet cat`;
const operateBotFromPlatForm = (platForm, token, clientId) => {
let robot = {}
switch (platForm) {
case 'telegram':
robot = new TelegramApi(token, { polling: true })
robot.onText(/\!faucet cat (.+)/, async (msg, match) => {
const chatId = msg.chat.id;
const address = match[1];
// const users = await UserTg.findAll({
// where: {
// chat_id: chatId,
// address: address
// }
// });
// if(!users || users.length === 0){
// sendToken(chatId, address, true)
// } else if(users[0].status !== 'mined') {
// sendToken(chatId, address, false)
// } else {
// sendMsg(chatId, 'This address has already received test coins and cannot receive them anymore.');
// }
});
break;
case 'discord':
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
client.on(Events.MessageCreate, async (interaction) => {
const content = interaction.content.trim();
if (!content.startsWith(prefix) || interaction.author.id === clientId) return;
const address = content.split(prefix)[1].trim().replace(/^ELF_/, '').replace(/_.*$/, '');
// readyClient
const id = interaction.author.id;
const user = userMention(id);
console.log(`|${address}|`)
sendToken(address, (res) => {
const { status, message } = res;
if (status === 'success') {
const { amount, symbol, address } = res.data;
interaction.reply(`Hi ${user},${amount} ${symbol} is sent to your wallet address ${address}, please check.
Note: Each address can only claim once.`);
} else if (status === 'error') {
interaction.reply(`${user},${message}`);
} else if (status === 'networkError') {
interaction.reply(`Hi ${user},the transaction to your wallet address ${address} failed due to network or server issues. Please try again.`);
}
})
});
client.login(token).then(() => {
console.log('success login')
})
break;
default:
throw Error('do not support this platForm')
}
}
module.exports = { insertItem, updateItem, operateBotFromPlatForm };