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,8 +1,9 @@
import { getUUID, getPlayer, getGuild, getIGN } from "../../utils/Hypixel"
import { color, devMessage } from "../../../config/options.json"
import { ChannelType, ChatInputCommandInteraction } from "discord.js"
import { redis } from "../../utils/Redis"
import { GuildData } from "../../interfaces/Guild"
import Illegitimate from "../../utils/Illegitimate"
const redis = Illegitimate.redis
async function guildTop(
interaction: ChatInputCommandInteraction,

View File

@@ -15,7 +15,7 @@ import staffapp from "../../schemas/staffAppSchema"
import settings from "../../schemas/settingsSchema"
import { getUUID } from "../../utils/Hypixel"
import { Button } from "../../interfaces"
import config from "../../utils/Config"
import env from "../../utils/Env"
export = {
name: "staffapply",
@@ -42,7 +42,7 @@ export = {
if (interaction.customId === "staffapply") {
await interaction.deferReply({ ephemeral: true })
if (user.user.id !== config.prod.dev) {
if (user.user.id !== env.prod.dev) {
if (status === "0") {
await interaction.editReply(
"Staff applications are currently closed.",
@@ -51,15 +51,15 @@ export = {
}
}
// if (!userRoles.has(guildRole)) {
// await interaction.editReply("You must be a member of the guild to apply for staff.")
// return
// }
if (!userRoles.has(guildRole)) {
await interaction.editReply("You must be a member of the guild to apply for staff.")
return
}
// if (userRoles.has(guildStaff)) {
// await interaction.editReply("You are already a staff member.")
// return
// }
if (userRoles.has(guildStaff)) {
await interaction.editReply("You are already a staff member.")
return
}
const application = await staffapp.findOne({ userID: user.user.id })

View File

@@ -1,4 +1,4 @@
interface ProdConfig {
interface ProdEnv {
token: string | undefined
mongoURI: string | undefined
dev: string | undefined
@@ -6,14 +6,14 @@ interface ProdConfig {
redisURI: string | undefined
}
interface DevConfig {
interface DevEnv {
devtoken: string | undefined
clientid: string | undefined
devid: string | undefined
guildid: string | undefined
}
export default interface Config {
prod: ProdConfig
dev: DevConfig
export default interface Env {
prod: ProdEnv
dev: DevEnv
}

View File

@@ -5,7 +5,7 @@ import ContextMenu from "./ContextMenu"
import Event from "./Event"
import Modal from "./Modal"
import Config from "./Config"
import Env from "./Env"
import Cron from "./Cron"
import TimeZones from "./TimeZones"
@@ -13,7 +13,7 @@ import { Guild, GuildData } from "./Guild"
import { Player, PlayerData } from "./Player"
export {
Config,
Env,
Cron,
TimeZones,
Autocomplete,

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,
"..",
@@ -53,5 +53,3 @@ function loadAutocompleteEvents(client: Client, ft: FileType) {
}
})
}
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)
@@ -47,5 +47,3 @@ 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))
@@ -46,5 +46,3 @@ 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,
"..",
@@ -53,5 +53,3 @@ 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) {
@@ -16,5 +16,3 @@ 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) {
@@ -15,5 +15,3 @@ function loadEvents(client: Client) {
}
}
}
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"