diff --git a/src/main.ts b/src/main.ts index 3f8b126..0e7a2d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,46 +31,58 @@ const checkAddress = (address): string | false => { console.log('KingOfCoins is online!'); }); - // Create an event listener for messages + // Event listener for messages client.on('message', async (message) => { - if (message.content.startsWith('!ser')) { - if (message.channel.id == config.discord.channel_id) { - console.log(`Received "${message.content.slice(5)}" from ${message.author.username}`); - - const address = message.content.split(' ')[1]; - const checkedAddress = checkAddress(address); - if (!checkedAddress) { - message.channel.send(`I don't understand this address, ${message.author.username}... 😕`); - return; - } - - const promocode = message.content.split(' ')[2]; - if (!activeCodes.includes(promocode)) { - message.channel.send(`Invalid promocode, ${message.author.username}... 😕`); - return; - } - - const entry = await db.getCode(promocode); - if (!entry) { - if (isProcessing) return; - else isProcessing = true; - - const success = await sender.sendTokens(checkedAddress, amount.toString()); - if (success) { - await db.saveOrUpdateCode(promocode, address); - message.channel.send( - `Sent ${amount / 10 ** 10} ZBS to ${message.author.username} against ${promocode}! 🎉` - ); - } else { - message.channel.send(`Sorry, something went wrong! Please try again...`); - } - isProcessing = false; - } else { - message.channel.send(`Promocode already used! 💼`); - } - return; - } + // Validate channel + if (message.channel.id !== config.discord.channel_id) return; + + // Validate message pattern + if (!message.content.startsWith('!ser')) { + message.delete(); + return; + } + console.log(`Received "${message.content.slice(5)}" from ${message.author.username}`); + + // Validate address + const address = message.content.split(' ')[1]; + const checkedAddress = checkAddress(address); + if (!checkedAddress) { + message.channel.send(`I don't understand this address, ${message.author.username}... 😕`); + return; + } + + // Verify promocode + const promocode = message.content.split(' ')[2]; + if (!activeCodes.includes(promocode)) { + message.channel.send(`Invalid promocode, ${message.author.username}... 😕`); + return; + } + + // Validate promocode + const entry = await db.getCode(promocode); + if (entry) { + message.channel.send(`Promocode already used! 💼`); + return; + } + + // Address is eligible to receive tokens + // Check if no pending txns are present on polkadot-api + if (isProcessing) return; + + isProcessing = true; // Lock polkadot-api + const success = await sender.sendTokens(checkedAddress, amount.toString()); + isProcessing = false; // Release polkadot-api + + // Validate response from polkadot-api + if (!success) { + message.channel.send(`Sorry, something went wrong! Please try again...`); + return; } + + // Add code redemption entry in db + await db.saveOrUpdateCode(promocode, address); + message.channel.send(`Sent ${amount / 10 ** 10} ZBS to ${message.author.username} against ${promocode}! 🎉`); + return; }); client.login(config.discord.token);