♻️ making changes to improve codeclimate
This commit is contained in:
parent
1dddcd0251
commit
4af0cf7250
6 changed files with 136 additions and 115 deletions
|
@ -5,6 +5,8 @@ import logger from "@logger";
|
||||||
|
|
||||||
import { errorColor, footerText, footerIcon } from "@config/embed";
|
import { errorColor, footerText, footerIcon } from "@config/embed";
|
||||||
import i18next from "i18next";
|
import i18next from "i18next";
|
||||||
|
import deferReply from "@root/helpers/deferReply";
|
||||||
|
import getCommandMeta from "@root/helpers/getCommandMeta";
|
||||||
|
|
||||||
export default async (interaction: CommandInteraction) => {
|
export default async (interaction: CommandInteraction) => {
|
||||||
if (!interaction.isCommand()) return;
|
if (!interaction.isCommand()) return;
|
||||||
|
@ -12,24 +14,14 @@ export default async (interaction: CommandInteraction) => {
|
||||||
const { client, guild, commandName, user, memberPermissions } = interaction;
|
const { client, guild, commandName, user, memberPermissions } = interaction;
|
||||||
|
|
||||||
const currentCommand = client.commands.get(commandName);
|
const currentCommand = client.commands.get(commandName);
|
||||||
if (!currentCommand) {
|
|
||||||
logger.verbose(`Command ${commandName} not found`);
|
if (currentCommand == null) {
|
||||||
|
logger.silly(`Command ${commandName} not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let meta;
|
const meta = await getCommandMeta(interaction, currentCommand);
|
||||||
|
|
||||||
const subcommand = interaction.options.getSubcommand()
|
await deferReply(interaction, meta.ephemeral || false);
|
||||||
|
|
||||||
if (!interaction.options.getSubcommandGroup(false)) {
|
|
||||||
meta = currentCommand.modules[subcommand].meta;
|
|
||||||
} else {
|
|
||||||
meta =
|
|
||||||
currentCommand.groups[interaction.options.getSubcommandGroup()].modules[
|
|
||||||
subcommand
|
|
||||||
].meta;
|
|
||||||
}
|
|
||||||
|
|
||||||
await interaction.deferReply({ ephemeral: meta?.ephemeral || false });
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
meta.permissions &&
|
meta.permissions &&
|
||||||
|
|
22
src/helpers/deferReply.ts
Normal file
22
src/helpers/deferReply.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import { CommandInteraction, MessageEmbed } from "discord.js";
|
||||||
|
import { waitColor, footerText, footerIcon } from "@config/embed";
|
||||||
|
|
||||||
|
export default async (interaction: CommandInteraction, ephemeral: boolean) => {
|
||||||
|
await interaction.deferReply({
|
||||||
|
ephemeral,
|
||||||
|
});
|
||||||
|
|
||||||
|
await interaction.editReply({
|
||||||
|
embeds: [
|
||||||
|
new MessageEmbed()
|
||||||
|
.setFooter({
|
||||||
|
text: footerText,
|
||||||
|
iconURL: footerIcon,
|
||||||
|
})
|
||||||
|
.setTimestamp(new Date())
|
||||||
|
.setTitle("Processing your request")
|
||||||
|
.setColor(waitColor)
|
||||||
|
.setDescription("Please wait..."),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
};
|
9
src/helpers/embedBuilder.ts
Normal file
9
src/helpers/embedBuilder.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import { footerText, footerIcon } from "@config/embed";
|
||||||
|
import { MessageEmbed } from "discord.js";
|
||||||
|
|
||||||
|
export default new MessageEmbed()
|
||||||
|
.setFooter({
|
||||||
|
text: footerText,
|
||||||
|
iconURL: footerIcon,
|
||||||
|
})
|
||||||
|
.setTimestamp(new Date());
|
12
src/helpers/getCommandMeta.ts
Normal file
12
src/helpers/getCommandMeta.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import { CommandInteraction } from "discord.js";
|
||||||
|
|
||||||
|
export default async (interaction: CommandInteraction, currentCommand: any) => {
|
||||||
|
const subcommand = interaction.options.getSubcommand();
|
||||||
|
const subcommandGroup = interaction.options.getSubcommandGroup(false);
|
||||||
|
|
||||||
|
if (!subcommandGroup) {
|
||||||
|
return currentCommand.modules[subcommand].meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentCommand.groups[subcommandGroup].modules[subcommand].meta;
|
||||||
|
};
|
|
@ -4,7 +4,6 @@ import { token, intents } from "@config/discord";
|
||||||
|
|
||||||
import { Client } from "discord.js"; // discord.js
|
import { Client } from "discord.js"; // discord.js
|
||||||
|
|
||||||
import locale from "@locale";
|
|
||||||
import database from "@database";
|
import database from "@database";
|
||||||
import schedules from "@schedules";
|
import schedules from "@schedules";
|
||||||
import events from "@handlers/events";
|
import events from "@handlers/events";
|
||||||
|
@ -15,7 +14,6 @@ async function main() {
|
||||||
intents,
|
intents,
|
||||||
});
|
});
|
||||||
|
|
||||||
await locale();
|
|
||||||
await database();
|
await database();
|
||||||
await schedules(client);
|
await schedules(client);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// Dependencies
|
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { CommandInteraction } from "discord.js";
|
import { CommandInteraction, MessageEmbed } from "discord.js";
|
||||||
|
|
||||||
// Configurations
|
|
||||||
import {
|
import {
|
||||||
successColor,
|
successColor,
|
||||||
errorColor,
|
errorColor,
|
||||||
|
@ -12,10 +10,9 @@ import {
|
||||||
|
|
||||||
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
||||||
|
|
||||||
// Handlers
|
|
||||||
import logger from "@logger";
|
import logger from "@logger";
|
||||||
|
import embedBuilder from "@root/helpers/embedBuilder";
|
||||||
|
|
||||||
// Function
|
|
||||||
export default {
|
export default {
|
||||||
meta: { guildOnly: false, ephemeral: false },
|
meta: { guildOnly: false, ephemeral: false },
|
||||||
|
|
||||||
|
@ -33,104 +30,95 @@ export default {
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
execute: async (interaction: CommandInteraction) => {
|
execute: async (interaction: CommandInteraction) => {
|
||||||
|
const embedTitle = "[:hammer:] Utility (Lookup)";
|
||||||
|
|
||||||
|
embedBuilder.setTitle(embedTitle);
|
||||||
|
|
||||||
const { options } = interaction;
|
const { options } = interaction;
|
||||||
// Get lookup query
|
const query = options.getString("query");
|
||||||
const query = options?.getString("query");
|
|
||||||
|
|
||||||
// Make API request
|
|
||||||
await axios
|
await axios
|
||||||
// Make a get request
|
.get(`http://ip-api.com/json/${query}`)
|
||||||
?.get(`http://ip-api.com/json/${query}`)
|
.then(async (response) => {
|
||||||
|
if (response.data.status !== "success") {
|
||||||
// If successful
|
await interaction.editReply({
|
||||||
?.then(async (res) => {
|
embeds: [
|
||||||
// If query failed
|
embedBuilder
|
||||||
if (res?.data?.status === "fail") {
|
.setColor(errorColor)
|
||||||
// Create embed object
|
.setDescription(
|
||||||
const embed = {
|
`${response?.data?.message}: ${response?.data?.query}`
|
||||||
title: ":hammer: Utilities - Lookup",
|
),
|
||||||
description: `${res?.data?.message}: ${res?.data?.query}`,
|
|
||||||
color: errorColor,
|
|
||||||
timestamp: new Date(),
|
|
||||||
footer: {
|
|
||||||
iconURL: footerIcon,
|
|
||||||
text: footerText,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send interaction reply
|
|
||||||
await interaction?.editReply({ embeds: [embed] });
|
|
||||||
}
|
|
||||||
|
|
||||||
// If query is successful
|
|
||||||
else if (res?.data?.status === "success") {
|
|
||||||
// Create embed object
|
|
||||||
const embed = {
|
|
||||||
title: ":hammer: Utilities - Lookup",
|
|
||||||
fields: [
|
|
||||||
{
|
|
||||||
name: "AS",
|
|
||||||
value: `${res?.data?.as || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Country",
|
|
||||||
value: `${res?.data?.country || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Country Code",
|
|
||||||
value: `${res?.data?.countryCode || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Region",
|
|
||||||
value: `${res?.data?.region || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Region Name",
|
|
||||||
value: `${res?.data?.regionName || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "City",
|
|
||||||
value: `${res?.data?.city || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "ZIP Code",
|
|
||||||
value: `${res?.data?.zip || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Latitude",
|
|
||||||
value: `${res?.data?.lat || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Longitude",
|
|
||||||
value: `${res?.data?.lon || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Timezone",
|
|
||||||
value: `${res?.data?.timezone || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "ISP",
|
|
||||||
value: `${res?.data?.isp || "Not available"}`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Organization",
|
|
||||||
value: `${res?.data?.org || "Not available"}`,
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
color: successColor,
|
});
|
||||||
timestamp: new Date(),
|
return;
|
||||||
footer: {
|
|
||||||
iconURL: footerIcon,
|
|
||||||
text: footerText,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Send interaction reply
|
|
||||||
await interaction?.editReply({ embeds: [embed] });
|
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(async (e) => {
|
await interaction.editReply({
|
||||||
logger?.error(e);
|
embeds: [
|
||||||
|
embedBuilder.setColor(successColor).setFields([
|
||||||
|
{
|
||||||
|
name: ":classical_building: AS",
|
||||||
|
value: `${response.data.as || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":classical_building: ISP",
|
||||||
|
value: `${response.data.isp || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":classical_building: Organization",
|
||||||
|
value: `${response.data.org || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":compass: Latitude",
|
||||||
|
value: `${response.data.lat || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":compass: Longitude",
|
||||||
|
value: `${response.data.lon || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":clock4: Timezone",
|
||||||
|
value: `${response.data.timezone || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: Country",
|
||||||
|
value: `${response.data.country || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: Region",
|
||||||
|
value: `${response.data.regionName || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: City",
|
||||||
|
value: `${response.data.city || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: Country Code",
|
||||||
|
value: `${response.data.countryCode || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: Region Code",
|
||||||
|
value: `${response.data.region || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: ":globe_with_meridians: ZIP",
|
||||||
|
value: `${response.data.zip || "Unknown"}`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue