-
-
Notifications
You must be signed in to change notification settings - Fork 3
Generation command & refactors #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AbdullahCXD
wants to merge
2
commits into
is-a-dev:main
Choose a base branch
from
AbdullahCXD:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
import Command from "../../classes/Command"; | ||
import ExtendedClient from "../../classes/ExtendedClient"; | ||
import { | ||
ActionRowBuilder, | ||
ButtonBuilder, | ||
ButtonInteraction, | ||
ButtonStyle, | ||
ChatInputCommandInteraction, | ||
ColorResolvable, | ||
ModalBuilder, | ||
TextInputBuilder, | ||
TextInputStyle, | ||
} from "discord.js"; | ||
|
||
import { emojis as emoji } from "../../../config.json"; | ||
import ms from "ms"; | ||
import { fetchDomains, filterObject } from "../../util/functions"; | ||
|
||
const command: Command = { | ||
name: "generate-domain-json", | ||
description: "Generate a subdomain's JSON file.", | ||
options: [ | ||
{ | ||
type: 3, | ||
name: "subdomain", | ||
description: "The subdomain you want to generate the JSON for.", | ||
max_length: 253 - ".is-a.dev".length, | ||
required: true, | ||
}, | ||
], | ||
botPermissions: [], | ||
requiredRoles: [], | ||
cooldown: 5, | ||
enabled: true, | ||
deferReply: true, | ||
ephemeral: false, | ||
async execute( | ||
interaction: ChatInputCommandInteraction, | ||
client: ExtendedClient, | ||
Discord: typeof import("discord.js") | ||
) { | ||
try { | ||
const subdomain = interaction.options.get("subdomain").value as string; | ||
|
||
const hostnameRegex = | ||
/^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/; | ||
|
||
if (!hostnameRegex.test(`${subdomain}.is-a.dev`)) { | ||
const invalidSubdomain = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.error as ColorResolvable) | ||
.setDescription( | ||
`${emoji.cross} \`${subdomain}\` is not a valid subdomain.` | ||
); | ||
|
||
await interaction.editReply({ embeds: [invalidSubdomain] }); | ||
return; | ||
} | ||
|
||
let domains = await fetchDomains(); | ||
const data = domains.find((v: any) => v.domain === `${subdomain}.is-a.dev`); | ||
|
||
if (data) { | ||
const noResult = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.error as ColorResolvable) | ||
.setDescription( | ||
`${emoji.cross} \`${subdomain}.is-a.dev\` exists already.` | ||
); | ||
|
||
await interaction.editReply({ embeds: [noResult] }); | ||
return; | ||
} | ||
|
||
if (data && (data.internal || data.reserved)) { | ||
const internalError = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.error as ColorResolvable) | ||
.setDescription( | ||
`${emoji.cross} \`${subdomain}.is-a.dev\` is an internal or reserved subdomain and cannot be accessed.` | ||
); | ||
|
||
await interaction.editReply({ embeds: [internalError] }); | ||
return; | ||
} | ||
|
||
const embed = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.default as ColorResolvable) | ||
.setTitle("Domain JSON Generation") | ||
.setDescription( | ||
`Generate your .is-a.dev domain JSON by hitting the button bellow.` | ||
); | ||
|
||
const endEmbed = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.error as ColorResolvable) | ||
.setDescription(`${emoji.cross} Ended Generation`); | ||
|
||
const actions = new ActionRowBuilder<ButtonBuilder>().setComponents( | ||
new ButtonBuilder() | ||
.setCustomId("start-generation") | ||
.setStyle(ButtonStyle.Primary) | ||
.setLabel("Generate domain JSON") | ||
); | ||
|
||
const records = [ | ||
new TextInputBuilder() | ||
.setCustomId("cname-record") | ||
.setLabel("CNAME Record") | ||
.setStyle(TextInputStyle.Short) | ||
.setPlaceholder("Place your CNAME Record here") | ||
.setRequired(false), | ||
new TextInputBuilder() | ||
.setCustomId("a-records") | ||
.setLabel("A Records") | ||
.setStyle(TextInputStyle.Paragraph) | ||
.setPlaceholder("A Records are split into an array by commas") | ||
.setRequired(false), | ||
]; | ||
|
||
const records2 = [ | ||
new TextInputBuilder() | ||
.setCustomId("txt-record") | ||
.setLabel("TXT Record") | ||
.setStyle(TextInputStyle.Short) | ||
.setPlaceholder("Place your TXT Record here") | ||
.setRequired(false), | ||
new TextInputBuilder() | ||
.setCustomId("mx-record") | ||
.setLabel("MX Records") | ||
.setStyle(TextInputStyle.Paragraph) | ||
.setPlaceholder("MX Records are split into an array by commas") | ||
.setRequired(false), | ||
]; | ||
|
||
const userInfo = [ | ||
new TextInputBuilder() | ||
.setCustomId("username") | ||
.setLabel("Github Username") | ||
.setStyle(TextInputStyle.Short) | ||
.setPlaceholder("Place your github username here") | ||
.setRequired(true), | ||
]; | ||
|
||
// Each TextInput needs its own ActionRow, or discord will cause an error | ||
const modalActionRows = [ | ||
new ActionRowBuilder<TextInputBuilder>().addComponents(userInfo[0]), | ||
new ActionRowBuilder<TextInputBuilder>().addComponents(records[0]), | ||
new ActionRowBuilder<TextInputBuilder>().addComponents(records[1]), | ||
new ActionRowBuilder<TextInputBuilder>().addComponents(records2[0]), | ||
new ActionRowBuilder<TextInputBuilder>().addComponents(records2[1]), | ||
]; | ||
|
||
const modal = new ModalBuilder() | ||
.setCustomId("generation-modal") | ||
.setTitle(".is-a.dev JSON Domain") | ||
.addComponents(...modalActionRows); | ||
|
||
const sentMessage = await interaction.editReply({ | ||
embeds: [embed], | ||
components: [actions], | ||
}); | ||
|
||
const componentCollector = sentMessage.createMessageComponentCollector({ | ||
time: ms("10m"), | ||
}); | ||
|
||
componentCollector.on( | ||
"collect", | ||
async (v: ButtonInteraction<"cached">) => { | ||
if (v.user.id !== interaction.user.id) return; // Disallow any other users from disrupting the generation | ||
if (v.customId === "start-generation") { | ||
await v.showModal(modal); | ||
|
||
const modalResponse = await v.awaitModalSubmit({ time: ms("10m") }); | ||
await modalResponse.reply({ | ||
content: "Generated", | ||
flags: Discord.MessageFlags.Ephemeral, | ||
}); | ||
const aRecords = | ||
modalResponse.fields.getTextInputValue("a-records"); | ||
|
||
const mxRecords = modalResponse.fields.getTextInputValue("mx-record"); | ||
|
||
const { A, cname, txt, mx, github, discord } = { | ||
cname: modalResponse.fields.getTextInputValue("cname-record"), | ||
A: aRecords.length ? aRecords.split(",") : [], | ||
txt: modalResponse.fields.getTextInputValue("txt-record"), | ||
mx: mxRecords.length ? aRecords.split(",") : [], | ||
|
||
// User Data | ||
github: modalResponse.fields.getTextInputValue("username"), | ||
discord: interaction.user.username, | ||
}; | ||
|
||
const data = filterObject( | ||
{ | ||
owner: { | ||
username: github, | ||
discord, | ||
}, | ||
records: { | ||
CNAME: cname.length ? cname : null, | ||
A: A.length ? A : null, | ||
TXT: txt.length ? txt : null, | ||
MX: mx.length ? mx : null, | ||
}, | ||
}, | ||
(_, v) => { | ||
return v != null; | ||
}, | ||
true | ||
); | ||
|
||
const resultEmbed = new Discord.EmbedBuilder() | ||
.setColor(client.config.embeds.default as ColorResolvable) | ||
.setDescription( | ||
`\`\`\`json\n${JSON.stringify(data, null, 2)}\n\`\`\`` | ||
); | ||
|
||
await interaction.editReply({ | ||
embeds: [resultEmbed], | ||
components: [], | ||
}); | ||
componentCollector.stop("generated"); | ||
} | ||
} | ||
); | ||
|
||
componentCollector.on("end", async (collected, reason) => { | ||
if (reason === "unfinished") { | ||
await interaction.editReply({ | ||
embeds: [endEmbed], | ||
components: [], // Remove all the components, aka the action row | ||
}); | ||
} | ||
}); | ||
} catch (err) { | ||
client.logCommandError(err, interaction, Discord); | ||
} | ||
}, | ||
}; | ||
|
||
export = command; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.