Skip to content

Commit

Permalink
feat: all existing commands support on multiple servers
Browse files Browse the repository at this point in the history
  • Loading branch information
sanriodev committed Oct 24, 2024
1 parent 20a6a63 commit 4f710bd
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/modules/command/commands/activate-birthday-shoutout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
5 changes: 1 addition & 4 deletions src/modules/command/commands/add-birthday-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion src/modules/command/commands/get-a-quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export default class GetRandomQuote extends ACommand {

// @Role(CommandAccessLevel.member)
async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
const someoneOnceSaid = await this.someoneonceSaidService.getRandomQuote();
const someoneOnceSaid = await this.someoneonceSaidService.getRandomQuote(
arg.guildId,
);
if (!someoneOnceSaid) return;
const quoteEmbed = new EmbedBuilder()
.setTitle(
Expand Down
2 changes: 2 additions & 0 deletions src/modules/command/commands/someone-once-said.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CommandAccessLevel,
Role,
} from '../../../common/decoratos/role.decorator';
import { server } from 'typescript';

export default class SomeoneOnceSaidCommand extends ACommand {
constructor(
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 24 additions & 15 deletions src/modules/models/birthday/service/birthday-entry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ export class BirthdayEntryService {
@InjectModel('BirthdayEntry')
private readonly birthdayEntry: Model<BirthdayEntryDocument>,
) {}
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 {
Expand All @@ -26,14 +31,17 @@ export class BirthdayEntryService {
secName: birthdayEntryDto?.secName,
birthDate: birthdayEntryDto.birthDate,
active: true,
serverId: birthdayEntryDto.serverId,
createdAt: new Date(),
});
} catch (e) {
return null;
}
}

async updateBirthdayEntry(birthdayEntryDto: CreateOrUpdateBirthdayEntryDto): Promise<BirthdayEntryDocument> {
async updateBirthdayEntry(
birthdayEntryDto: CreateOrUpdateBirthdayEntryDto,
): Promise<BirthdayEntryDocument> {
try {
return await this.birthdayEntry.findOneAndUpdate(
{ username: birthdayEntryDto.username },
Expand All @@ -51,22 +59,23 @@ export class BirthdayEntryService {

async getEntryForToday(): Promise<BirthdayEntryDocument[]> {
try {
const entries = await this.birthdayEntry.find<BirthdayEntryDocument>({ active: true });
const entries = await this.birthdayEntry.find<BirthdayEntryDocument>({
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;
}
Expand Down
35 changes: 18 additions & 17 deletions src/modules/models/poll/dto/update-poll.dto.ts
Original file line number Diff line number Diff line change
@@ -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[];


}
@IsOptional()
upMembers: string[];
}
13 changes: 10 additions & 3 deletions src/modules/models/poll/service/db-poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class DbPollService {
downvotes: pollDto.downvotes,
upMembers: pollDto?.upMembers ?? [],
downMembers: pollDto?.downMembers ?? [],
serverId: pollDto.serverId,
active: true,
createdAt: new Date(),
});
Expand All @@ -28,10 +29,12 @@ export class DbPollService {
}
}

async update(updateDto: UpdatePollDto): Promise<PollDocument | null | undefined> {
async update(
updateDto: UpdatePollDto,
): Promise<PollDocument | null | undefined> {
try {
return await this.pollModel.findOneAndUpdate(
{ msg: updateDto.msg },
{ msg: updateDto.msg, serverId: updateDto.serverId },
{
upvotes: updateDto.upvotes,
downvotes: updateDto?.downvotes,
Expand All @@ -46,10 +49,14 @@ export class DbPollService {
}
}

async get(messageId: string): Promise<PollDocument | undefined> {
async get(
messageId: string,
serverId: string,
): Promise<PollDocument | undefined> {
try {
const test = await this.pollModel.findOne({
msg: messageId,
serverId: serverId,
active: true,
});
return test;
Expand Down
16 changes: 13 additions & 3 deletions src/modules/models/poll/service/poll.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class PollService {
downMembers: [],
active: true,
ownerName: arg.user.username,
serverId: arg.guild.id,
createdAt: new Date(),
});
}
Expand All @@ -79,7 +80,10 @@ export class PollService {
}

public async upVote(interaction: ButtonInteraction<CacheType>) {
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);
Expand All @@ -103,7 +107,10 @@ export class PollService {
}

public async downVote(interaction: ButtonInteraction<CacheType>) {
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);
Expand All @@ -127,7 +134,10 @@ export class PollService {
}

public async closePoll(interaction: ButtonInteraction<CacheType>) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeoneOnceSaidDocument | null> {

async getRandomQuote(
serverId: string,
): Promise<SomeoneOnceSaidDocument | null> {
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);

Expand Down
14 changes: 7 additions & 7 deletions src/schemas/birthday-entry.model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class BirthdayEntryEntity {
username: string;
secName: string;
birthDate: Date;
createdAt: Date;
active?: boolean;
}
username: string;
secName: string;
birthDate: Date;
serverId: string;
createdAt: Date;
active?: boolean;
}
8 changes: 5 additions & 3 deletions src/schemas/birthday-entry.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,5 +27,4 @@ export class BirthdayEntry {

export type BirthdayEntryDocument = BirthdayEntry & Document;

export const BirthdayEntrySchema =
SchemaFactory.createForClass(BirthdayEntry);
export const BirthdayEntrySchema = SchemaFactory.createForClass(BirthdayEntry);
20 changes: 10 additions & 10 deletions src/schemas/poll-entity.model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export class PollEntity {
msg: string;
ownerName: string;
upvotes: number;
downvotes: number;
upMembers?: string[];
downMembers?: string[];
active: boolean;
createdAt: Date;
}
msg: string;
ownerName: string;
upvotes: number;
downvotes: number;
upMembers?: string[];
downMembers?: string[];
active: boolean;
serverId: string;
createdAt: Date;
}
6 changes: 4 additions & 2 deletions src/schemas/poll.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export class Poll {
@Prop({ required: true })
active: boolean;

@Prop({ required: true })
serverId: string;

@Prop({ required: true })
createdAt: Date;

Expand All @@ -33,5 +36,4 @@ export class Poll {

export type PollDocument = Poll & Document;

export const PollSchema =
SchemaFactory.createForClass(Poll);
export const PollSchema = SchemaFactory.createForClass(Poll);
Loading

0 comments on commit 4f710bd

Please sign in to comment.