generated from SocketSomeone/nestjs-open-source-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
c89864c
commit b8f3273
Showing
11 changed files
with
190 additions
and
10 deletions.
There are no files selected for viewing
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,80 @@ | ||
import { Injectable, OnModuleInit } from '@nestjs/common'; | ||
import { ChangelogService } from './changelog.service'; | ||
import { Context, SlashCommand, SlashCommandContext } from 'necord'; | ||
import { NecordPaginationService, PageBuilder } from '@necord/pagination'; | ||
import { map, switchMap, tap, timer } from 'rxjs'; | ||
import { EmbedBuilder, time } from 'discord.js'; | ||
|
||
@Injectable() | ||
export class ChangelogController implements OnModuleInit { | ||
public constructor( | ||
private readonly changelogService: ChangelogService, | ||
private readonly paginationService: NecordPaginationService | ||
) {} | ||
|
||
public onModuleInit() { | ||
return timer(0, 1000 * 60 * 60) | ||
.pipe( | ||
switchMap(() => this.changelogService.changelog), | ||
tap(changelog => | ||
this.paginationService.register(builder => | ||
builder | ||
.setCustomId('changelog') | ||
.setMaxPages(changelog.length) | ||
.setPages( | ||
changelog.map(release => | ||
new PageBuilder().addEmbed( | ||
new EmbedBuilder() | ||
.setAuthor({ | ||
name: release.author.login, | ||
url: release.author.url, | ||
iconURL: release.author.avatar_url | ||
}) | ||
.setTitle(`Changelog`) | ||
.setURL(release.url) | ||
.setDescription( | ||
release.body | ||
.slice(0, 4096) | ||
.replace('\r\n\r\n', '\n') | ||
) | ||
.setColor('Blurple') | ||
.setFields([ | ||
{ | ||
name: 'Created At', | ||
value: `${time( | ||
new Date(release.created_at) | ||
)} (${time( | ||
new Date(release.created_at), | ||
'R' | ||
)})`, | ||
inline: true | ||
}, | ||
{ | ||
name: 'Published At', | ||
value: `${time( | ||
new Date(release.published_at) | ||
)} (${time( | ||
new Date(release.published_at), | ||
'R' | ||
)})`, | ||
inline: true | ||
} | ||
]) | ||
.setFooter({ text: `Release ${release.tag_name}` }) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
) | ||
.subscribe(); | ||
} | ||
|
||
@SlashCommand({ name: 'changelog', description: 'Get the latest changelog' }) | ||
public async changelog(@Context() [interaction]: SlashCommandContext) { | ||
const pageBuilder = this.paginationService.get('changelog'); | ||
const pageOptions = await pageBuilder.build(); | ||
|
||
return interaction.reply(pageOptions); | ||
} | ||
} |
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,42 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { NecordPaginationModule } from '@necord/pagination'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { ChangelogService } from './changelog.service'; | ||
import { ChangelogController } from './changelog.controller'; | ||
import { ButtonStyle } from 'discord.js'; | ||
|
||
@Module({ | ||
imports: [ | ||
HttpModule.register({ | ||
baseURL: 'https://api.github.com' | ||
}), | ||
NecordPaginationModule.forRoot({ | ||
buttons: { | ||
first: { | ||
label: ' ', | ||
style: ButtonStyle.Primary, | ||
emoji: '⏮️' | ||
}, | ||
back: { | ||
label: ' ', | ||
style: ButtonStyle.Primary, | ||
emoji: '⬅️' | ||
}, | ||
next: { | ||
label: ' ', | ||
style: ButtonStyle.Primary, | ||
emoji: '➡️' | ||
}, | ||
last: { | ||
label: ' ', | ||
style: ButtonStyle.Primary, | ||
emoji: '⏭️' | ||
} | ||
}, | ||
allowSkip: true, | ||
allowTraversal: false | ||
}) | ||
], | ||
providers: [ChangelogController, ChangelogService] | ||
}) | ||
export class ChangelogModule {} |
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,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { HttpService } from '@nestjs/axios'; | ||
import { map } from 'rxjs'; | ||
import { Release } from './interfaces'; | ||
import { SharedReplayRefresh } from './helpers'; | ||
|
||
@Injectable() | ||
export class ChangelogService { | ||
private readonly dataSource = new SharedReplayRefresh<Release[]>(); | ||
|
||
private readonly changelog$ = this.httpService | ||
.get<Release[]>('repos/necordjs/necord/releases') | ||
.pipe(map(res => res.data)); | ||
|
||
public constructor(private readonly httpService: HttpService) {} | ||
|
||
public get changelog() { | ||
return this.dataSource.sharedReplayTimerRefresh(this.changelog$, 1, 1000 * 60 * 60); | ||
} | ||
} |
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 @@ | ||
export * from './shared-replay-refresh.helper'; |
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,21 @@ | ||
import { Observable, SchedulerLike, shareReplay } from 'rxjs'; | ||
|
||
export class SharedReplayRefresh<T> { | ||
private sharedReplay$: Observable<T>; | ||
private subscriptionTime: number; | ||
|
||
sharedReplayTimerRefresh( | ||
source: Observable<T>, | ||
bufferSize = 1, | ||
windowTime = 3000000, | ||
scheduler?: SchedulerLike | ||
): Observable<T> { | ||
const currentTime = new Date().getTime(); | ||
if (!this.sharedReplay$ || currentTime - this.subscriptionTime > windowTime) { | ||
this.sharedReplay$ = source.pipe(shareReplay(bufferSize, windowTime, scheduler)); | ||
this.subscriptionTime = currentTime; | ||
} | ||
|
||
return this.sharedReplay$; | ||
} | ||
} |
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 @@ | ||
export * from './release.interface'; |
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,15 @@ | ||
interface ReleaseAuthor { | ||
login: string; | ||
url: string; | ||
avatar_url: string; | ||
} | ||
|
||
export interface Release { | ||
tag_name: string; | ||
name: string; | ||
body: string; | ||
url: string; | ||
author: ReleaseAuthor; | ||
created_at: string; | ||
published_at: string; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.