Updated dir structure

This commit is contained in:
2024-01-15 11:00:11 +01:00
parent 06ab42e258
commit 301ab66f58
26 changed files with 62 additions and 80 deletions

View File

@@ -0,0 +1,79 @@
import { ExtendedClient as Client } from "utils/Client"
import { color } from "config/options.json"
import { Autocomplete } from "interfaces"
import { Events } from "discord.js"
import colorLog from "utils/functions/colors"
import path = require("path")
import fs = require("fs")
import logToChannel from "utils/functions/logtochannel"
type FileType = "js" | "ts"
const embedColor = Number(color.replace("#", "0x"))
export default function loadAutocompleteEvents(client: Client, ft: FileType) {
const autocompletePath = path.join(
__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 autocomplete: Autocomplete = require(filePath)
if (
"name" in autocomplete &&
"execute" in autocomplete &&
autocomplete.type === "autocomplete"
) {
client.autocomplete.set(autocomplete.name, autocomplete)
} else {
console.log(colorLog(
`[WARNING] The autocomplete at ${filePath} is missing a required "name", "execute" or "type" property.`,
"red"
))
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isAutocomplete()) return
const autocomplete = client.autocomplete.get(interaction.commandName)
if (!autocomplete) {
console.error(
`No autocomplete matching ${interaction.commandName} was found.`,
)
return
}
try {
await autocomplete.execute(interaction)
} catch (error) {
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [
{
title: "Autocomplete error occured",
description: String(error),
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text:
interaction.user.username +
" | " +
interaction.commandName,
},
},
],
})
}
console.error(error)
}
})
}

View File

@@ -0,0 +1,83 @@
import { ExtendedClient as Client } from "utils/Client"
import colorLog from "utils/functions/colors"
import { color } from "config/options.json"
import { Button } from "interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import logToChannel from "utils/functions/logtochannel"
type FileType = "js" | "ts"
const embedColor = Number(color.replace("#", "0x"))
export default function loadButtonEvents(client: Client, ft: FileType) {
const btnPath = path.join(__dirname, "..", "..", "components", "buttons")
const btnFiles = fs.readdirSync(btnPath).filter(file => file.endsWith(ft))
for (const file of btnFiles) {
const filePath = path.join(btnPath, file)
const btn: Button = require(filePath)
if ("name" in btn && "execute" in btn && btn.type === "button") {
client.buttons.set(btn.name, btn)
} else {
console.log(colorLog(
`[WARNING] The button at ${filePath} is missing a required "name", "execute" or "type" property.`,
"red"
))
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isButton()) return
const button = client.buttons.get(interaction.customId)
if (!button) {
console.error(
`No event matching ${interaction.customId} was found.`,
)
return
}
try {
await button.execute(interaction)
} catch (error) {
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [
{
title: "Button error occured",
description: "```" + error + "```",
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text:
interaction.user.username +
" | " +
interaction.customId,
},
},
],
})
}
console.error(error)
if (!interaction.deferred) {
await interaction.reply({
embeds: [{
description: "There was an error while executing this button!",
color: embedColor,
}],
ephemeral: true,
})
} else {
await interaction.editReply({
embeds: [{
description: "There was an error while executing this button! 2",
color: embedColor,
}]
})
}
}
})
}

View File

@@ -0,0 +1,84 @@
import { ExtendedClient as Client } from "utils/Client"
import colorLog from "utils/functions/colors"
import { color } from "config/options.json"
import { Command } from "interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import logToChannel from "utils/functions/logtochannel"
type FileType = "js" | "ts"
const embedColor = Number(color.replace("#", "0x"))
export default function loadSlashCommandsEvents(client: Client, ft: FileType) {
const cmdPath = path.join(__dirname, "..", "..", "commands")
const cmdFiles = fs.readdirSync(cmdPath).filter(file => file.endsWith(ft))
for (const file of cmdFiles) {
const filePath = path.join(cmdPath, file)
const cmd: Command = require(filePath)
if ("data" in cmd && "execute" in cmd && cmd.type === "slash") {
client.commands.set(cmd.data.name, cmd)
} else {
console.log(colorLog(
`[WARNING] The command at ${filePath} is missing a required "data", "execute" or "type" property.`,
"red"
))
}
}
//! command handler
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return
const command = client.commands.get(interaction.commandName)
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
)
return
}
try {
await command.execute(interaction, client)
} catch (error) {
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [
{
title: "Command error occured",
description: "```" + error + "```",
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text:
interaction.user.username +
" | " +
interaction.commandName,
},
},
],
})
}
console.error(error)
if (!interaction.deferred) {
await interaction.reply({
embeds: [{
description: "There was an error while executing this command!",
color: embedColor,
}],
ephemeral: true,
})
} else {
await interaction.editReply({
embeds: [{
description: "There was an error while executing this command!",
color: embedColor,
}]
})
}
}
})
}

View File

