gwei-alert-bot/src/handlers.ts

51 lines
1.9 KiB
TypeScript

import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import redisClient from './redis';
import { getGasPricesInGwei } from './blockchain';
import { GasAlert } from '../types/gasAlert';
// Respond to the "/gas" command
const handleGasCommand = async (interaction: ChatInputCommandInteraction): Promise<void> => {
console.log(`Replying to command "/gas"`);
const gasPrices = await getGasPricesInGwei();
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Current Gas Prices')
.setDescription(`The current gas prices are: \n\n Fast: ${gasPrices.fast} Gwei \n\n Average: ${gasPrices.average} Gwei \n\n Slow: ${gasPrices.slow} Gwei`)
await interaction.reply({ embeds: [embed] });
};
// Respond to the "/gas alert ${gwei}" command
const handleGasAlertCommand = async (interaction: ChatInputCommandInteraction): Promise<void> => {
const threshold = parseInt(interaction.options.getString('gwei')!);
const channelId = interaction.channelId;
const userId = interaction.user.id;
const gasAlert: GasAlert = { threshold, channelId, userId };
console.log(`Replying to command "/gas gwei ${threshold}"`);
await redisClient.hSet('gas-alerts', `${userId}`, JSON.stringify(gasAlert));
await interaction.reply(`Your gas alert threshold of ${threshold} Gwei has been set.`);
};
// Respond to the "/gas pending" command
const handleGasPendingCommand = async (interaction: ChatInputCommandInteraction): Promise<void> => {
const userId = interaction.user.id;
const alertThreshold = await redisClient.get(`${userId}`);
console.log(`Replying to command "/gas pending"`);
if (alertThreshold === null) {
await interaction.reply('No gas price alerts set');
} else {
await interaction.reply(`Current gas price alert threshold: ${alertThreshold} Gwei`);
}
};
export { handleGasCommand, handleGasAlertCommand, handleGasPendingCommand };