refactor: 🧑‍💻 refactored interactionCreate

Improved handling of interactions, using correct types
This commit is contained in:
Axel Olausson Holtenäs 2022-12-21 07:38:54 +01:00
parent f9c6ad8836
commit e8bfd0cc15
5 changed files with 97 additions and 85 deletions

View file

@ -1,14 +1,41 @@
// Dependencies
import { BaseInteraction } from "discord.js";
export default async (interaction: BaseInteraction) => {
if (!interaction.isButton()) return;
import {
ActionRowBuilder,
ButtonBuilder,
ButtonInteraction,
ButtonStyle,
EmbedBuilder,
} from "discord.js";
import getEmbedData from "../../../../helpers/getEmbedData";
export default async (interaction: ButtonInteraction) => {
const { errorColor, footerText, footerIcon } = await getEmbedData(
interaction.guild
);
const { customId } = interaction;
const currentButton = await import(`../../../buttons/${customId}`);
if (!currentButton) throw new Error(`Unknown button ${customId}`);
await currentButton.execute(interaction);
await currentButton.execute(interaction).catch((error: Error) => {
const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel("Report Problem")
.setStyle(ButtonStyle.Link)
.setEmoji("✏️")
.setURL("https://discord.zyner.org")
);
return interaction.editReply({
embeds: [
new EmbedBuilder()
.setTitle(`:no_entry_sign:︱Your request failed`)
.setDescription(`${error.message}`)
.setColor(errorColor)
.setTimestamp(new Date())
.setFooter({ text: footerText, iconURL: footerIcon }),
],
components: [buttons],
});
});
};

View file

@ -0,0 +1,42 @@
import {
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ChatInputCommandInteraction,
EmbedBuilder,
} from "discord.js";
import getEmbedData from "../../../../helpers/getEmbedData";
export default async (interaction: ChatInputCommandInteraction) => {
const { errorColor, footerText, footerIcon } = await getEmbedData(
interaction.guild
);
if (!interaction.isCommand()) return;
const { client, commandName } = interaction;
const currentCommand = client.commands.get(commandName);
if (!currentCommand) throw new Error(`Unknown command ${commandName}`);
await currentCommand.execute(interaction).catch((error: Error) => {
const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel("Report Problem")
.setStyle(ButtonStyle.Link)
.setEmoji("✏️")
.setURL("https://discord.zyner.org")
);
return interaction.editReply({
embeds: [
new EmbedBuilder()
.setTitle(`:no_entry_sign:︱Your request failed`)
.setDescription(`${error.message}`)
.setColor(errorColor)
.setTimestamp(new Date())
.setFooter({ text: footerText, iconURL: footerIcon }),
],
components: [buttons],
});
});
};

View file

@ -1,12 +0,0 @@
// Dependencies
import { ChatInputCommandInteraction } from "discord.js";
export default async (interaction: ChatInputCommandInteraction) => {
if (!interaction.isCommand()) return;
const { client, commandName } = interaction;
const currentCommand = client.commands.get(commandName);
if (!currentCommand) throw new Error(`Unknown command ${commandName}`);
await currentCommand.execute(interaction);
};

View file

@ -1,50 +0,0 @@
import {
ActionRowBuilder,
BaseInteraction,
ButtonBuilder,
ButtonInteraction,
ButtonStyle,
ChatInputCommandInteraction,
CommandInteraction,
EmbedBuilder,
} from "discord.js";
import getEmbedConfig from "../../../helpers/getEmbedData";
import button from "./button";
import command from "./command";
// Send interactions to all available handlers
export const execute = async (interaction: BaseInteraction) => {
await button(<ButtonInteraction>interaction);
await command(<ChatInputCommandInteraction>interaction);
};
// Handle interactions from commands
export const handleCommandInteraction = async (
interaction: CommandInteraction
) => {
const { errorColor, footerText, footerIcon } = await getEmbedConfig(
interaction.guild
);
await command(<ChatInputCommandInteraction>interaction).catch((err) => {
const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setLabel("Report Problem")
.setStyle(ButtonStyle.Link)
.setEmoji("📝")
.setURL("https://discord.zyner.org")
);
return interaction.editReply({
embeds: [
new EmbedBuilder()
.setTitle(`:no_entry_sign:︱Your request failed`)
.setDescription(`${err.message}`)
.setColor(errorColor)
.setTimestamp(new Date())
.setFooter({ text: footerText, iconURL: footerIcon }),
],
components: [buttons],
});
});
};

View file

@ -1,34 +1,39 @@
// 3rd party dependencies
import {
BaseInteraction,
CommandInteraction,
InteractionType,
} from "discord.js";
import { BaseInteraction, InteractionType } from "discord.js";
import upsertGuildMember from "../../helpers/upsertGuildMember";
import { IEventOptions } from "../../interfaces/EventOptions";
import logger from "../../middlewares/logger";
// Dependencies
import audits from "./audits";
import { handleCommandInteraction as HandlersHandleCommandInteraction } from "./handlers";
import button from "./handlers/button";
import chatInputCommand from "./handlers/chatInputCommand";
export const options: IEventOptions = {
type: "on",
};
// Execute the event
export const execute = async (interaction: BaseInteraction) => {
const { guild, id } = interaction;
logger.silly({ interaction });
const { guild, user } = interaction;
logger?.silly(
`New interaction: ${id} in guild: ${guild?.name} (${guild?.id})`
);
if (guild) {
await upsertGuildMember(guild, user);
}
switch (interaction.type) {
case InteractionType.ApplicationCommand:
await HandlersHandleCommandInteraction(<CommandInteraction>interaction);
break;
case InteractionType.ApplicationCommand: {
if (interaction.isChatInputCommand()) {
await chatInputCommand(interaction);
return;
}
if (interaction.isButton()) {
await button(interaction);
return;
}
break;
}
default:
logger?.error(`Unknown interaction type: ${interaction.type}`);
throw new Error("Unknown interaction type");
}
await audits.execute(interaction);