Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature |Birthdaylist and automated shoutouts with cron #84

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bun.lockb
Binary file not shown.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"joi": "^17.10.2",
"mongoose": "^8.0.1",
"ngrok": "^5.0.0-beta.2",
"node-cron": "^3.0.3",
"node-fetch": "^3.3.2",
"rimraf": "^5.0.1",
"source-map-support": "^0.5.21",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CommandModule } from './modules/command/command.module';
import { EventModule } from './modules/event/event.module';
import { DeployModule } from './deployment/deploy.module';
import { MongoDatabaseProviderModule } from './config/database/mongo/provider/mongo-provider.module';
import { TaskModule } from './modules/cron-tasks/task.module';

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { MongoDatabaseProviderModule } from './config/database/mongo/provider/mo
DeployModule,
EventModule,
MongoDatabaseProviderModule,
TaskModule
],
})
export class AppModule {}
Binary file added src/assets/cakeday.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/modules/birthday/module/birthday-entry.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { BirthdayEntry, BirthdayEntrySchema } from '../../../schemas/birthday-entry.schema';
import { BirthdayEntryService } from '../service/birthday-entry.service';

@Module({
imports: [
MongooseModule.forFeature([
{
name: BirthdayEntry.name,
schema: BirthdayEntrySchema,
},
]),
],
controllers: [],
providers: [BirthdayEntryService],
exports: [BirthdayEntryService],
})
export class BirthdayEntryModule {}
74 changes: 74 additions & 0 deletions src/modules/birthday/service/birthday-entry.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { BirthdayEntryDocument } from '../../../schemas/birthday-entry.schema';
import { BirthdayEntryEntity } from '../../../schemas/birthday-entry.model';

export class BirthdayEntryService {
constructor(
@InjectModel('BirthdayEntry')
private readonly birthdayEntry: Model<BirthdayEntryDocument>,
) {}
async createOrUpdateBirthdayEntry(birthdayEntryDto: BirthdayEntryEntity,) {
const birthdayEntry = await this.birthdayEntry.findOne({ username: birthdayEntryDto.username });
if (birthdayEntry) {
return await this.updateBirthdayEntry(birthdayEntryDto);
} else {
return await this.create(birthdayEntryDto);
}
}

async create(
birthdayEntryDto: BirthdayEntryEntity,
): Promise<BirthdayEntryDocument> {
try {
return await this.birthdayEntry.create({
username: birthdayEntryDto.username,
secName: birthdayEntryDto?.secName,
birthDate: birthdayEntryDto.birthDate,
active: true,
createdAt: new Date(),
});
} catch (e) {
return null;
}
}

async updateBirthdayEntry(birthdayEntryDto: BirthdayEntryEntity): Promise<BirthdayEntryDocument> {
try {
return await this.birthdayEntry.findOneAndUpdate(
{ username: birthdayEntryDto.username },
{
secName: birthdayEntryDto.secName,
birthDate: birthdayEntryDto.birthDate,
active: birthdayEntryDto.active,
},
{ new: true },
);
} catch (e) {
return null;
}
}

async getEntryForToday(): Promise<BirthdayEntryDocument[]> {
try {
let entries = await this.birthdayEntry.find<BirthdayEntryDocument>({ active: true });
if (!entries) {
return null
}

let today = new Date()

return entries
.filter((entry) => {
let date = new Date(entry.birthDate)
return (
date.getFullYear() !== today.getFullYear() &&
date.getMonth() === today.getMonth() &&
date.getDate() === today.getDate()
)
})
} catch (e) {
return null;
}
}
}
11 changes: 9 additions & 2 deletions src/modules/command/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import { PollCommand } from './commands/poll';
import { PollModule } from '../poll/module/poll.module';
import { VersionModule } from '../version/module/version.module';
import { VersionCommand } from './commands/version';
import { BirthdayEntryModule } from '../birthday/module/birthday-entry.module';
import AddBirthdayEntryCommand from './commands/add-birthday-entry';
import ActivateBirthdayEntryShoutoutCommand from './commands/activate-birthday-shoutout';
import DeactivateBirthdayEntryShoutoutCommand from './commands/deactivate-birthday-shoutout';

@Module({
providers: [
Expand All @@ -28,9 +32,12 @@ import { VersionCommand } from './commands/version';
SomeoneOnceSaidCommand,
GetRandomQuote,
PollCommand,
VersionCommand
VersionCommand,
AddBirthdayEntryCommand,
DeactivateBirthdayEntryShoutoutCommand,
ActivateBirthdayEntryShoutoutCommand,
],
imports: [SomeoneOnceSaidModule, PollModule, VersionModule],
imports: [SomeoneOnceSaidModule, PollModule, VersionModule, BirthdayEntryModule],
exports: [CommandService],
})
export class CommandModule {}
14 changes: 12 additions & 2 deletions src/modules/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import SomeoneOnceSaidCommand from './commands/someone-once-said';
import GetRandomQuote from './commands/get-a-quote';
import { PollCommand } from './commands/poll';
import { VersionCommand } from './commands/version';
import { BirthdayEntry } from '../../schemas/birthday-entry.schema';
import AddBirthdayEntryCommand from './commands/add-birthday-entry';
import DeactivateBirthdayEntryShoutoutCommand from './commands/deactivate-birthday-shoutout';
import ActivateBirthdayEntryShoutoutCommand from './commands/activate-birthday-shoutout';

