Merge branch 'dev' into 'main'

Dev

See merge request illegitimate/illegitimate-bot!199
This commit is contained in:
2024-01-26 08:43:46 +00:00
2 changed files with 92 additions and 80 deletions

View File

@@ -1,101 +1,113 @@
import { Command, ContextMenu } from "interfaces"
import color from "./functions/colors"
import env from "./Env"
import { REST, RESTGetAPIApplicationGuildCommandResult, RESTPostAPIApplicationGuildCommandsJSONBody, RESTPutAPIApplicationGuildCommandsJSONBody, Routes } from "discord.js"
import fs from "fs" import fs from "fs"
import { ExtendedClient } from "./Client"
import env from "./Env"
import { Command } from "interfaces"
import { RESTPostAPIChatInputApplicationCommandsJSONBody } from "discord.js"
import color from "./functions/colors"
type FileType = "js" | "ts" type FileType = "js" | "ts"
export default async function autoDeployCommands(fileType: FileType) { export default async function autoDeployCommands(fileType: FileType, client: ExtendedClient) {
const commands: { const commands: RESTPostAPIChatInputApplicationCommandsJSONBody[] = []
name: string
description: string
data: RESTPostAPIApplicationGuildCommandsJSONBody
}[] = []
let commandFiles: string[] = [] let commandFiles: string[] = []
let contentMenuCommands: string[] = [] // let contentMenuCommands: string[] = []
if (fileType === "js") { if (fileType === "js") {
commandFiles = fs.readdirSync("./dist/commands/").filter(file => file.endsWith(fileType)) commandFiles = fs.readdirSync("./dist/commands/").filter(file => file.endsWith(fileType))
contentMenuCommands = fs.readdirSync("./dist/commands-contextmenu/").filter(file => file.endsWith(fileType)) // contentMenuCommands = fs.readdirSync("./dist/commands-contextmenu/").filter(file => file.endsWith(fileType))
} else if (fileType === "ts") { } else if (fileType === "ts") {
commandFiles = fs.readdirSync("./src/commands/").filter(file => file.endsWith(fileType)) commandFiles = fs.readdirSync("./src/commands/").filter(file => file.endsWith(fileType))
contentMenuCommands = fs.readdirSync("./src/commands-contextmenu/").filter(file => file.endsWith(fileType)) // contentMenuCommands = fs.readdirSync("./src/commands-contextmenu/").filter(file => file.endsWith(fileType))
} }
for (const file of commandFiles) { for (const file of commandFiles) {
const command: Command = require(`../commands/${file}`) const command: Command = require(`../commands/${file}`)
if (command.dev) { if (command.dev) {
commands.push({ commands.push(command.data.toJSON())
name: command.name,
description: command.description,
data: command.data.toJSON()
})
}
}
for (const file of contentMenuCommands) {
const command: ContextMenu = require(`../commands-contextmenu/${file}`)
if (command.dev) {
commands.push({
name: command.name,
description: command.description,
data: command.data.toJSON()
})
} }
} }
const rest = new REST({ version: "10" }).setToken(env.dev.devtoken!) const commandData = commands.map(command => {
const currentCommands = (await rest.get(
Routes.applicationGuildCommands(env.dev.devid!, env.dev.guildid!)
)) as RESTGetAPIApplicationGuildCommandResult[]
const currentCommandsInfo = currentCommands.map(command => {
return { return {
name: command.name, name: command.name,
description: command.description, description: command.description,
options: command.options?.map(option => {
return {
name: option.name,
description: option.description,
type: option.type,
} }
}) }),
const newCommandsInfo = commands.map(command => { defaultPermission: command.default_member_permissions ?? null,
}
}).sort((a, b) => a.name > b.name ? 1 : -1)
client.on("ready", async (c) => {
const guildclient = c.guilds.cache.get(env.dev.guildid!)!
const currentCommands = await guildclient.commands.fetch()
if (!currentCommands) return
const currentCommandsData = currentCommands.map(command => {
return { return {
name: command.name, name: command.name,
description: command.description, description: command.description,
options: command.options?.map(option => {
return {
name: option.name,
description: option.description,
type: option.type,
} }
}) }),
defaultPermission: command.defaultMemberPermissions,
}
}).sort((a, b) => a.name > b.name ? 1 : -1)
const sortedCurrentCommandsInfo = currentCommandsInfo.sort((a, b) => const nc = commands.map(cmd => {
a.name.localeCompare(b.name) return " " + cmd.name
)
const sortedNewCommandsInfo = newCommandsInfo.sort((a, b) =>
a.name.localeCompare(b.name)
)
const newCmds = sortedNewCommandsInfo.map(cmd => {
return " " + cmd.name + " was registered."
}).join("\n")
const currentCmds = sortedCurrentCommandsInfo.map(cmd => {
return " " + cmd.name + " was unregistered."
}).join("\n") }).join("\n")
if (JSON.stringify(sortedNewCommandsInfo) === JSON.stringify(sortedCurrentCommandsInfo)) { if (JSON.stringify(commandData) === JSON.stringify(currentCommandsData)) {
console.log(color("Commands are the same, skipping deploy.", "lavender")) console.log(color("Commands are up to date.", "green"))
console.log(color(newCmds, "lavender")) console.log(color(nc, "lavender"))
} else {
console.log(color("Commands are not up to date.", "red"))
if (currentCommands.size === 0) {
for (const cmd of commands) {
await guildclient.commands.create(cmd)
}
console.log(color(nc, "lavender"))
console.log(color("All commands were registered.", "green"))
return return
} }
try { for (const cmd of currentCommandsData) {
console.log(color("Commands are different, starting deploy.", "red")) if (!commandData.find(c => c.name === cmd.name)) {
console.log(color(currentCmds, "red")) await guildclient.commands.delete(currentCommands.find(c => c.name === cmd.name)!.id)
console.log(`Started refreshing ${commands.length} application (/) commands.`) console.log(color(" " + cmd.name + " was unregistered.", "red"))
const data = (await rest.put(
Routes.applicationGuildCommands(env.dev.devid!, env.dev.guildid!),
{ body: commands }
)) as RESTPutAPIApplicationGuildCommandsJSONBody[]
console.log(color("New commands deployed.", "lavender"))
console.log(color(newCmds, "lavender"))
console.log(`Successfully reloaded ${data.length} application (/) commands.`)
} catch (error) {
console.error(error)
} }
}
for (const cmd of commandData) {
if (!currentCommandsData.find(c => c.name === cmd.name)) {
await guildclient.commands.create(commands.find(c => c.name === cmd.name)!)
console.log(color(" " + cmd.name + " was registered.", "lavender"))
}
}
for (const cmd of commandData) {
const currentCommand = currentCommandsData.find(c => c.name === cmd.name)!
const newCmd = commands.find(c => c.name === cmd.name)!
const currentCommandID = currentCommands.find(c => c.name === cmd.name)!.id
if (JSON.stringify(cmd) !== JSON.stringify(currentCommand)) {
await guildclient.commands.edit(currentCommandID, newCmd)
console.log(color(" " + cmd.name + " was updated.", "lavender"))
}
}
console.log(color("-------------", "lavender"))
console.log(color(nc, "lavender"))
}
})
} }

View File

@@ -35,11 +35,11 @@ export class ExtendedClient extends Client {
if (process.env.NODE_ENV === "dev" && process.env.TYPESCRIPT) { if (process.env.NODE_ENV === "dev" && process.env.TYPESCRIPT) {
console.log(color("Running in development mode. [ts-node]", "lavender")) console.log(color("Running in development mode. [ts-node]", "lavender"))
token = env.dev.devtoken! token = env.dev.devtoken!
autoDeployCommands("ts") autoDeployCommands("ts", this)
} else if (process.env.NODE_ENV === "dev" && !process.env.TYPESCRIPT) { } else if (process.env.NODE_ENV === "dev" && !process.env.TYPESCRIPT) {
console.log(color("Running in development mode.", "lavender")) console.log(color("Running in development mode.", "lavender"))
token = env.dev.devtoken! token = env.dev.devtoken!
autoDeployCommands("js") autoDeployCommands("js", this)
} else { } else {
console.log(color("Running in production mode.", "green")) console.log(color("Running in production mode.", "green"))
token = env.prod.token! token = env.prod.token!