diff --git a/Dockerfile b/Dockerfile index aa81a90..0d34413 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,5 @@ RUN npm clean-install # RUN npm run register +ENTRYPOINT [ "npm", "run", "register" ] CMD [ "npm", "run", "start" ] diff --git a/package-lock.json b/package-lock.json index 7218677..c8f1f08 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1276,7 +1276,6 @@ "resolved": "https://registry.npmjs.org/pg/-/pg-8.16.3.tgz", "integrity": "sha512-enxc1h0jA/aq5oSDMvqyW3q89ra6XIIDZgCX9vkMrnz5DFTw/Ny3Li2lFQ+pt3L6MCgm/5o2o8HW9hiJji+xvw==", "license": "MIT", - "peer": true, "dependencies": { "pg-connection-string": "^2.9.1", "pg-pool": "^3.10.1", @@ -1487,8 +1486,7 @@ "version": "0.2.2", "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.2.2.tgz", "integrity": "sha512-urBwgfrvVP/eAyXx4hluJivBKzuEbSQs9rKWCrCkbSxNv8mxPcUZKeuoF3Uy4mJl3Lwprp6yy5/39VWigZ4K6Q==", - "license": "Apache-2.0", - "peer": true + "license": "Apache-2.0" }, "node_modules/require-directory": { "version": "2.1.1", diff --git a/src/commands/utility/follow.command.ts b/src/commands/utility/follow.command.ts index 6805ad1..6a6808e 100644 --- a/src/commands/utility/follow.command.ts +++ b/src/commands/utility/follow.command.ts @@ -26,8 +26,7 @@ const cmd: CommandDefinition = { const userRepo = AppDataSource.getRepository(Follow); const hostvalue = interaction.options.getString('service'); - const services = await statusService.serviceRepo.find(); - const realHost = services.find((v) => v.notify && v.name == hostvalue); + const realHost = await statusService.serviceRepo.findOne({where: {name: hostvalue+''}}); if (!hostvalue || !realHost) { await interaction.reply({ content: '⚠️ Host not found !', flags: [MessageFlags.Ephemeral] }); diff --git a/src/commands/utility/uptime.command.ts b/src/commands/utility/uptime.command.ts new file mode 100644 index 0000000..7140834 --- /dev/null +++ b/src/commands/utility/uptime.command.ts @@ -0,0 +1,54 @@ +import { ApplicationIntegrationType, ChatInputCommandInteraction, ContainerBuilder, InteractionContextType, MessageFlags, SlashCommandBuilder } from "discord.js"; +import { CommandDefinition } from "../../type"; +import statusService from "../../services/status.service"; + +const cmd: CommandDefinition = { + data: new SlashCommandBuilder() + .setName('uptime') + .setDescription('Get more info for a host.') + .addStringOption((o) => + o.setName('service') + .setDescription('Select a service') + .setRequired(true) + .setAutocomplete(true) + ) + .setIntegrationTypes( + ApplicationIntegrationType.UserInstall + ) + .setContexts( + InteractionContextType.BotDM, + InteractionContextType.Guild, + InteractionContextType.PrivateChannel + ), + async execute(interaction: ChatInputCommandInteraction) { + const hostvalue = interaction.options.getString('service'); + const realHost = await statusService.serviceRepo.findOne({where: {name: hostvalue+''}}); + if (!hostvalue || !realHost) { + await interaction.reply({ content: '⚠️ Host not found !', flags: [MessageFlags.Ephemeral] }); + } else { + const img = await statusService.getStatusImageBar(realHost.id); + + const container = new ContainerBuilder() + .setAccentColor(0x0000ed) + .addTextDisplayComponents((t) => t + .setContent(`## ${realHost.alive ? `${process.env.EMOJI_STATUS_ONLINE}` : `${process.env.EMOJI_STATUS_OFFLINE}`} ${realHost.name}\nService status over 7 days :`) + ) + .addMediaGalleryComponents((m) => + m.addItems((i) => i.setURL('attachment://uptime.png')) + ); + + await interaction.reply({components: [container], flags: [MessageFlags.IsComponentsV2], files: [{attachment: img, name: 'uptime.png'}]}) + } + }, + autocompletes: [ + { + name: 'service', + execute: async (interaction) => { + const services = await statusService.serviceRepo.find(); + interaction.respond(services.map((v) => ({name: v.name, value: v.name}))); + } + } + ] +} + +export default cmd; \ No newline at end of file diff --git a/src/deploy-commands.ts b/src/deploy-commands.ts index 1d88eb8..7df358b 100644 --- a/src/deploy-commands.ts +++ b/src/deploy-commands.ts @@ -2,8 +2,6 @@ import path from "path"; import fs from "fs"; import { configDotenv } from "dotenv"; import { REST, Routes } from "discord.js"; -import "reflect-metadata"; -import { AppDataSource } from "./data-source"; configDotenv(); @@ -59,8 +57,6 @@ const rest = new REST().setToken(process.env.TOKEN); (async () => { try { - // Initialize DataSource first - await AppDataSource.initialize(); console.log("Data Source initialized for command deployment!"); console.log(`Started refreshing ${commands.length} application (/) commands.`); @@ -71,9 +67,6 @@ const rest = new REST().setToken(process.env.TOKEN); ) as any[]; console.log(`Successfully reloaded ${data.length} application (/) commands.`); - - // Close the connection - await AppDataSource.destroy(); process.exit(0); } catch (error) { console.error('[ERROR] Failed to deploy commands:', error); diff --git a/src/index.ts b/src/index.ts index 0016362..73d1c96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,8 +63,7 @@ client.on(Events.InteractionCreate, async interaction => { return; }else if(interaction.isAutocomplete()){ const option = interaction.options.getFocused(true); - - commands.forEach((value) => { + commands.filter((c) => c.data.name == interaction.commandName).forEach((value) => { if(value.autocompletes) { const auto = value.autocompletes.filter((a) => a.name == option.name); if(auto.length >= 1){ diff --git a/src/services/status.service.ts b/src/services/status.service.ts index a545f0f..01395bd 100644 --- a/src/services/status.service.ts +++ b/src/services/status.service.ts @@ -81,13 +81,12 @@ export class StatusService { this.client.user?.setActivity({ name: '💭 Server load and status...' }) } - public async getStatusImageBar(host: string) { + public async getStatusImageBar(serviceId: number) { const datas = await this.hostsLogRepo.createQueryBuilder() - .where('host = :host AND created_at > :date', {host, date: dayjs().subtract(1, 'week').toDate()}).getMany(); + .where('HostsLog.serviceId = :serviceId AND HostsLog.created_at > :date ORDER BY HostsLog.created_at ASC', {serviceId, date: dayjs().subtract(1, 'week').toDate()}).getMany(); const uptimes : { up: boolean, date: Dayjs }[] = datas.map((log) => { - return { up: log.status, date: dayjs(log.created_at)