From 491d417de579ee7285339f20419353fb5edc7009 Mon Sep 17 00:00:00 2001 From: Matteo Juen Date: Fri, 5 Apr 2024 10:51:56 +0200 Subject: [PATCH 1/2] feat(slashcommands): version command --- src/modules/command/command.abstract.ts | 1 - src/modules/command/command.module.ts | 7 +++-- src/modules/command/command.service.ts | 7 +++-- src/modules/command/commands/version.ts | 28 +++++++++++++++++++ src/modules/version/module/version.module.ts | 10 +++++++ .../version/service/version.service.ts | 17 +++++++++++ 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/modules/command/commands/version.ts create mode 100644 src/modules/version/module/version.module.ts create mode 100644 src/modules/version/service/version.service.ts diff --git a/src/modules/command/command.abstract.ts b/src/modules/command/command.abstract.ts index b5ee918..b62202b 100644 --- a/src/modules/command/command.abstract.ts +++ b/src/modules/command/command.abstract.ts @@ -1,7 +1,6 @@ import { CacheType, CommandInteraction, - Interaction, SlashCommandBuilder, } from 'discord.js'; diff --git a/src/modules/command/command.module.ts b/src/modules/command/command.module.ts index 884ac59..76d1c66 100644 --- a/src/modules/command/command.module.ts +++ b/src/modules/command/command.module.ts @@ -12,6 +12,8 @@ import { SomeoneOnceSaidModule } from '../someone-once-said/module/someone-once- import GetRandomQuote from './commands/get-a-quote'; import { PollCommand } from './commands/poll'; import { PollModule } from '../poll/module/poll.module'; +import { VersionModule } from '../version/module/version.module'; +import { VersionCommand } from './commands/version'; @Module({ providers: [ @@ -25,9 +27,10 @@ import { PollModule } from '../poll/module/poll.module'; GoldCommand, SomeoneOnceSaidCommand, GetRandomQuote, - PollCommand + PollCommand, + VersionCommand ], - imports: [SomeoneOnceSaidModule, PollModule], + imports: [SomeoneOnceSaidModule, PollModule, VersionModule], exports: [CommandService], }) export class CommandModule {} diff --git a/src/modules/command/command.service.ts b/src/modules/command/command.service.ts index 8c6863a..8a59048 100644 --- a/src/modules/command/command.service.ts +++ b/src/modules/command/command.service.ts @@ -9,6 +9,7 @@ import { GoldCommand } from './commands/gold'; import SomeoneOnceSaidCommand from './commands/someone-once-said'; import GetRandomQuote from './commands/get-a-quote'; import { PollCommand } from './commands/poll'; +import { VersionCommand } from './commands/version'; @Injectable() export class CommandService { @@ -24,7 +25,8 @@ export class CommandService { goldModule: GoldCommand, someoneOnceSaidModule: SomeoneOnceSaidCommand, getRandomQuoteModule: GetRandomQuote, - pollModule: PollCommand + pollModule: PollCommand, + versionModule: VersionCommand ) { const commands: ACommand[] = [ //pingpongModule, @@ -36,7 +38,8 @@ export class CommandService { goldModule, someoneOnceSaidModule, getRandomQuoteModule, - pollModule + pollModule, + versionModule ]; commands.forEach((command) => { if (command.data.name && command.execute) { diff --git a/src/modules/command/commands/version.ts b/src/modules/command/commands/version.ts new file mode 100644 index 0000000..c050d31 --- /dev/null +++ b/src/modules/command/commands/version.ts @@ -0,0 +1,28 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { SlashCommandBuilder } from 'discord.js'; +import { ACommand } from '../command.abstract'; +import { VersionService } from '../../version/service/version.service'; + +@Injectable() +export class VersionCommand extends ACommand { + constructor( + @Inject(VersionService) private readonly versionService: VersionService, + ) { + super(); + } + data = new SlashCommandBuilder() + .setName('version') + .setDescription( + 'show the version of the currently running bingus instance', + ); + + async execute(interaction) { + const version = await this.versionService.getVersion(); + return this.run(async () => { + if (version && version.length > 0) { + return await interaction.reply('The current version is: ${version}'); + } + return await interaction.reply('Failed to get version'); + }); + } +} diff --git a/src/modules/version/module/version.module.ts b/src/modules/version/module/version.module.ts new file mode 100644 index 0000000..82123d7 --- /dev/null +++ b/src/modules/version/module/version.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { VersionService } from '../service/version.service'; + +@Module({ + imports: [], + controllers: [], + providers: [VersionService], + exports: [VersionService], +}) +export class VersionModule {} diff --git a/src/modules/version/service/version.service.ts b/src/modules/version/service/version.service.ts new file mode 100644 index 0000000..400e173 --- /dev/null +++ b/src/modules/version/service/version.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@nestjs/common'; +import * as fs from 'fs'; + +@Injectable() +export class VersionService { + private version: string; + + constructor() { + // Read package.json synchronously + const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf8')); + this.version = packageJson.version; + } + + getVersion(): string { + return this.version; + } +} From 49a716a840b3283f9f558b5e2e574b8b170e977c Mon Sep 17 00:00:00 2001 From: Matteo Juen Date: Fri, 5 Apr 2024 10:53:55 +0200 Subject: [PATCH 2/2] fix: template string --- src/modules/command/commands/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/command/commands/version.ts b/src/modules/command/commands/version.ts index c050d31..7c77372 100644 --- a/src/modules/command/commands/version.ts +++ b/src/modules/command/commands/version.ts @@ -20,7 +20,7 @@ export class VersionCommand extends ACommand { const version = await this.versionService.getVersion(); return this.run(async () => { if (version && version.length > 0) { - return await interaction.reply('The current version is: ${version}'); + return await interaction.reply(`The current version is: ${version}`); } return await interaction.reply('Failed to get version'); });