|
| 1 | +import { getWalletHoldings } from '../integration/xrpl/getWalletHoldings.js'; |
| 2 | +import { updateUserWallet } from '../data/updateUserWallet.js'; |
| 3 | +import { updateUserRoles } from '../integration/discord/updateUserRoles.js'; |
| 4 | +import { Client, User } from 'discord.js'; |
| 5 | +import truncate from '../utils/truncate.js'; |
| 6 | +import { WalletUpdateResponse } from '../models/enum/WalletUpdateResponse.js'; |
| 7 | + |
| 8 | +const linkWalletToDiscordAccount = async ( |
| 9 | + walletAddress: string, |
| 10 | + verified: boolean, |
| 11 | + user: User, |
| 12 | + client: Client, |
| 13 | + LOGGER: any |
| 14 | +): Promise<string> => { |
| 15 | + if (!walletAddress) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + // Get holdings |
| 20 | + let holdings = await getWalletHoldings(walletAddress, null); |
| 21 | + if (holdings === -1) { |
| 22 | + if (LOGGER !== null) { |
| 23 | + LOGGER.trackEvent({ |
| 24 | + name: 'linkWallet-no-trustline', |
| 25 | + properties: { walletAddress }, |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + return `Seems like you don't have the project trustline yet, please retry once it has been added 👉 https://xrpscan.com/account/${walletAddress}`; |
| 30 | + } |
| 31 | + |
| 32 | + // Allow them to set it even with network error |
| 33 | + let hadError = false; |
| 34 | + if (holdings === null) { |
| 35 | + hadError = true; |
| 36 | + holdings = 0; |
| 37 | + } |
| 38 | + |
| 39 | + const newWallet: IWallet = { |
| 40 | + address: walletAddress, |
| 41 | + points: holdings, |
| 42 | + verified, |
| 43 | + }; |
| 44 | + |
| 45 | + const newUser: IBotUser = { |
| 46 | + discordId: user.id, |
| 47 | + discordUsername: user.username, |
| 48 | + discordDiscriminator: user.discriminator, |
| 49 | + previousDiscordUsername: '', |
| 50 | + previousDiscordDiscriminator: '', |
| 51 | + totalPoints: holdings, |
| 52 | + wallets: [], |
| 53 | + }; |
| 54 | + |
| 55 | + // Save in Mongo |
| 56 | + const mongoUpdateResult = await updateUserWallet(newUser, newWallet, false); |
| 57 | + |
| 58 | + // The wallet has been claimed before, needs to be set by admin |
| 59 | + if (mongoUpdateResult === WalletUpdateResponse.ErrorAddressAlreadyClaimed) { |
| 60 | + if (LOGGER !== null) { |
| 61 | + LOGGER.trackEvent({ |
| 62 | + name: 'linkWallet-claimed-by-another-user', |
| 63 | + properties: { |
| 64 | + walletAddress, |
| 65 | + activeUserId: user.id, |
| 66 | + activeUserName: user.username, |
| 67 | + }, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + return `This address has been claimed before, if it wasn't done by you please message a mod with ownership proof to claim it`; |
| 72 | + } |
| 73 | + |
| 74 | + // If the user has set too many addresses an admin has to do it |
| 75 | + if (mongoUpdateResult === WalletUpdateResponse.ErrorTooManyAccountClaims) { |
| 76 | + if (LOGGER !== null) { |
| 77 | + LOGGER.trackEvent({ |
| 78 | + name: 'linkWallet-too-many-claimed', |
| 79 | + properties: { |
| 80 | + walletAddress, |
| 81 | + activeUserId: user.id, |
| 82 | + activeUserName: user.username, |
| 83 | + }, |
| 84 | + }); |
| 85 | + } |
| 86 | + return `You seem to have claimed too many addresses, please message a mod with ownership proof to claim more`; |
| 87 | + } |
| 88 | + |
| 89 | + // Set role |
| 90 | + await updateUserRoles(0, holdings, user.id, client, LOGGER, false); |
| 91 | + |
| 92 | + if (LOGGER !== null) { |
| 93 | + LOGGER.trackEvent({ |
| 94 | + name: 'linkWallet-success', |
| 95 | + properties: { |
| 96 | + walletAddress, |
| 97 | + activeUserId: user.id, |
| 98 | + activeUserName: user.username, |
| 99 | + }, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + // Send confirmation to the user |
| 104 | + if (hadError) { |
| 105 | + return `Wallet linked! There was an error trying to get your holdings from the XRPL network. Your role will be updated automatically once the network is working. You do not need to do anything else.`; |
| 106 | + } |
| 107 | + |
| 108 | + return `Found your ${truncate( |
| 109 | + holdings, |
| 110 | + 2 |
| 111 | + )} points! Updated server roles set 🚀`; |
| 112 | +}; |
| 113 | + |
| 114 | +export { linkWalletToDiscordAccount }; |
0 commit comments