Skip to content

Commit 7ae12e8

Browse files
committed
fix
1 parent 16ef9dc commit 7ae12e8

File tree

5 files changed

+82
-33
lines changed

5 files changed

+82
-33
lines changed

tooling/sparta/src/clients/discord.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import nodeOperatorCommands from "../roles/nodeOperators/index.js";
1818
import adminsCommands from "../roles/admins/index.js";
1919
import { logger } from "../utils/logger.js";
20+
import { subscribe } from "diagnostics_channel";
2021

2122
// Extended Discord client interface with commands collection
2223
export interface ExtendedClient extends Client {
@@ -95,25 +96,33 @@ export class Discord {
9596
const command = client.commands.get(interaction.commandName);
9697
if (!command) return;
9798

99+
logger.debug(
100+
{
101+
command: interaction.commandName,
102+
subcommand: interaction.options.getSubcommand(),
103+
},
104+
"Command"
105+
);
98106
try {
99107
const channel = interaction.channel as TextChannel;
100108

101-
const reply = await command.execute(interaction);
102-
logger.info(
109+
logger.debug(
103110
{
104-
name: interaction.commandName,
105111
channel: channel.name,
106112
user: interaction.user.username,
107113
date: interaction.createdAt,
108-
result: reply,
109114
},
110-
"Command executed"
115+
"Command info"
111116
);
112-
} catch (error) {
113-
logger.error(
114-
{ error, command: interaction.commandName },
115-
"Error executing command"
117+
const reply = await command.execute(interaction);
118+
logger.debug(
119+
{
120+
reply,
121+
},
122+
"Command reply"
116123
);
124+
} catch (error) {
125+
logger.error({ error }, "Error executing command");
117126
await interaction.reply({
118127
content: "There was an error executing this command!",
119128
flags: MessageFlags.Ephemeral,

tooling/sparta/src/clients/google.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class GoogleSheet {
107107
row: number,
108108
rowData: any[]
109109
) => void,
110-
intervalMs: number = 1000
110+
intervalMs: number = 10000
111111
): void {
112112
logger.info(
113113
{ columnIndexes, sheetRange, intervalMs },
@@ -192,7 +192,6 @@ export class GoogleSheet {
192192
range: string
193193
): Promise<any[][]> {
194194
try {
195-
logger.debug({ spreadsheetId, range }, "Fetching sheet data");
196195
const response = await this.sheets.spreadsheets.values.get({
197196
spreadsheetId,
198197
range,

tooling/sparta/src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "module",
55
"scripts": {
66
"build": "FOUNDRY_DISABLE_NIGHTLY_WARNING=true bun build index.ts --target bun --minify --outdir=dist",
7-
"dev": "FOUNDRY_DISABLE_NIGHTLY_WARNING=true bun run --watch index.ts",
7+
"dev": "LOG_LEVEL=debug FOUNDRY_DISABLE_NIGHTLY_WARNING=true bun run --watch index.ts",
88
"start": "FOUNDRY_DISABLE_NIGHTLY_WARNING=true bun run index.ts",
99
"watch": "FOUNDRY_DISABLE_NIGHTLY_WARNING=true tsc -w",
1010
"lint": "eslint . --ext .ts",
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,63 @@
1-
import { ChatInputCommandInteraction } from "discord.js";
1+
import {
2+
ChatInputCommandInteraction,
3+
DiscordAPIError,
4+
MessageFlags,
5+
TextChannel,
6+
} from "discord.js";
27
import { ChainInfoService } from "../../../services/chaininfo-service.js";
3-
48
export const get = async (
59
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+
);
935

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+
}
1258

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}`,
1561
});
62+
return `Pending block: ${pendingBlockNum}\nProven block: ${provenBlockNum}\nCurrent epoch: ${currentEpoch}\nCurrent slot: ${currentSlot}\nProposer now: ${proposerNow}`;
1663
};

tooling/sparta/src/roles/nodeOperators/chainInfo/index.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,25 @@ export default {
1818
.setName(NodeOperatorSubcommands.ChainInfo)
1919
.setDescription("Get chain information")
2020
),
21-
execute: async (
22-
interaction: ChatInputCommandInteraction
23-
): Promise<string> => {
21+
execute: async (interaction: ChatInputCommandInteraction) => {
2422
try {
25-
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
26-
2723
const subcommand = interaction.options.getSubcommand();
2824
switch (subcommand) {
2925
case NodeOperatorSubcommands.ChainInfo:
3026
await get(interaction);
31-
return `Retrieved chain info`;
27+
break;
3228
default:
3329
await interaction.editReply({
34-
content: `Unknown subcommand: ${subcommand}`,
30+
content: `Invalid subcommand: ${subcommand}`,
3531
});
36-
return `Unknown subcommand`;
32+
return;
3733
}
3834
} catch (error) {
3935
await interaction.editReply({
40-
content: `Failed with error: ${
41-
error instanceof Error ? error.message : "Unknown error"
42-
}`,
36+
content: `Failed with error: ${error}`,
4337
});
4438
return `Failed with error: ${
45-
error instanceof Error ? error.message : "Unknown error"
39+
error instanceof Error && error.message
4640
}`;
4741
}
4842
},

0 commit comments

Comments
 (0)