♻️ interactionCreate event

This commit is contained in:
Axel Olausson Holtenäs 2022-04-13 12:23:26 +02:00
parent 61f1111d08
commit bc6ef0c117
No known key found for this signature in database
GPG key ID: 9347A5E873995701
2 changed files with 58 additions and 53 deletions

View file

@ -1,59 +1,11 @@
import config from "../../../config.json";
import logger from "../../logger";
import guilds from "../../database/schemas/guild";
// Dependencies
import { CommandInteraction } from "discord.js";
import { Interaction, ColorResolvable } from "discord.js";
import isCommand from "./isCommand";
export default {
name: "interactionCreate",
async execute(interaction: Interaction) {
// Destructure member, client
const { client, guild } = interaction;
// If interaction is command
if (interaction.isCommand()) {
// Get command from collection
const command = client.commands.get(interaction.commandName);
// If command do not exist
if (!command) return;
// Create guild if it does not exist already
await guilds.findOne({ guildId: guild?.id }, { new: true, upsert: true });
// Defer reply
await interaction.deferReply({ ephemeral: true });
try {
// Execute command
await command.execute(interaction);
const { commandName, user } = interaction;
return logger?.verbose(
`Guild: ${guild?.id} User: ${user?.tag} executed ${commandName}`
);
} catch (e) {
// Send debug message
logger.error(e);
// Send interaction reply
await interaction.editReply({
embeds: [
{
author: {
name: client?.user?.username,
icon_url: client?.user?.displayAvatarURL(),
url: "https://bot.zyner.org/",
},
title: "Error",
description: "There was an error while executing this command!",
color: config.colors.error as ColorResolvable,
timestamp: new Date(),
},
],
});
}
}
async execute(interaction: CommandInteraction) {
await isCommand(interaction);
},
};

View file

@ -0,0 +1,53 @@
import { ColorResolvable, CommandInteraction } from "discord.js";
import config from "../../../config.json";
import logger from "../../logger";
import guilds from "../../database/schemas/guild";
export default async (interaction: CommandInteraction) => {
if (!interaction.isCommand()) return;
const { client, guild } = interaction;
// Get command from collection
const command = client.commands.get(interaction.commandName);
// If command do not exist
if (!command) return;
// Create guild if it does not exist already
await guilds.findOne({ guildId: guild?.id }, { new: true, upsert: true });
// Defer reply
await interaction.deferReply({ ephemeral: true });
try {
// Execute command
await command.execute(interaction);
const { commandName, user } = interaction;
return logger?.verbose(
`Guild: ${guild?.id} User: ${user?.tag} executed ${commandName}`
);
} catch (e) {
// Send debug message
logger.error(e);
// Send interaction reply
await interaction.editReply({
embeds: [
{
author: {
name: client?.user?.username,
icon_url: client?.user?.displayAvatarURL(),
url: "https://bot.zyner.org/",
},
title: "Error",
description: "There was an error while executing this command!",
color: config.colors.error as ColorResolvable,
timestamp: new Date(),
},
],
});
}
};