gwei-alert-bot/src/gasAlertChecker.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-04-18 05:51:24 +00:00
import { EmbedBuilder, TextChannel } from 'discord.js';
2023-04-18 20:35:09 +00:00
2023-04-18 05:51:24 +00:00
import { DiscordClient } from './discordClient';
2023-04-18 20:35:09 +00:00
import redisClient from './redis';
2023-04-18 01:49:20 +00:00
import { GasAlert } from '../types/gasAlert';
import { GasPrices } from '../types/gasPrices';
2023-04-18 01:49:20 +00:00
const createGasAlertChecker = (client: DiscordClient) =>
async (gasPrices: GasPrices) => {
2023-04-18 01:49:20 +00:00
try {
const gasAlerts: GasAlert[] = await redisClient
2023-04-18 20:35:09 +00:00
.hVals('gas-alerts')
.then((values) => values.map(val => JSON.parse(val)));
2023-04-18 01:49:20 +00:00
gasAlerts.forEach(async (gasAlert) => {
if (gasPrices.fast <= gasAlert.threshold) {
2023-04-18 01:49:20 +00:00
const channel = await client.channels.fetch(gasAlert.channelId) as TextChannel;
const user = await client.users.fetch(gasAlert.userId);
channel.send({
embeds: [
new EmbedBuilder()
.setTitle('Gas price alert!')
2023-04-18 20:35:09 +00:00
.setDescription(`<@${gasAlert.userId}>!
Gas prices have fallen below your alert threshold of ${gasAlert.threshold} Gwei:
2023-04-22 00:18:31 +00:00
${gasPrices.fast} 🚶 ${gasPrices.average} 🐢 ${gasPrices.slow}`)
2023-04-18 01:49:20 +00:00
.setColor('#FF8C00')
],
target: user
})
2023-04-18 20:35:09 +00:00
console.log(`Alerted ${user} under threshold ${gasAlert.threshold}: Removing alert...`)
await redisClient.hDel('gas-alerts', `${gasAlert.userId}`)
2023-04-18 01:49:20 +00:00
}
});
} catch (error) {
2023-04-18 20:35:09 +00:00
console.log(`Error checking gas prices:\n`, error);
2023-04-18 01:49:20 +00:00
}
};
2023-04-18 01:49:20 +00:00
export { createGasAlertChecker };