@@ -0,0 +1,91 @@
import { ExtendedClient as Client } from "utils/Client"
import colorLog from "utils/functions/colors"
import { ContextMenu } from "interfaces"
import { color } from "config/options.json"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import logToChannel from "utils/functions/logtochannel"
type FileType = "js" | "ts"
const embedColor = Number(color.replace("#", "0x"))
export default function loadContextMenuEvents(client: Client, ft: FileType) {
const contextMenuPath = path.join(
__dirname,
"..",
"..",
"commands-contextmenu",
)
const contextMenuFiles = fs
.readdirSync(contextMenuPath)
.filter(file => file.endsWith(ft))
for (const file of contextMenuFiles) {
const filePath = path.join(contextMenuPath, file)
const cmd: ContextMenu = require(filePath)
if ("data" in cmd && "execute" in cmd && cmd.type === "contextmenu") {
client.contextmenus.set(cmd.data.name, cmd)
} else {
console.log(colorLog(
`[WARNING] The command at ${filePath} is missing a required "data", "execute" or "type" property.`,
"red"
))
}
}
//! context menu command handler
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isContextMenuCommand()) return
const command = client.contextmenus.get(interaction.commandName)
if (!command) {
console.error(
`No command matching ${interaction.commandName} was found.`,
)
return
}
try {
await command.execute(interaction)
} catch (error) {
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [
{
title: "Contextmenu error occured",
description: "```" + error + "```",
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text:
interaction.user.username +
" | " +
interaction.commandName,
},
},
],
})
}
console.error(error)
if (!interaction.deferred) {
await interaction.reply({
embeds: [{
description: "There was an error while executing this contextmenu command!",
color: embedColor,
}],
ephemeral: true,
})
} else {
await interaction.editReply({
embeds: [{
description: "There was an error while executing this contextmenu command!",
color: embedColor,
}]
})
}
}
})
}

37
src/utils/Events/cron.ts Normal file
View File

@@ -0,0 +1,37 @@
import { CronJob } from "cron"
import path from "path"
import fs from "fs"
import { Cron } from "interfaces"
export default function loadCronEvents() {
const cronPath = path.join(__dirname, "..", "..", "events", "cron")
const cronFiles = fs
.readdirSync(cronPath)
.filter(file => file.endsWith(".js"))
for (const file of cronFiles) {
const filePath = path.join(cronPath, file)
const cron: Cron = require(filePath)
const time =
cron.time.seconds +
" " +
cron.time.minutes +
" " +
cron.time.hours +
" " +
cron.time.dayOfMonth +
" " +
cron.time.month +
" " +
cron.time.dayOfWeek
new CronJob(
time,
cron.execute,
cron.onComplete,
cron.start,
cron.timeZone,
).start()
}
}

View File

@@ -0,0 +1,17 @@
import { ExtendedClient as Client } from "utils/Client"
import { Event } from "interfaces"
import path = require("path")
import fs = require("fs")
export default function loadEvents(client: Client) {
const serverDir = path.join(__dirname, "..", "..", "events", "server")
const eventDirs = fs.readdirSync(serverDir)
for (const eventDir of eventDirs) {
const eventFiles = fs.readdirSync(path.join(serverDir, eventDir))
for (const eventFile of eventFiles) {
const eventPath = path.join(serverDir, eventDir, eventFile)
const event: Event = require(eventPath)
client.on(event.event, event.execute)
}
}
}

19
src/utils/Events/index.ts Normal file
View File

@@ -0,0 +1,19 @@
import { ExtendedClient as Client } from "../Client"
import loadAutocompleteEvents from "./autocomplete"
import loadButtonEvents from "./button"
import loadSlashCommandsEvents from "./command"
import loadContextMenuEvents from "./contextmenu"
import loadCronEvents from "./cron"
import loadEvents from "./events"
import loadModalEvents from "./modal"
type FileType = "js" | "ts"
export function loadAllEvents(client: Client, ft: FileType) {
loadEvents(client)
loadButtonEvents(client, ft)
loadSlashCommandsEvents(client, ft)
loadContextMenuEvents(client, ft)
loadModalEvents(client, ft)
loadAutocompleteEvents(client, ft)
loadCronEvents()
}

84
src/utils/Events/modal.ts Normal file
View File

@@ -0,0 +1,84 @@
import { ExtendedClient as Client } from "utils/Client"
import colorLog from "utils/functions/colors"
import { color } from "config/options.json"
import { Modal } from "interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import logToChannel from "utils/functions/logtochannel"
type FileType = "js" | "ts"
const embedColor = Number(color.replace("#", "0x"))
export default function loadModalEvents(client: Client, ft: FileType) {
const modalPath = path.join(__dirname, "..", "..", "components", "modals")
const modalFiles = fs
.readdirSync(modalPath)
.filter(file => file.endsWith(ft))
for (const file of modalFiles) {
const filePath = path.join(modalPath, file)
const modal: Modal = require(filePath)
if ("name" in modal && "execute" in modal && modal.type === "modal") {
client.modals.set(modal.name, modal)
} else {
console.log(colorLog(
`[WARNING] The modal at ${filePath} is missing a required "name", "execute" or "type" property.`,
"red"
))
}
}
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isModalSubmit()) return
const modal = client.modals.get(interaction.customId)
if (!modal) {
console.error(
`No modal matching ${interaction.customId} was found.`,
)
return
}
try {
await modal.execute(interaction)
} catch (error) {
if (process.env.NODE_ENV !== "dev") {
await logToChannel("error", {
embeds: [
{
title: "Button error occured",
description: "```" + error + "```",
color: embedColor,
footer: {
icon_url: interaction.guild!.iconURL() || undefined,
text:
interaction.user.username +
" | " +
interaction.customId,
},
},
],
})
}
console.error(error)
if (!interaction.deferred) {
await interaction.reply({
embeds: [{
description: "There was an error while executing this modal!",
color: embedColor
}]
})
} else {
await interaction.editReply({
embeds: [{
description: "There was an error while executing this modal!",
color: embedColor
}]
})
}
}
})
}