Files
illegitimate-bot/src/utils/Events/autocomplete.ts
2025-06-01 20:30:58 +02:00

51 lines
1.9 KiB
TypeScript

import { Events } from "discord.js"
import fs from "fs"
import path from "path"
import { embedColor } from "~/config/options"
import { IAutocomplete } from "~/typings"
import { ExtendedClient as Client } from "~/utils/Client"
import logToChannel from "~/utils/Functions/logtochannel"
import tryCatch from "../Functions/trycatch"
import { log } from "../Logger"
type FileType = "js" | "ts"
export default async function loadAutocompleteEvents(client: Client, ft: FileType) {
const autocompletePath = path.join(import.meta.dirname, "..", "..", "components", "autocomplete")
const autocompleteFiles = fs.readdirSync(autocompletePath).filter(file => file.endsWith(ft))
for (const file of autocompleteFiles) {
const filePath = path.join(autocompletePath, file)
const { default: autocomplete } = await import("file://" + filePath) as { default: IAutocomplete }
client.autocomplete.set(autocomplete.name, autocomplete)
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isAutocomplete()) return
const autocomplete = client.autocomplete.get(interaction.commandName)
if (!autocomplete) {
log.error(`No autocomplete matching ${interaction.commandName} was found.`)
return
}
const [error] = await tryCatch(autocomplete.execute({ interaction, client }))
if (!error) return
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [{
title: "Autocomplete error occured",
description: "```" + error + "```",
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text: interaction.user.username + " | " + interaction.commandName
}
}]
})
}
log.error(error)
})
}