|
| 1 | +import Command from "../../classes/Command"; |
| 2 | +import ExtendedClient from "../../classes/ExtendedClient"; |
| 3 | +import { AutocompleteInteraction, ChatInputCommandInteraction, ColorResolvable } from "discord.js"; |
| 4 | + |
| 5 | +import axios from "axios"; |
| 6 | +import { emojis as emoji } from "../../../config.json"; |
| 7 | + |
| 8 | +const command: Command = { |
| 9 | + name: "get-domain-json", |
| 10 | + description: "Get a subdomain's JSON file.", |
| 11 | + options: [ |
| 12 | + { |
| 13 | + type: 3, |
| 14 | + name: "subdomain", |
| 15 | + description: "The subdomain you want to get the JSON file for.", |
| 16 | + max_length: 253 - ".is-a.dev".length, |
| 17 | + required: true, |
| 18 | + autocomplete: true |
| 19 | + } |
| 20 | + ], |
| 21 | + botPermissions: [], |
| 22 | + requiredRoles: [], |
| 23 | + cooldown: 5, |
| 24 | + enabled: true, |
| 25 | + deferReply: true, |
| 26 | + ephemeral: false, |
| 27 | + async execute( |
| 28 | + interaction: ChatInputCommandInteraction, |
| 29 | + client: ExtendedClient, |
| 30 | + Discord: typeof import("discord.js") |
| 31 | + ) { |
| 32 | + try { |
| 33 | + const subdomain = interaction.options.get("subdomain").value as string; |
| 34 | + |
| 35 | + const hostnameRegex = |
| 36 | + /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/; |
| 37 | + |
| 38 | + if (!hostnameRegex.test(`${subdomain}.is-a.dev`)) { |
| 39 | + const invalidSubdomain = new Discord.EmbedBuilder() |
| 40 | + .setColor(client.config.embeds.error as ColorResolvable) |
| 41 | + .setDescription(`${emoji.cross} \`${subdomain}\` is not a valid subdomain.`); |
| 42 | + |
| 43 | + await interaction.editReply({ embeds: [invalidSubdomain] }); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const data = (await axios.get(`https://raw.githubusercontent.com/is-a-dev/register/main/domains/${subdomain}.json`)).data; |
| 48 | + |
| 49 | + if (data.internal || data.reserved) { |
| 50 | + const internalError = new Discord.EmbedBuilder() |
| 51 | + .setColor(client.config.embeds.error as ColorResolvable) |
| 52 | + .setDescription( |
| 53 | + `${emoji.cross} \`${subdomain}.is-a.dev\` is an internal or reserved subdomain and cannot be accessed.` |
| 54 | + ); |
| 55 | + |
| 56 | + await interaction.editReply({ embeds: [internalError] }); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!data) { |
| 61 | + const noResult = new Discord.EmbedBuilder() |
| 62 | + .setColor(client.config.embeds.error as ColorResolvable) |
| 63 | + .setDescription(`${emoji.cross} \`${subdomain}.is-a.dev\` does not exist.`); |
| 64 | + |
| 65 | + await interaction.editReply({ embeds: [noResult] }); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const result = new Discord.EmbedBuilder() |
| 70 | + .setColor(client.config.embeds.default as ColorResolvable) |
| 71 | + .setTitle(`${subdomain}.is-a.dev`) |
| 72 | + .setDescription(`\`\`\`json\n${JSON.stringify(data, null, 2)}\n\`\`\``) |
| 73 | + .setTimestamp(); |
| 74 | + |
| 75 | + await interaction.editReply({ embeds: [result] }); |
| 76 | + } catch (err) { |
| 77 | + client.logCommandError(err, interaction, Discord); |
| 78 | + } |
| 79 | + }, |
| 80 | + autocomplete: async (interaction: AutocompleteInteraction, client: ExtendedClient) => { |
| 81 | + const option = interaction.options.getFocused(true); |
| 82 | + |
| 83 | + if (option.name === "subdomain") { |
| 84 | + // Fetch all subdomains |
| 85 | + const res = (await axios.get("https://raw.is-a.dev/v2.json")).data; |
| 86 | + |
| 87 | + // Filter subdomains |
| 88 | + const filteredSubdomains = res.filter((entry: any) => entry.subdomain.startsWith(option.value) && !entry.internal && !entry.reserved); |
| 89 | + |
| 90 | + // Map subdomains to choices |
| 91 | + const choices = filteredSubdomains |
| 92 | + .map((entry: any) => { |
| 93 | + return { |
| 94 | + name: entry.subdomain, |
| 95 | + value: entry.subdomain |
| 96 | + }; |
| 97 | + }) |
| 98 | + .slice(0, 25); |
| 99 | + |
| 100 | + await interaction.respond(choices); |
| 101 | + } |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +export = command; |
0 commit comments