refactor: 🧑‍💻 refactor config cpgg

Refactored and improved UX for /config cpgg
This commit is contained in:
Axel Olausson Holtenäs 2022-12-20 09:20:30 +01:00
parent d1413e50ca
commit 915b78e5d4

View file

@ -1,24 +1,23 @@
import { import {
ChatInputCommandInteraction, ChatInputCommandInteraction,
EmbedBuilder,
PermissionsBitField, PermissionsBitField,
SlashCommandSubcommandBuilder, SlashCommandSubcommandBuilder,
} from "discord.js"; } from "discord.js";
import prisma from "../../../../handlers/database"; import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply"; import deferReply from "../../../../handlers/deferReply";
import { success as embedSuccess } from "../../../../helpers/baseEmbeds";
import checkPermission from "../../../../helpers/checkPermission"; import checkPermission from "../../../../helpers/checkPermission";
import encryption from "../../../../helpers/encryption"; import encryption from "../../../../helpers/encryption";
import getEmbedConfig from "../../../../helpers/getEmbedData";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
export const builder = (command: SlashCommandSubcommandBuilder) => { export const builder = (command: SlashCommandSubcommandBuilder) => {
return command return command
.setName("cpgg") .setName("cpgg")
.setDescription("Controlpanel.gg") .setDescription("Controlpanel.gg API")
.addStringOption((option) => .addStringOption((option) =>
option option
.setName("scheme") .setName("scheme")
.setDescription(`Controlpanel.gg Scheme`) .setDescription(`API protocol`)
.setRequired(true) .setRequired(true)
.setChoices( .setChoices(
{ name: "HTTPS (secure)", value: "https" }, { name: "HTTPS (secure)", value: "https" },
@ -26,16 +25,10 @@ export const builder = (command: SlashCommandSubcommandBuilder) => {
) )
) )
.addStringOption((option) => .addStringOption((option) =>
option option.setName("domain").setDescription(`API domain`).setRequired(true)
.setName("domain")
.setDescription(`Controlpanel.gg Domain`)
.setRequired(true)
) )
.addStringOption((option) => .addStringOption((option) =>
option option.setName("token").setDescription(`API Token`).setRequired(true)
.setName("token")
.setDescription(`Controlpanel.gg Application API`)
.setRequired(true)
); );
}; };
@ -44,22 +37,22 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
checkPermission(interaction, PermissionsBitField.Flags.ManageGuild); checkPermission(interaction, PermissionsBitField.Flags.ManageGuild);
const { successColor, footerText, footerIcon } = await getEmbedConfig(
interaction.guild
);
const { options, guild } = interaction; const { options, guild } = interaction;
if (!guild) throw new Error("Guild unavailable");
const tokenData = options.getString("token");
const scheme = options.getString("scheme"); const scheme = options.getString("scheme");
const domain = options.getString("domain"); const domain = options.getString("domain");
const token = tokenData && encryption.encrypt(tokenData); const tokenData = options.getString("token");
const url = scheme && domain && encryption.encrypt(`${scheme}://${domain}`); if (!scheme) throw new Error("Scheme must be set");
if (!domain) throw new Error("Domain must be set");
if (!tokenData) throw new Error("Token must be set");
if (!guild) throw new Error("No guild found"); const url = encryption.encrypt(`${scheme}://${domain}`);
if (!token) throw new Error("Token not found"); const token = encryption.encrypt(tokenData);
if (!url) throw new Error("URL not found"); if (!url) throw new Error("URL must be set");
if (!token) throw new Error("Token must be set");
const createGuild = await prisma.guildConfigApisCpgg.upsert({ const upsertGuildConfigApisCpgg = await prisma.guildConfigApisCpgg.upsert({
where: { where: {
id: guild.id, id: guild.id,
}, },
@ -78,28 +71,33 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
}, },
}); });
logger.silly(createGuild); logger.silly(upsertGuildConfigApisCpgg);
logger?.silly(`Updated API credentials.`); const successEmbed = await embedSuccess(
guild,
":gear:︱Configuration of CPGG"
);
const interactionEmbed = new EmbedBuilder() successEmbed.setDescription("Configuration updated successfully!").addFields(
.setTitle("[:tools:] CPGG") {
.setDescription( name: "Scheme",
`The following configuration will be used. value: `${scheme}`,
inline: true,
},
{
name: "Domain",
value: `${domain}`,
inline: true,
},
{
name: "Token",
value: `ends with ${tokenData.slice(-4)}`,
inline: true,
}
);
**Scheme**: ${scheme} await interaction.editReply({
**Domain**: ${domain} embeds: [successEmbed],
**Token**: ends with ${tokenData?.slice(-4)}`
)
.setColor(successColor)
.setTimestamp()
.setFooter({
iconURL: footerIcon,
text: footerText,
});
await interaction?.editReply({
embeds: [interactionEmbed],
}); });
return; return;
}; };