|
| 1 | +import { SlashCommandBuilder, ChatInputCommandInteraction } from "discord.js"; |
| 2 | +import { ValidatorService } from "../services/validator-service"; |
| 3 | +import { ChainInfoService } from "../services/chaininfo-service"; |
| 4 | + |
| 5 | +export default { |
| 6 | + data: new SlashCommandBuilder() |
| 7 | + .setName("validator") |
| 8 | + .setDescription("Manage validator addresses") |
| 9 | + .addSubcommand((subcommand) => |
| 10 | + subcommand |
| 11 | + .setName("add") |
| 12 | + .setDescription("Add yourself to the validator set") |
| 13 | + .addStringOption((option) => |
| 14 | + option |
| 15 | + .setName("address") |
| 16 | + .setDescription("Your validator address") |
| 17 | + ) |
| 18 | + ) |
| 19 | + .addSubcommand((subcommand) => |
| 20 | + subcommand |
| 21 | + .setName("check") |
| 22 | + .setDescription("Check if you are a validator") |
| 23 | + .addStringOption((option) => |
| 24 | + option |
| 25 | + .setName("address") |
| 26 | + .setDescription("The validator address to check") |
| 27 | + ) |
| 28 | + ), |
| 29 | + |
| 30 | + execute: async (interaction: ChatInputCommandInteraction) => { |
| 31 | + const address = interaction.options.getString("address"); |
| 32 | + if (!address) { |
| 33 | + return interaction.reply({ |
| 34 | + content: "Address is required.", |
| 35 | + ephemeral: true, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + // Basic address validation |
| 40 | + if (!address.match(/^0x[a-fA-F0-9]{40}$/)) { |
| 41 | + return interaction.reply({ |
| 42 | + content: "Please provide a valid Ethereum address.", |
| 43 | + ephemeral: true, |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + await interaction.deferReply(); |
| 48 | + |
| 49 | + if (interaction.options.getSubcommand() === "add") { |
| 50 | + try { |
| 51 | + await ValidatorService.addValidator(address); |
| 52 | + |
| 53 | + await interaction.editReply({ |
| 54 | + content: `Successfully added validator address: ${address}`, |
| 55 | + }); |
| 56 | + } catch (error) { |
| 57 | + await interaction.editReply({ |
| 58 | + content: `Failed to add validator address: ${ |
| 59 | + error instanceof Error ? error.message : String(error) |
| 60 | + }`, |
| 61 | + }); |
| 62 | + } |
| 63 | + } else if (interaction.options.getSubcommand() === "check") { |
| 64 | + try { |
| 65 | + const info = await ChainInfoService.getInfo(); |
| 66 | + const { validators, committee } = info; |
| 67 | + |
| 68 | + let reply = ""; |
| 69 | + if (validators.includes(address)) { |
| 70 | + reply += "You are a validator\n"; |
| 71 | + } |
| 72 | + if (committee.includes(address)) { |
| 73 | + reply += "You are a committee member\n"; |
| 74 | + } |
| 75 | + |
| 76 | + await interaction.editReply({ |
| 77 | + content: reply, |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + await interaction.editReply({ |
| 81 | + content: `Failed to check validator address: ${ |
| 82 | + error instanceof Error ? error.message : String(error) |
| 83 | + }`, |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | +}; |
0 commit comments