Added dev script for ts-node

This commit is contained in:
2023-12-30 00:03:32 +01:00
parent 7d7b44412e
commit efa45e7db9
12 changed files with 89 additions and 59 deletions

14
nodemon-ts.json Normal file
View File

@@ -0,0 +1,14 @@
{
"restartable": "rs",
"ignore": [
".git",
"node_modules/**/node_modules",
"dist/config"
],
"verbose": true,
"env": {
"NODE_ENV": "dev",
"TYPESCRIPT": "true"
},
"ext": "ts, json"
}

View File

@@ -8,6 +8,7 @@
"build": "tsc",
"watch": "tsc -w",
"dev": "nodemon dist/src/index.js",
"dev:ts": "nodemon --config nodemon-ts.json",
"dev:build": "ts-node scripts/dev-deploy.ts",
"dev:delete": "ts-node scripts/delete-commands.ts",
"format": "prettier --write src/",

View File

@@ -13,3 +13,5 @@ export type Profile2 = {
profileActions: []
}
}
export type FileType = "js" | "ts"

View File

@@ -1,3 +1,3 @@
import { Profile, Profile2 } from "./Profile"
import { Profile, Profile2, FileType } from "./Types"
export { Profile, Profile2 }
export { Profile, Profile2, FileType }

View File

