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 3b0bde3cc5
commit b5a6385e18
5 changed files with 97 additions and 85 deletions

View file

@ -1,14 +1,41 @@
// Dependencies import {
import { BaseInteraction } from "discord.js"; ActionRowBuilder,
ButtonBuilder,
export default async (interaction: BaseInteraction) => { ButtonInteraction,
if (!interaction.isButton()) return; 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 { customId } = interaction;
const currentButton = await import(`../../../buttons/${customId}`); const currentButton = await import(`../../../buttons/${customId}`);
if (!currentButton) throw new Error(`Unknown button ${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, InteractionType } from "discord.js";
import { import upsertGuildMember from "../../helpers/upsertGuildMember";
BaseInteraction,
CommandInteraction,
InteractionType,
} from "discord.js";
import { IEventOptions } from "../../interfaces/EventOptions"; import { IEventOptions } from "../../interfaces/EventOptions";
import logger from "../../middlewares/logger"; import logger from "../../middlewares/logger";
// Dependencies
import audits from "./audits"; import audits from "./audits";
import { handleCommandInteraction as HandlersHandleCommandInteraction } from "./handlers"; import button from "./handlers/button";
import chatInputCommand from "./handlers/chatInputCommand";
export const options: IEventOptions = { export const options: IEventOptions = {
type: "on", type: "on",
}; };
// Execute the event
export const execute = async (interaction: BaseInteraction) => { export const execute = async (interaction: BaseInteraction) => {
const { guild, id } = interaction; logger.silly({ interaction });
const { guild, user } = interaction;
logger?.silly( if (guild) {
`New interaction: ${id} in guild: ${guild?.name} (${guild?.id})` await upsertGuildMember(guild, user);
); }
switch (interaction.type) { switch (interaction.type) {
case InteractionType.ApplicationCommand: case InteractionType.ApplicationCommand: {
await HandlersHandleCommandInteraction(<CommandInteraction>interaction); if (interaction.isChatInputCommand()) {
break; await chatInputCommand(interaction);
return;
}
if (interaction.isButton()) {
await button(interaction);
return;
}
break;
}
default: default:
logger?.error(`Unknown interaction type: ${interaction.type}`); throw new Error("Unknown interaction type");
} }
await audits.execute(interaction); await audits.execute(interaction);