botService.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { MemberModel, IMember, FaucetModel, IFaucet } from './db';
  2. import axios from 'axios';
  3. import puppeteer from 'puppeteer';
  4. import TelegramBot from 'node-telegram-bot-api';
  5. import { BotServiceProps } from './types';
  6. import AccountTransferTokens from './commands/transferTokens';
  7. import startCommand from './commands/start';
  8. import inductedCommand from './commands/inducted';
  9. import faucetCommand from './commands/faucet';
  10. import getFmInfo from './getFmInfo';
  11. const prod = process.env.PRODUCTION === 'true';
  12. async function lookupCommand(
  13. member: IMember | null,
  14. message: any,
  15. props: BotServiceProps
  16. ) {
  17. const regexp = new RegExp(`^${props.commandPrefix}lookup(.*)`);
  18. const regexpMatch = new RegExp(`${props.commandPrefix}lookup (.*)`);
  19. if (regexp.test(props.getText(message))) {
  20. const match = props.getText(message).match(regexpMatch);
  21. // console.log('message', message);
  22. const handle = match ? match[1] : member?.handle;
  23. if (handle) {
  24. const fmInfo = await getFmInfo();
  25. const memberData = fmInfo.data.scores.totalScores.find(
  26. (m: any) => m.memberHandle === handle
  27. );
  28. if (memberData) {
  29. const memberDataStr =
  30. `From data processed berofe: ${new Date(
  31. fmInfo.data.scores.cutoff
  32. ).toLocaleDateString()}\n\n` +
  33. `Direct Score = *${memberData.totalDirectScore}*\n` +
  34. `Referral Score = *${memberData.totalReferralScore}*\n` +
  35. `Total Score = *${memberData.totalScore}*\n\n` +
  36. memberData.directScores
  37. .map((m: any, index: number) => `Period ${index} = *${m || 0}*`)
  38. .join('\n');
  39. await props.send(message, memberDataStr);
  40. } else {
  41. await props.send(
  42. message,
  43. `Don't find member ${handle}. Please note that the handle is case sensitive.`
  44. );
  45. }
  46. } else {
  47. await props.send(message, 'Please, set your handle.');
  48. }
  49. }
  50. }
  51. async function setHandleCommand(
  52. member: IMember | null,
  53. message: any,
  54. props: BotServiceProps
  55. ) {
  56. const regexp = new RegExp(`^${props.commandPrefix}sethandle`);
  57. if (regexp.test(props.getText(message))) {
  58. props.log('sethandle');
  59. if (member === null) {
  60. const newMember = {
  61. [props.dbId]: props.getId(message),
  62. date: props.getDate(message),
  63. lastCommand: 'sethandle',
  64. };
  65. await MemberModel.create(newMember);
  66. } else {
  67. await MemberModel.updateOne(
  68. { [props.dbId]: props.getId(message) },
  69. { $set: { lastCommand: 'sethandle' } }
  70. );
  71. }
  72. await props.send(
  73. message,
  74. 'Write your handle. Please note that the handle is case sensitive.'
  75. );
  76. } else if (member && member.lastCommand === 'sethandle') {
  77. const updMember = await MemberModel.findOne({
  78. handle: props.getText(message),
  79. });
  80. if (updMember) {
  81. await MemberModel.remove({
  82. [props.dbId]: props.getId(message),
  83. handle: undefined,
  84. });
  85. await updMember.updateOne({
  86. $set: {
  87. lastCommand: undefined,
  88. [props.dbId]: props.getId(message),
  89. handle: props.getText(message),
  90. },
  91. });
  92. } else {
  93. await MemberModel.updateOne(
  94. { [props.dbId]: props.getId(message) },
  95. { $set: { lastCommand: undefined, handle: props.getText(message) } }
  96. );
  97. }
  98. await props.send(message, 'Good! Now you can get statistics =)');
  99. }
  100. }
  101. async function checkFaucetBalanceCommand(message: any, props: BotServiceProps) {
  102. const regexp = new RegExp(`^${props.commandPrefix}balancefaucet`);
  103. if (regexp.test(props.getText(message))) {
  104. const transferTokens = new AccountTransferTokens();
  105. const balance = await transferTokens.getBalance();
  106. // console.log('balance', balance);
  107. await props.send(message, `Balance = ${balance}`);
  108. }
  109. }
  110. // enable notification about expiring balance
  111. async function enableFaucetBalanceNotifyTgCommand(
  112. member: IMember | null,
  113. message: TelegramBot.Message,
  114. props: BotServiceProps
  115. ) {
  116. changeFaucetBalanceNotifyTg(member, message, props, 'enablefaucetnotify', true);
  117. }
  118. async function disableFaucetBalanceNotifyTgCommand(
  119. member: IMember | null,
  120. message: TelegramBot.Message,
  121. props: BotServiceProps
  122. ) {
  123. changeFaucetBalanceNotifyTg(member, message, props, 'disablefaucetnotify', false);
  124. }
  125. async function changeFaucetBalanceNotifyTg(
  126. member: IMember | null,
  127. message: TelegramBot.Message,
  128. props: BotServiceProps,
  129. command = '',
  130. enableNotify = true,
  131. ) {
  132. const regexp = new RegExp(`^${props.commandPrefix}${command}`);
  133. if (regexp.test(props.getText(message))) {
  134. if (member) {
  135. await MemberModel.updateOne(
  136. { [props.dbId]: props.getId(message) },
  137. { $set: { lastCommand: '', enableNotify: enableNotify ? (new Date().getTime() - 1000 * 60 * 60 * 24) : undefined } }
  138. );
  139. // console.log('message', message);
  140. await props.send(message, `Notifies ${enableNotify ? 'enabled' : 'disabled'}`);
  141. } else {
  142. await props.send(message, 'Set your handle througs /sethandle');
  143. }
  144. }
  145. }
  146. export default async function BotService(_bot, props: BotServiceProps) {
  147. props.client.on('message', async (message: any) => {
  148. const id = props.getId(message);
  149. let member = null;
  150. try {
  151. if (props.commandPrefix === '/' && !props?.isPrivate(message)) {
  152. props.deleteMessage(_bot, message);
  153. }
  154. } catch (ex) {
  155. console.log('ex', ex);
  156. }
  157. try {
  158. member = await MemberModel.findOne({ [props.dbId]: id });
  159. console.log('member =>', member);
  160. } catch (e) {
  161. console.log(e);
  162. await props.send(message, 'Error =( please try later');
  163. }
  164. console.log('message', message);
  165. try {
  166. startCommand(message, props);
  167. lookupCommand(member, message, props);
  168. await faucetCommand(member, message, props);
  169. if (props.commandPrefix === '/') {
  170. setHandleCommand(member, message, props);
  171. checkFaucetBalanceCommand(message, props);
  172. enableFaucetBalanceNotifyTgCommand(member, message, props);
  173. disableFaucetBalanceNotifyTgCommand(member, message, props);
  174. }
  175. } catch (ex) {
  176. console.log('ex', ex);
  177. await props.send(message, ex.message);
  178. }
  179. });
  180. }