-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from Blvckleg/birthdaylist-and-automated-shout…
…outs-with-cron feature |Birthdaylist and automated shoutouts with cron
- Loading branch information
Showing
20 changed files
with
435 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/modules/command/commands/activate-birthday-shoutout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/modules/command/commands/deactivate-birthday-shoutout.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
Oops, something went wrong.