-
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.
- Loading branch information
Showing
11 changed files
with
155 additions
and
13 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Client, TextChannel } from 'discord.js' | ||
import { ITask } from './tasks/interfaces/task.interface' | ||
import BirthdayShoutoutTask from './tasks/birthday-shoutout.task' | ||
import * as cron from 'node-cron' | ||
import { Inject } from '@nestjs/common' | ||
import { BirthdayEntryService } from '../birthday/service/birthday-entry.service' | ||
|
||
|
||
interface TaskEntry { | ||
name: string | ||
schedule: string | ||
task: ITask | ||
} | ||
|
||
class CronScheduler { | ||
private static instance: CronScheduler | ||
private client: Client | ||
private tasks: TaskEntry[] | ||
|
||
constructor(client: Client, @Inject(BirthdayEntryService) private readonly birthdayService?: BirthdayEntryService) { | ||
if (CronScheduler.instance) { | ||
throw new Error(`ERROR: An instance has already been created.`) | ||
} | ||
this.client = client | ||
this.tasks = [ | ||
{ | ||
name: 'birthday-shoutout', | ||
schedule: '0 10 * * *', | ||
task: new BirthdayShoutoutTask( | ||
this.client.channels.cache.find((channel) => channel.id === '447554141724737548') as TextChannel,birthdayService | ||
), | ||
}, | ||
] | ||
|
||
CronScheduler.instance = this | ||
} | ||
|
||
static getInstance(): CronScheduler { | ||
return CronScheduler.instance | ||
} | ||
|
||
registerTasks(): void { | ||
this.tasks.forEach((task) => { | ||
if (!cron.validate(task.schedule)) { | ||
throw new Error(`ERROR: Invalid cron schedule expression for task '${task.name}'`) | ||
} | ||
|
||
cron.schedule(task.schedule, async () => { | ||
await task.task.execute() | ||
}).start() | ||
}) | ||
} | ||
} | ||
|
||
export { CronScheduler } |
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,14 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { BirthdayEntryModule } from "../birthday/module/birthday-entry.module"; | ||
import BirthdayShoutoutTask from "./tasks/birthday-shoutout.task"; | ||
import { CronScheduler } from "./cron-scheduler"; | ||
|
||
|
||
@Module({ | ||
imports: [ | ||
BirthdayEntryModule, | ||
], | ||
providers: [], | ||
exports: [], | ||
}) | ||
export class TaskModule {} |
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,35 @@ | ||
import { EmbedBuilder, TextChannel } from 'discord.js' | ||
import { ITask } from './interfaces/task.interface' | ||
import { BirthdayEntryService } from '../../birthday/service/birthday-entry.service' | ||
|
||
export default class BirthdayShoutoutTask implements ITask { | ||
private channel: TextChannel | ||
|
||
constructor(channel: TextChannel, private readonly birthdayService: BirthdayEntryService) { | ||
this.channel = channel | ||
} | ||
|
||
async execute(): Promise<void> { | ||
let birthDayEntries = await this.birthdayService.getEntryForToday() | ||
|
||
if (!birthDayEntries || birthDayEntries.length < 1) { | ||
return | ||
} | ||
|
||
birthDayEntries.forEach((entry) => { | ||
let creator = this.channel.members.find((member) => member.user.username === entry.username || member.user.displayName === entry.secName) | ||
|
||
let embed = new EmbedBuilder() | ||
.setTitle(`🚨 Birthday Alert!! 🚨`) | ||
.setColor('Random') | ||
.setDescription(`${entry.username ?? entry.secName} is turning **${new Date().getFullYear() - entry.birthDate.getFullYear()}** years old today! 🎉🎂🎈`) | ||
.setFooter({ | ||
text: creator?.user.username ?? 'bingus', | ||
iconURL: creator?.displayAvatarURL() ?? undefined, | ||
}) | ||
.setTimestamp(new Date()) | ||
|
||
this.channel.send({ embeds: [embed] }) | ||
}) | ||
} | ||
} |
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,6 @@ | ||
|
||
interface ITask { | ||
execute(...args: any): Promise<void> | ||
} | ||
|
||
export type { ITask } |
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