Code refactror

This commit is contained in:
2023-12-30 16:53:44 +01:00
parent a4d96dd63d
commit 474a537797
21 changed files with 81 additions and 80 deletions

View File

@@ -1,6 +1,6 @@
import { Command } from "../interfaces"
import config from "./Config"
import color from "./Colors"
import env from "./Env"
import color from "./functions/colors"
import {
REST,
RESTGetAPIApplicationGuildCommandResult,
@@ -36,10 +36,10 @@ async function autoDeployCommands(fileType: FileType) {
}
}
const rest = new REST({ version: "10" }).setToken(config.dev.devtoken!)
const rest = new REST({ version: "10" }).setToken(env.dev.devtoken!)
const currentCommands = (await rest.get(
Routes.applicationGuildCommands(config.dev.devid!, config.dev.guildid!),
Routes.applicationGuildCommands(env.dev.devid!, env.dev.guildid!),
)) as RESTGetAPIApplicationGuildCommandResult[]
const currentCommandsInfo = currentCommands.map(command => {
@@ -98,8 +98,8 @@ async function autoDeployCommands(fileType: FileType) {
const data = (await rest.put(
Routes.applicationGuildCommands(
config.dev.devid!,
config.dev.guildid!,
env.dev.devid!,
env.dev.guildid!,
),
{ body: commands },
)) as RESTPutAPIApplicationGuildCommandsJSONBody[]

View File

@@ -4,7 +4,7 @@ import { ContextMenu } from "../interfaces"
import { Button } from "../interfaces"
import { Modal } from "../interfaces"
import { Autocomplete } from "../interfaces"
import config from "./Config"
import env from "./Env"
import { autoDeployCommands } from "./Autodeploy"
import { loadAllEvents } from "./Events"
@@ -40,17 +40,17 @@ export class ExtendedClient extends Client {
if (process.env.NODE_ENV === "dev" && process.env.TYPESCRIPT) {
console.log("Running in development mode. [ts-node]")
loadAllEvents(this, "ts")
token = config.dev.devtoken!
token = env.dev.devtoken!
autoDeployCommands("ts")
} else if (process.env.NODE_ENV === "dev" && !process.env.TYPESCRIPT) {
console.log("Running in development mode.")
loadAllEvents(this, "js")
token = config.dev.devtoken!
token = env.dev.devtoken!
autoDeployCommands("js")
} else {
console.log("Running in production mode.")
loadAllEvents(this, "js")
token = config.prod.token!
token = env.prod.token!
}
this.login(token)

View File

@@ -1,7 +1,7 @@
import { Config } from "../interfaces"
import { Env } from "../interfaces"
import "dotenv/config"
const config: Config = {
const env: Env = {
prod: {
token: process.env.TOKEN,
mongoURI: process.env.MONGOURI,
@@ -17,4 +17,4 @@ const config: Config = {
},
}
export default config
export default env

View File

@@ -1,10 +1,10 @@
import { ExtendedClient as Client } from "./Client"
import { loadButtonEvents } from "./eventHandlers/button"
import { loadSlashCommandsEvents } from "./eventHandlers/command"
import { loadContextMenuEvents } from "./eventHandlers/contextmenu"
import { loadModalEvents } from "./eventHandlers/modal"
import { loadEvents } from "./eventHandlers/events"
import { loadAutocompleteEvents } from "./eventHandlers/autocomplete"
import { loadButtonEvents } from "./eventHandlers"
import { loadSlashCommandsEvents } from "./eventHandlers"
import { loadContextMenuEvents } from "./eventHandlers"
import { loadModalEvents } from "./eventHandlers"
import { loadEvents } from "./eventHandlers"
import { loadAutocompleteEvents } from "./eventHandlers"
import { FileType } from "../typings"
export function loadAllEvents(client: Client, ft: FileType) {

View File

@@ -1,10 +1,11 @@
import { ExtendedClient as Client } from "./Client"
import config from "./Config"
import { redis } from "./Redis"
import { Redis } from "ioredis"
import env from "./Env"
import { connect } from "mongoose"
import init from "./Init"
import { loadCronEvents } from "./Cron"
import { loadCronEvents } from "./eventHandlers"
const client = new Client()
const redis = new Redis(env.prod.redisURI!)
class Bot {
constructor() {}
@@ -16,10 +17,10 @@ class Bot {
redis.on("ready", () => {
console.log("Connected to Redis")
})
connect(config.prod.mongoURI!, {}).then(() => {
connect(env.prod.mongoURI!, {}).then(() => {
console.log("Connected to MongoDB")
})
}
}
export default { Bot, client }
export default { Bot, client, redis }

View File

@@ -1,7 +1,7 @@
import config from "./Config"
import env from "./Env"
const prodValues = config.prod
const devValues = config.dev
const prodValues = env.prod
const devValues = env.dev
export default function init() {
if (process.env.NODE_ENV === "dev") {

View File

@@ -1,6 +0,0 @@
import { Redis } from "ioredis"
import config from "./Config"
const redis = new Redis(config.prod.redisURI!)
export { redis }

View File

@@ -5,7 +5,7 @@ import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadAutocompleteEvents(client: Client, ft: FileType) {
export default function loadAutocompleteEvents(client: Client, ft: FileType) {
const autocompletePath = path.join(
__dirname,
"..",
@@ -52,6 +52,4 @@ function loadAutocompleteEvents(client: Client, ft: FileType) {
console.error(error)
}
})
}
export { loadAutocompleteEvents }
}

View File

@@ -5,7 +5,7 @@ import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadButtonEvents(client: Client, ft: FileType) {
export default function loadButtonEvents(client: Client, ft: FileType) {
const btnPath = path.join(__dirname, "..", "..", "events", "buttons")
const btnFiles = fs
.readdirSync(btnPath)
@@ -46,6 +46,4 @@ function loadButtonEvents(client: Client, ft: FileType) {
})
}
})
}
export { loadButtonEvents }
}

View File

@@ -5,7 +5,7 @@ import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadSlashCommandsEvents(client: Client, ft: FileType) {
export default function loadSlashCommandsEvents(client: Client, ft: FileType) {
const cmdPath = path.join(__dirname, "..", "..", "commands")
const cmdFiles = fs .readdirSync(cmdPath) .filter(file => file.endsWith(ft))
@@ -45,6 +45,4 @@ function loadSlashCommandsEvents(client: Client, ft: FileType) {
})
}
})
}
export { loadSlashCommandsEvents }
}

View File

@@ -5,7 +5,7 @@ import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadContextMenuEvents(client: Client, ft: FileType) {
export default function loadContextMenuEvents(client: Client, ft: FileType) {
const contextMenuPath = path.join(
__dirname,
"..",
@@ -52,6 +52,4 @@ function loadContextMenuEvents(client: Client, ft: FileType) {
})
}
})
}
export { loadContextMenuEvents }
}

