♻️ making changes to improve codeclimate

This commit is contained in:
Axel Olausson Holtenäs 2022-04-30 16:55:58 +02:00
parent 1dddcd0251
commit 4af0cf7250
No known key found for this signature in database
GPG key ID: 7BF6826B76382CBA
6 changed files with 136 additions and 115 deletions

View file

@ -5,6 +5,8 @@ import logger from "@logger";
import { errorColor, footerText, footerIcon } from "@config/embed";
import i18next from "i18next";
import deferReply from "@root/helpers/deferReply";
import getCommandMeta from "@root/helpers/getCommandMeta";
export default async (interaction: CommandInteraction) => {
if (!interaction.isCommand()) return;
@ -12,24 +14,14 @@ export default async (interaction: CommandInteraction) => {
const { client, guild, commandName, user, memberPermissions } = interaction;
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()
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 });
await deferReply(interaction, meta.ephemeral || false);
if (
meta.permissions &&

22
src/helpers/deferReply.ts Normal file
View 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..."),
],
});
};

View 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());

View 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;
};

View file

@ -4,7 +4,6 @@ import { token, intents } from "@config/discord";
import { Client } from "discord.js"; // discord.js
import locale from "@locale";
import database from "@database";
import schedules from "@schedules";
import events from "@handlers/events";
@ -15,7 +14,6 @@ async function main() {
intents,
});
await locale();
await database();
await schedules(client);

View file

@ -1,8 +1,6 @@
// Dependencies
import axios from "axios";
import { CommandInteraction } from "discord.js";
import { CommandInteraction, MessageEmbed } from "discord.js";
// Configurations
import {
successColor,
errorColor,
@ -12,10 +10,9 @@ import {
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
// Handlers
import logger from "@logger";
import embedBuilder from "@root/helpers/embedBuilder";
// Function
export default {
meta: { guildOnly: false, ephemeral: false },
@ -33,104 +30,95 @@ export default {
);
},
execute: async (interaction: CommandInteraction) => {
const embedTitle = "[:hammer:] Utility (Lookup)";
embedBuilder.setTitle(embedTitle);
const { options } = interaction;
// Get lookup query
const query = options?.getString("query");
const query = options.getString("query");
// Make API request
await axios
// Make a get request
?.get(`http://ip-api.com/json/${query}`)
// If successful
?.then(async (res) => {
// If query failed
if (res?.data?.status === "fail") {
// Create embed object
const embed = {
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"}`,
},
.get(`http://ip-api.com/json/${query}`)
.then(async (response) => {
if (response.data.status !== "success") {
await interaction.editReply({
embeds: [
embedBuilder
.setColor(errorColor)
.setDescription(
`${response?.data?.message}: ${response?.data?.query}`
),
],
color: successColor,
timestamp: new Date(),
footer: {
iconURL: footerIcon,
text: footerText,
},
};
// Send interaction reply
await interaction?.editReply({ embeds: [embed] });
});
return;
}
})
.catch(async (e) => {
logger?.error(e);
await interaction.editReply({
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,
},
]),
],
});
});
},
};