@@ -8,15 +8,20 @@ import {
Routes,
} from "discord.js"
import fs = require("fs")
import { FileType } from "../typings"
async function autoDeployCommands() {
async function autoDeployCommands(fileType: FileType) {
const commands = []
const commandFiles = fs
.readdirSync("./dist/src/commands/")
.filter(file => file.endsWith(".js"))
const contentMenuCommands = fs
.readdirSync("./dist/src/commands-contextmenu/")
.filter(file => file.endsWith(".js"))
let commandFiles: string[] = []
let contentMenuCommands: string[] = []
if (fileType === "js") {
commandFiles = fs.readdirSync("./dist/src/commands/").filter(file => file.endsWith(fileType))
contentMenuCommands = fs.readdirSync("./dist/src/commands-contextmenu/").filter(file => file.endsWith(fileType))
} else if (fileType === "ts") {
commandFiles = fs.readdirSync("./src/commands/").filter(file => file.endsWith(fileType))
contentMenuCommands = fs.readdirSync("./src/commands-contextmenu/").filter(file => file.endsWith(fileType))
}
for (const file of commandFiles) {
const command = require(`../commands/${file}`)
@@ -79,36 +84,34 @@ async function autoDeployCommands() {
return
}
;(async () => {
try {
console.log(
color.colorize(
"Commands are different, starting deploy.",
"red",
),
)
console.log(color.colorize(currentCmds, "red"))
console.log(
`Started refreshing ${commands.length} application (/) commands.`,
)
try {
console.log(
color.colorize(
"Commands are different, starting deploy.",
"red",
),
)
console.log(color.colorize(currentCmds, "red"))
console.log(
`Started refreshing ${commands.length} application (/) commands.`,
)
const data = (await rest.put(
Routes.applicationGuildCommands(
config.dev.devid!,
config.dev.guildid!,
),
{ body: commands },
)) as RESTPutAPIApplicationGuildCommandsJSONBody[]
const data = (await rest.put(
Routes.applicationGuildCommands(
config.dev.devid!,
config.dev.guildid!,
),
{ body: commands },
)) as RESTPutAPIApplicationGuildCommandsJSONBody[]
console.log(color.colorize("New commands deployed.", "green"))
console.log(color.colorize(newCmds, "green"))
console.log(
`Successfully reloaded ${data.length} application (/) commands.`,
)
} catch (error) {
console.error(error)
}
})()
console.log(color.colorize("New commands deployed.", "green"))
console.log(color.colorize(newCmds, "green"))
console.log(
`Successfully reloaded ${data.length} application (/) commands.`,
)
} catch (error) {
console.error(error)
}
}
export { autoDeployCommands }

View File

@@ -35,15 +35,21 @@ export class ExtendedClient extends Client {
}
async start() {
loadAllEvents(this)
let token: string
if (process.env.NODE_ENV === "dev") {
console.log("Running in development mode.")
if (process.env.NODE_ENV === "dev" && process.env.TYPESCRIPT) {
console.log("Running in development mode. [ts-node]")
loadAllEvents(this, "ts")
token = config.dev.devtoken!
autoDeployCommands()
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!
autoDeployCommands("js")
} else {
console.log("Running in production mode.")
loadAllEvents(this, "js")
token = config.prod.token!
}

View File

@@ -5,12 +5,13 @@ import { loadContextMenuEvents } from "./eventHandlers/contextmenu"
import { loadModalEvents } from "./eventHandlers/modal"
import { loadEvents } from "./eventHandlers/events"
import { loadAutocompleteEvents } from "./eventHandlers/autocomplete"
import { FileType } from "../typings"
export function loadAllEvents(client: Client) {
export function loadAllEvents(client: Client, ft: FileType) {
loadEvents(client)
loadButtonEvents(client)
loadSlashCommandsEvents(client)
loadContextMenuEvents(client)
loadModalEvents(client)
loadAutocompleteEvents(client)
loadButtonEvents(client, ft)
loadSlashCommandsEvents(client, ft)
loadContextMenuEvents(client, ft)
loadModalEvents(client, ft)
loadAutocompleteEvents(client, ft)
}

View File

@@ -3,8 +3,9 @@ import { Autocomplete } from "../../interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadAutocompleteEvents(client: Client) {
function loadAutocompleteEvents(client: Client, ft: FileType) {
const autocompletePath = path.join(
__dirname,
"..",
@@ -14,7 +15,7 @@ function loadAutocompleteEvents(client: Client) {
)
const autocompleteFiles = fs
.readdirSync(autocompletePath)
.filter(file => file.endsWith(".js"))
.filter(file => file.endsWith(ft))
for (const file of autocompleteFiles) {
const filePath = path.join(autocompletePath, file)

View File

@@ -3,12 +3,13 @@ import { Button } from "../../interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadButtonEvents(client: Client) {
function loadButtonEvents(client: Client, ft: FileType) {
const btnPath = path.join(__dirname, "..", "..", "events", "buttons")
const btnFiles = fs
.readdirSync(btnPath)
.filter(file => file.endsWith(".js"))
.filter(file => file.endsWith(ft))
for (const file of btnFiles) {
const filePath = path.join(btnPath, file)

View File

@@ -3,12 +3,11 @@ import { Command } from "../../interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadSlashCommandsEvents(client: Client) {
function loadSlashCommandsEvents(client: Client, ft: FileType) {
const cmdPath = path.join(__dirname, "..", "..", "commands")
const cmdFiles = fs
.readdirSync(cmdPath)
.filter(file => file.endsWith(".js"))
const cmdFiles = fs .readdirSync(cmdPath) .filter(file => file.endsWith(ft))
for (const file of cmdFiles) {
const filePath = path.join(cmdPath, file)

View File

@@ -3,8 +3,9 @@ import { ContextMenu } from "../../interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadContextMenuEvents(client: Client) {
function loadContextMenuEvents(client: Client, ft: FileType) {
const contextMenuPath = path.join(
__dirname,
"..",
@@ -13,7 +14,7 @@ function loadContextMenuEvents(client: Client) {
)
const contextMenuFiles = fs
.readdirSync(contextMenuPath)
.filter(file => file.endsWith(".js"))
.filter(file => file.endsWith(ft))
for (const file of contextMenuFiles) {
const filePath = path.join(contextMenuPath, file)

View File

@@ -3,12 +3,13 @@ import { Modal } from "../../interfaces"
import { Events } from "discord.js"
import path = require("path")
import fs = require("fs")
import { FileType } from "../../typings"
function loadModalEvents(client: Client) {
function loadModalEvents(client: Client, ft: FileType) {
const modalPath = path.join(__dirname, "..", "..", "events", "modals")
const modalFiles = fs
.readdirSync(modalPath)
.filter(file => file.endsWith(".js"))
.filter(file => file.endsWith(ft))
for (const file of modalFiles) {
const filePath = path.join(modalPath, file)