View File

@@ -1,10 +1,10 @@
import { CronJob } from "cron"
import path from "path"
import fs from "fs"
import { Cron } from "../interfaces"
import { Cron } from "../../interfaces"
function loadCronEvents() {
const cronPath = path.join(__dirname, "..", "events", "cron")
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) {
@@ -15,6 +15,4 @@ function loadCronEvents() {
new CronJob(time, cron.execute, cron.onComplete, cron.start, cron.timeZone).start()
}
}
export { loadCronEvents }
}

View File

@@ -3,7 +3,7 @@ import { Event } from "../../interfaces"
import path = require("path")
import fs = require("fs")
function loadEvents(client: Client) {
export default function loadEvents(client: Client) {
const serverDir = path.join(__dirname, "..", "..", "events", "server")
const eventDirs = fs.readdirSync(serverDir)
for (const eventDir of eventDirs) {
@@ -14,6 +14,4 @@ function loadEvents(client: Client) {
client.on(event.event, event.execute)
}
}
}
export { loadEvents }
}

View File

@@ -0,0 +1,17 @@
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"
export {
loadAutocompleteEvents,
loadButtonEvents,
loadSlashCommandsEvents,
loadContextMenuEvents,
loadCronEvents,
loadEvents,
loadModalEvents
}

View File

@@ -5,7 +5,7 @@ import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadModalEvents(client: Client, ft: FileType) {
export default function loadModalEvents(client: Client, ft: FileType) {
const modalPath = path.join(__dirname, "..", "..", "events", "modals")
const modalFiles = fs
.readdirSync(modalPath)

View File

@@ -1,9 +1,9 @@
import fetch from "axios"
import config from "../Config"
import env from "../Env"
import { Profile, Profile2 } from "../../typings"
import { Player, PlayerData } from "../../interfaces"
import { Guild, GuildData } from "../../interfaces"
const apikey = config.prod.hypixelapikey
const apikey = env.prod.hypixelapikey
const mojang = "https://api.mojang.com/users/profiles/minecraft/"
const mojanguuid = "https://sessionserver.mojang.com/session/minecraft/profile/"
const hypixel = "https://api.hypixel.net/player"