|
1 |
| -import { ChatInputCommandInteraction } from "discord.js"; |
| 1 | +import { |
| 2 | + ChatInputCommandInteraction, |
| 3 | + DiscordAPIError, |
| 4 | + MessageFlags, |
| 5 | + TextChannel, |
| 6 | +} from "discord.js"; |
2 | 7 | import { ChainInfoService } from "../../../services/chaininfo-service.js";
|
3 |
| - |
4 | 8 | export const get = async (
|
5 | 9 | interaction: ChatInputCommandInteraction
|
6 |
| -): Promise<void> => { |
7 |
| - const info = await ChainInfoService.getInfo(); |
8 |
| - const { validators, committee } = info; |
| 10 | +): Promise<string> => { |
| 11 | + const { |
| 12 | + pendingBlockNum, |
| 13 | + provenBlockNum, |
| 14 | + currentEpoch, |
| 15 | + currentSlot, |
| 16 | + proposerNow, |
| 17 | + } = await ChainInfoService.getInfo(); |
| 18 | + const channel = interaction.channel as TextChannel; |
| 19 | + const messages = await channel.messages.fetch({ |
| 20 | + limit: 15, |
| 21 | + }); |
| 22 | + |
| 23 | + // Filter for bot messages that look like chain info responses |
| 24 | + // and aren't the current interaction's response |
| 25 | + const botMessages = messages.filter( |
| 26 | + (m) => |
| 27 | + m.author.id === interaction.client.user?.id && |
| 28 | + m.content.includes("Pending block:") && |
| 29 | + m.content.includes("Proven block:") && |
| 30 | + m.content.includes("Current epoch:") && |
| 31 | + !m.flags.has(MessageFlags.Ephemeral) && |
| 32 | + // Ensure we don't delete the message we're about to send |
| 33 | + m.id !== interaction.id |
| 34 | + ); |
9 | 35 |
|
10 |
| - let reply = `Current validators: ${validators.join(", ")}\n`; |
11 |
| - reply += `Current committee: ${committee.join(", ")}`; |
| 36 | + if (botMessages.size > 0) { |
| 37 | + try { |
| 38 | + // Try bulk delete first (only works for messages less than 14 days old) |
| 39 | + await channel.bulkDelete(botMessages); |
| 40 | + } catch (error) { |
| 41 | + // If bulk delete fails (e.g., messages are too old), delete individually |
| 42 | + if (error instanceof DiscordAPIError && error.code === 50034) { |
| 43 | + for (const message of botMessages.values()) { |
| 44 | + try { |
| 45 | + await message.delete(); |
| 46 | + } catch (deleteError) { |
| 47 | + console.error( |
| 48 | + "Error deleting individual message:", |
| 49 | + deleteError |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + } else { |
| 54 | + throw error; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
12 | 58 |
|
13 |
| - await interaction.editReply({ |
14 |
| - content: reply, |
| 59 | + await interaction.reply({ |
| 60 | + content: `Pending block: ${pendingBlockNum}\nProven block: ${provenBlockNum}\nCurrent epoch: ${currentEpoch}\nCurrent slot: ${currentSlot}\nProposer now: ${proposerNow}`, |
15 | 61 | });
|
| 62 | + return `Pending block: ${pendingBlockNum}\nProven block: ${provenBlockNum}\nCurrent epoch: ${currentEpoch}\nCurrent slot: ${currentSlot}\nProposer now: ${proposerNow}`; |
16 | 63 | };
|
0 commit comments