@Injectable()
export class CommandService {
Expand All @@ -26,7 +30,10 @@ export class CommandService {
someoneOnceSaidModule: SomeoneOnceSaidCommand,
getRandomQuoteModule: GetRandomQuote,
pollModule: PollCommand,
versionModule: VersionCommand
versionModule: VersionCommand,
addBirthdayEntryModule: AddBirthdayEntryCommand,
deactivateBirthdayEntryShoutoutModule: DeactivateBirthdayEntryShoutoutCommand,
activateBirthdayEntryShoutoutModule: ActivateBirthdayEntryShoutoutCommand,
) {
const commands: ACommand[] = [
//pingpongModule,
Expand All @@ -39,7 +46,10 @@ export class CommandService {
someoneOnceSaidModule,
getRandomQuoteModule,
pollModule,
versionModule
versionModule,
addBirthdayEntryModule,
deactivateBirthdayEntryShoutoutModule,
activateBirthdayEntryShoutoutModule,
];
commands.forEach((command) => {
if (command.data.name && command.execute) {
Expand Down
41 changes: 41 additions & 0 deletions src/modules/command/commands/activate-birthday-shoutout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
CacheType,
CommandInteraction,
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../birthday/service/birthday-entry.service';
import { BirthdayEntry } from '../../../schemas/birthday-entry.schema';

export default class ActivateBirthdayEntryShoutoutCommand extends ACommand {
constructor(
@Inject(BirthdayEntryService)
private readonly birthdayEntryService: BirthdayEntryService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('activate-birthday-shoutout')
.setDescription(
'Use this if you want Bingus to shout you out on your birthday (default)!',
);

async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
await arg.deferReply();
const instance = new BirthdayEntry({
username: arg.user.username,
secName: arg.user.displayName,
active: true
});
const inactive =
await this.birthdayEntryService.updateBirthdayEntry(instance);
if (!inactive) {
await arg.editReply({ content: 'I don\'t know your birthday yet! 🎉'});
} else {
await arg.editReply({ content: 'You will now receive a birthday shoutout from Bingus! 🎉'});
}
return true;
}
}

68 changes: 68 additions & 0 deletions src/modules/command/commands/add-birthday-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
CacheType,
CommandInteraction,
EmbedBuilder,
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../birthday/service/birthday-entry.service';
import { BirthdayEntry } from '../../../schemas/birthday-entry.schema';

export default class AddBirthdayEntryCommand extends ACommand {
constructor(
@Inject(BirthdayEntryService)
private readonly birthdayEntryService: BirthdayEntryService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('addbirthday')
.setDescription(
'Add your Birhtday to receive a special message on your day!',
)
.addNumberOption((option) =>
option.setName('day').setDescription('your day of birth'),
)
.addNumberOption((option) =>
option.setName('month').setDescription('your month of birth'),
)
.addNumberOption((option) =>
option.setName('year').setDescription('your year of birth'),
);

async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
let day = arg.options.get('day');
let month = arg.options.get('month');
let year = arg.options.get('year');
if (!day || !month || !year) {
await arg.reply({
content: 'yo listen you need to provide a day, month and year! 🤓',
ephemeral: true,
});
return;
}
await arg.deferReply();
let dayValue = day.value as unknown as number;
let monthValue = month.value as unknown as number;
let yearValue = year.value as unknown as number;
let dateValue = new Date(yearValue, monthValue - 1, dayValue, 0, 0, 0, 0);
const instance = new BirthdayEntry({
birthDate: dateValue,
username: arg.user.username,
secName: arg.user.displayName,
active: true,
});
const created =
await this.birthdayEntryService.createOrUpdateBirthdayEntry(instance);
const quoteEmbed = new EmbedBuilder()
.setTitle('Your Birthday was added or updated! 🤓')
.setDescription('🎂')
.setFooter({
text: created?.secName ?? created.username,
})
.setTimestamp(created.createdAt);
await arg.editReply({ embeds: [quoteEmbed] });
return true;
}
}
41 changes: 41 additions & 0 deletions src/modules/command/commands/deactivate-birthday-shoutout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
CacheType,
CommandInteraction,
SlashCommandBuilder,
} from 'discord.js';
import { ACommand } from '../command.abstract';
import { Inject } from '@nestjs/common';
import { BirthdayEntryService } from '../../birthday/service/birthday-entry.service';
import { BirthdayEntry } from '../../../schemas/birthday-entry.schema';

export default class DeactivateBirthdayEntryShoutoutCommand extends ACommand {
constructor(
@Inject(BirthdayEntryService)
private readonly birthdayEntryService: BirthdayEntryService,
) {
super();
}
data = new SlashCommandBuilder()
.setName('deactivate-birthday-shoutout')
.setDescription(
'Use this if you don\'t want Bingus to shout you out on your birthday!',
);

async execute(arg: CommandInteraction<CacheType>): Promise<boolean> {
await arg.deferReply();
const instance = new BirthdayEntry({
username: arg.user.username,
secName: arg.user.displayName,
active: false
});
const inactive =
await this.birthdayEntryService.updateBirthdayEntry(instance);
if (!inactive) {
await arg.editReply({ content: 'You are already not receiving a birthday shoutout from Bingus! 🎉'});
} else {
await arg.editReply({ content: 'You will no longer receive a birthday shoutout from Bingus! 🎉'});
}
return true;
}
}

Loading
Loading