Skip to content

Commit

Permalink
Reformat overall code with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
saboonikhil committed Feb 18, 2024
1 parent 6c5f338 commit d63bd69
Showing 1 changed file with 50 additions and 38 deletions.
88 changes: 50 additions & 38 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d63bd69

Please sign in to comment.