From 4f710bde6ccf2eb39069c2854036e0fe5da41655 Mon Sep 17 00:00:00 2001 From: Matteo Juen Date: Thu, 24 Oct 2024 20:41:55 +0200 Subject: [PATCH] feat: all existing commands support on multiple servers --- .../commands/activate-birthday-shoutout.ts | 1 + .../command/commands/add-birthday-entry.ts | 5 +-- .../commands/deactivate-birthday-shoutout.ts | 1 + src/modules/command/commands/get-a-quote.ts | 4 +- .../command/commands/someone-once-said.ts | 2 + .../create-or-update-birthday-entry.dto.ts | 8 ++++ .../service/birthday-entry.service.ts | 39 ++++++++++++------- .../models/poll/dto/update-poll.dto.ts | 35 +++++++++-------- .../models/poll/service/db-poll.service.ts | 13 +++++-- .../models/poll/service/poll.service.ts | 16 ++++++-- .../service/someone-once-said.service.ts | 13 +++++-- src/schemas/birthday-entry.model.ts | 14 +++---- src/schemas/birthday-entry.schema.ts | 8 ++-- src/schemas/poll-entity.model.ts | 20 +++++----- src/schemas/poll.schema.ts | 6 ++- src/schemas/server-config.schema.ts | 19 +++++++++ src/schemas/someone-once-said-entity.model.ts | 1 + src/schemas/someone-once-said.schema.ts | 3 ++ 18 files changed, 139 insertions(+), 69 deletions(-) create mode 100644 src/schemas/server-config.schema.ts diff --git a/src/modules/command/commands/activate-birthday-shoutout.ts b/src/modules/command/commands/activate-birthday-shoutout.ts index 05968ad..00d48b4 100644 --- a/src/modules/command/commands/activate-birthday-shoutout.ts +++ b/src/modules/command/commands/activate-birthday-shoutout.ts @@ -27,6 +27,7 @@ export default class ActivateBirthdayEntryShoutoutCommand extends ACommand { const instance: CreateOrUpdateBirthdayEntryDto = { username: arg.user.username, secName: arg.user.displayName, + serverId: arg.guildId, active: true, }; const inactive = diff --git a/src/modules/command/commands/add-birthday-entry.ts b/src/modules/command/commands/add-birthday-entry.ts index 15483b0..5ebd1a9 100644 --- a/src/modules/command/commands/add-birthday-entry.ts +++ b/src/modules/command/commands/add-birthday-entry.ts @@ -8,10 +8,6 @@ import { ACommand } from '../command.abstract'; import { Inject } from '@nestjs/common'; import { BirthdayEntryService } from '../../models/birthday/service/birthday-entry.service'; import { CreateOrUpdateBirthdayEntryDto } from '../../models/birthday/dto/create-or-update-birthday-entry.dto'; -import { - CommandAccessLevel, - Role, -} from '../../../common/decoratos/role.decorator'; export default class AddBirthdayEntryCommand extends ACommand { constructor( @@ -56,6 +52,7 @@ export default class AddBirthdayEntryCommand extends ACommand { birthDate: dateValue, username: arg.user.username, secName: arg.user.displayName, + serverId: arg.guildId, active: true, }; const created = diff --git a/src/modules/command/commands/deactivate-birthday-shoutout.ts b/src/modules/command/commands/deactivate-birthday-shoutout.ts index 0761f13..14e4fbf 100644 --- a/src/modules/command/commands/deactivate-birthday-shoutout.ts +++ b/src/modules/command/commands/deactivate-birthday-shoutout.ts @@ -27,6 +27,7 @@ export default class DeactivateBirthdayEntryShoutoutCommand extends ACommand { const instance: CreateOrUpdateBirthdayEntryDto = { username: arg.user.username, secName: arg.user.displayName, + serverId: arg.guildId, active: false, }; const inactive = diff --git a/src/modules/command/commands/get-a-quote.ts b/src/modules/command/commands/get-a-quote.ts index cd8232a..f71522e 100644 --- a/src/modules/command/commands/get-a-quote.ts +++ b/src/modules/command/commands/get-a-quote.ts @@ -25,7 +25,9 @@ export default class GetRandomQuote extends ACommand { // @Role(CommandAccessLevel.member) async execute(arg: CommandInteraction): Promise { - const someoneOnceSaid = await this.someoneonceSaidService.getRandomQuote(); + const someoneOnceSaid = await this.someoneonceSaidService.getRandomQuote( + arg.guildId, + ); if (!someoneOnceSaid) return; const quoteEmbed = new EmbedBuilder() .setTitle( diff --git a/src/modules/command/commands/someone-once-said.ts b/src/modules/command/commands/someone-once-said.ts index 2108abc..ba58d86 100644 --- a/src/modules/command/commands/someone-once-said.ts +++ b/src/modules/command/commands/someone-once-said.ts @@ -12,6 +12,7 @@ import { CommandAccessLevel, Role, } from '../../../common/decoratos/role.decorator'; +import { server } from 'typescript'; export default class SomeoneOnceSaidCommand extends ACommand { constructor( @@ -46,6 +47,7 @@ export default class SomeoneOnceSaidCommand extends ACommand { phrase: phraseValue, username: arg.user.username, secName: arg.user.displayName, + serverId: arg.guildId, }); const created = await this.someoneonceSaidService.create(instance); const quoteEmbed = new EmbedBuilder() diff --git a/src/modules/models/birthday/dto/create-or-update-birthday-entry.dto.ts b/src/modules/models/birthday/dto/create-or-update-birthday-entry.dto.ts index efe2a3f..78e8048 100644 --- a/src/modules/models/birthday/dto/create-or-update-birthday-entry.dto.ts +++ b/src/modules/models/birthday/dto/create-or-update-birthday-entry.dto.ts @@ -18,6 +18,14 @@ export class CreateOrUpdateBirthdayEntryDto { @IsOptional() secName: string; + @ApiProperty({ + example: '12398182390132', + description: 'The discord serverId the interaction comes from', + type: String, + }) + @IsNotEmpty() + serverId: string; + @ApiProperty({ example: '2024-01-01T00:00:00.000Z', description: 'The birthdate of the user in ISO format', diff --git a/src/modules/models/birthday/service/birthday-entry.service.ts b/src/modules/models/birthday/service/birthday-entry.service.ts index 1a7ae60..d23940d 100644 --- a/src/modules/models/birthday/service/birthday-entry.service.ts +++ b/src/modules/models/birthday/service/birthday-entry.service.ts @@ -8,8 +8,13 @@ export class BirthdayEntryService { @InjectModel('BirthdayEntry') private readonly birthdayEntry: Model, ) {} - async createOrUpdateBirthdayEntry(birthdayEntryDto: CreateOrUpdateBirthdayEntryDto,) { - const birthdayEntry = await this.birthdayEntry.findOne({ username: birthdayEntryDto.username }); + async createOrUpdateBirthdayEntry( + birthdayEntryDto: CreateOrUpdateBirthdayEntryDto, + ) { + const birthdayEntry = await this.birthdayEntry.findOne({ + username: birthdayEntryDto.username, + serverId: birthdayEntryDto.serverId, + }); if (birthdayEntry) { return await this.updateBirthdayEntry(birthdayEntryDto); } else { @@ -26,6 +31,7 @@ export class BirthdayEntryService { secName: birthdayEntryDto?.secName, birthDate: birthdayEntryDto.birthDate, active: true, + serverId: birthdayEntryDto.serverId, createdAt: new Date(), }); } catch (e) { @@ -33,7 +39,9 @@ export class BirthdayEntryService { } } - async updateBirthdayEntry(birthdayEntryDto: CreateOrUpdateBirthdayEntryDto): Promise { + async updateBirthdayEntry( + birthdayEntryDto: CreateOrUpdateBirthdayEntryDto, + ): Promise { try { return await this.birthdayEntry.findOneAndUpdate( { username: birthdayEntryDto.username }, @@ -51,22 +59,23 @@ export class BirthdayEntryService { async getEntryForToday(): Promise { try { - const entries = await this.birthdayEntry.find({ active: true }); + const entries = await this.birthdayEntry.find({ + active: true, + }); if (!entries) { - return null + return null; } - const today = new Date() + const today = new Date(); - return entries - .filter((entry) => { - const date = new Date(entry.birthDate) - return ( - date.getFullYear() !== today.getFullYear() && - date.getMonth() === today.getMonth() && - date.getDate() === today.getDate() - ) - }) + return entries.filter((entry) => { + const date = new Date(entry.birthDate); + return ( + date.getFullYear() !== today.getFullYear() && + date.getMonth() === today.getMonth() && + date.getDate() === today.getDate() + ); + }); } catch (e) { return null; } diff --git a/src/modules/models/poll/dto/update-poll.dto.ts b/src/modules/models/poll/dto/update-poll.dto.ts index ade9487..095f8b7 100644 --- a/src/modules/models/poll/dto/update-poll.dto.ts +++ b/src/modules/models/poll/dto/update-poll.dto.ts @@ -1,26 +1,27 @@ -import { IsNotEmpty, IsOptional } from "class-validator"; +import { IsNotEmpty, IsOptional } from 'class-validator'; export class UpdatePollDto { - @IsNotEmpty() - msg: string; + @IsNotEmpty() + msg: string; - @IsNotEmpty() - upvotes: number; + @IsNotEmpty() + upvotes: number; - @IsNotEmpty() - downvotes: number; + @IsNotEmpty() + downvotes: number; - @IsOptional() - active: boolean; + @IsNotEmpty() + serverId: string; - @IsOptional() - ownerName: string; + @IsOptional() + active: boolean; - @IsOptional() - downMembers: string[] + @IsOptional() + ownerName: string; - @IsOptional() - upMembers: string[] + @IsOptional() + downMembers: string[]; - -} \ No newline at end of file + @IsOptional() + upMembers: string[]; +} diff --git a/src/modules/models/poll/service/db-poll.service.ts b/src/modules/models/poll/service/db-poll.service.ts index 45bd9da..2efa682 100644 --- a/src/modules/models/poll/service/db-poll.service.ts +++ b/src/modules/models/poll/service/db-poll.service.ts @@ -19,6 +19,7 @@ export class DbPollService { downvotes: pollDto.downvotes, upMembers: pollDto?.upMembers ?? [], downMembers: pollDto?.downMembers ?? [], + serverId: pollDto.serverId, active: true, createdAt: new Date(), }); @@ -28,10 +29,12 @@ export class DbPollService { } } - async update(updateDto: UpdatePollDto): Promise { + async update( + updateDto: UpdatePollDto, + ): Promise { try { return await this.pollModel.findOneAndUpdate( - { msg: updateDto.msg }, + { msg: updateDto.msg, serverId: updateDto.serverId }, { upvotes: updateDto.upvotes, downvotes: updateDto?.downvotes, @@ -46,10 +49,14 @@ export class DbPollService { } } - async get(messageId: string): Promise { + async get( + messageId: string, + serverId: string, + ): Promise { try { const test = await this.pollModel.findOne({ msg: messageId, + serverId: serverId, active: true, }); return test; diff --git a/src/modules/models/poll/service/poll.service.ts b/src/modules/models/poll/service/poll.service.ts index c8615c7..8e501cf 100644 --- a/src/modules/models/poll/service/poll.service.ts +++ b/src/modules/models/poll/service/poll.service.ts @@ -65,6 +65,7 @@ export class PollService { downMembers: [], active: true, ownerName: arg.user.username, + serverId: arg.guild.id, createdAt: new Date(), }); } @@ -79,7 +80,10 @@ export class PollService { } public async upVote(interaction: ButtonInteraction) { - const data = await this.dbPollService.get(interaction.message.id); + const data = await this.dbPollService.get( + interaction.message.id, + interaction.guild.id, + ); if (!data) return; const msg = await interaction.channel.messages.fetch(data.msg); @@ -103,7 +107,10 @@ export class PollService { } public async downVote(interaction: ButtonInteraction) { - const data = await this.dbPollService.get(interaction.message.id); + const data = await this.dbPollService.get( + interaction.message.id, + interaction.guild.id, + ); if (!data) return; const msg = await interaction.channel.messages.fetch(data.msg); @@ -127,7 +134,10 @@ export class PollService { } public async closePoll(interaction: ButtonInteraction) { - const data = await this.dbPollService.get(interaction.message.id); + const data = await this.dbPollService.get( + interaction.message.id, + interaction.guild.id, + ); if (!data) return; const msg = await interaction.channel.messages.fetch(data.msg); if (interaction.user.username == data.ownerName) { diff --git a/src/modules/models/someone-once-said/service/someone-once-said.service.ts b/src/modules/models/someone-once-said/service/someone-once-said.service.ts index 8494e05..447630a 100644 --- a/src/modules/models/someone-once-said/service/someone-once-said.service.ts +++ b/src/modules/models/someone-once-said/service/someone-once-said.service.ts @@ -17,21 +17,26 @@ export class SomeoneOnceSaidService { phrase: quoteDto.phrase, username: quoteDto.username, secName: quoteDto?.secName, + serverId: quoteDto.serverId, createdAt: new Date(), }); } catch (e) { return null; } } - - async getRandomQuote(): Promise { + + async getRandomQuote( + serverId: string, + ): Promise { try { - const count = await this.someoneOnceSaid.countDocuments(); + const count = await this.someoneOnceSaid.countDocuments({ + serverId: serverId, + }); const randomIndex = Math.floor(Math.random() * count); const randomQuote = await this.someoneOnceSaid - .findOne() + .findOne({ serverId: serverId }) .skip(randomIndex) .limit(1); diff --git a/src/schemas/birthday-entry.model.ts b/src/schemas/birthday-entry.model.ts index 773ef5f..6b12618 100644 --- a/src/schemas/birthday-entry.model.ts +++ b/src/schemas/birthday-entry.model.ts @@ -1,8 +1,8 @@ export class BirthdayEntryEntity { - username: string; - secName: string; - birthDate: Date; - createdAt: Date; - active?: boolean; - } - \ No newline at end of file + username: string; + secName: string; + birthDate: Date; + serverId: string; + createdAt: Date; + active?: boolean; +} diff --git a/src/schemas/birthday-entry.schema.ts b/src/schemas/birthday-entry.schema.ts index df61799..853386f 100644 --- a/src/schemas/birthday-entry.schema.ts +++ b/src/schemas/birthday-entry.schema.ts @@ -7,13 +7,16 @@ export class BirthdayEntry { @Prop({ required: false }) secName: string; - + @Prop({ required: true }) birthDate: Date; @Prop({ required: true }) active: boolean; + @Prop({ required: true }) + serverId: string; + @Prop({ required: true }) createdAt: Date; @@ -24,5 +27,4 @@ export class BirthdayEntry { export type BirthdayEntryDocument = BirthdayEntry & Document; -export const BirthdayEntrySchema = - SchemaFactory.createForClass(BirthdayEntry); +export const BirthdayEntrySchema = SchemaFactory.createForClass(BirthdayEntry); diff --git a/src/schemas/poll-entity.model.ts b/src/schemas/poll-entity.model.ts index ebd4a96..d931b41 100644 --- a/src/schemas/poll-entity.model.ts +++ b/src/schemas/poll-entity.model.ts @@ -1,11 +1,11 @@ export class PollEntity { - msg: string; - ownerName: string; - upvotes: number; - downvotes: number; - upMembers?: string[]; - downMembers?: string[]; - active: boolean; - createdAt: Date; - } - \ No newline at end of file + msg: string; + ownerName: string; + upvotes: number; + downvotes: number; + upMembers?: string[]; + downMembers?: string[]; + active: boolean; + serverId: string; + createdAt: Date; +} diff --git a/src/schemas/poll.schema.ts b/src/schemas/poll.schema.ts index 35d77fe..262e5aa 100644 --- a/src/schemas/poll.schema.ts +++ b/src/schemas/poll.schema.ts @@ -23,6 +23,9 @@ export class Poll { @Prop({ required: true }) active: boolean; + @Prop({ required: true }) + serverId: string; + @Prop({ required: true }) createdAt: Date; @@ -33,5 +36,4 @@ export class Poll { export type PollDocument = Poll & Document; -export const PollSchema = - SchemaFactory.createForClass(Poll); +export const PollSchema = SchemaFactory.createForClass(Poll); diff --git a/src/schemas/server-config.schema.ts b/src/schemas/server-config.schema.ts new file mode 100644 index 0000000..5061fb5 --- /dev/null +++ b/src/schemas/server-config.schema.ts @@ -0,0 +1,19 @@ +import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'; + +@Schema() +export class ServerConfig { + @Prop({ required: true }) + channelId: string; + + @Prop({ required: true, unique: true }) + serverId: string; + + constructor(data) { + Object.assign(this, data); + } +} + +export type ServerConfigDocument = ServerConfig & Document; + +export const ServerConfigSchema = SchemaFactory.createForClass(ServerConfig); + diff --git a/src/schemas/someone-once-said-entity.model.ts b/src/schemas/someone-once-said-entity.model.ts index 0eb6d1a..3689343 100644 --- a/src/schemas/someone-once-said-entity.model.ts +++ b/src/schemas/someone-once-said-entity.model.ts @@ -3,4 +3,5 @@ export class SomeoneOnceSaidEntity { username: string; secName?: string; createdAt: Date; + serverId: string; } diff --git a/src/schemas/someone-once-said.schema.ts b/src/schemas/someone-once-said.schema.ts index 1392e66..2117f2f 100644 --- a/src/schemas/someone-once-said.schema.ts +++ b/src/schemas/someone-once-said.schema.ts @@ -11,6 +11,9 @@ export class SomeoneOnceSaid { @Prop({ required: false }) secName: string; + @Prop({ required: true }) + serverId: string; + @Prop({ required: true }) createdAt: Date;