-
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
35 changed files
with
275 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
TOKEN="" | ||
DATABASE_URL="" | ||
# DISCORD BOT | ||
TOKEN='' | ||
CLIENT_ID='' | ||
|
||
# API/DISCORD OAUTH | ||
OAUTH_SECRET='' | ||
OAUTH_REDIRECT='' | ||
OAUTH_COOKIE='' | ||
API_PORT='3000' | ||
API_ORIGIN='' | ||
API_PREFIX='/api/v1' | ||
|
||
# DATABASE | ||
DATABASE_URL='postgresql://user:pass@host:port/dbname' |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.env | ||
.env.development | ||
node_modules | ||
dist | ||
|
||
|
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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,48 @@ | ||
import { createFunctionPrecondition } from '@sapphire/decorators'; | ||
import { RateLimitManager } from '@sapphire/ratelimits'; | ||
import { | ||
HttpCodes, | ||
type ApiRequest, | ||
type ApiResponse, | ||
} from '@sapphire/plugin-api'; | ||
|
||
export const authenticated = () => | ||
createFunctionPrecondition( | ||
(request: ApiRequest) => Boolean(request.auth?.token), | ||
(_request: ApiRequest, response: ApiResponse) => | ||
response.error(HttpCodes.Unauthorized), | ||
); | ||
|
||
export function rateLimit( | ||
time: number, | ||
limit: number = 1, | ||
auth: boolean = false, | ||
) { | ||
const manager = new RateLimitManager(time, limit); | ||
return createFunctionPrecondition( | ||
(request: ApiRequest, response: ApiResponse) => { | ||
const id = ( | ||
auth | ||
? request.auth!.id | ||
: request.headers['x-api-key'] || request.socket.remoteAddress | ||
) as string; | ||
const bucket = manager.acquire(id); | ||
|
||
response.setHeader('Date', new Date().toUTCString()); | ||
response.setHeader('X-RateLimit-Limit', time); | ||
response.setHeader('X-RateLimit-Remaining', bucket.remaining.toString()); | ||
response.setHeader('X-RateLimit-Reset', bucket.remainingTime.toString()); | ||
|
||
if (bucket.limited) { | ||
response.setHeader('Retry-After', bucket.remainingTime.toString()); | ||
return true; | ||
} | ||
|
||
try { | ||
bucket.consume(); | ||
} catch {} | ||
|
||
return false; | ||
}, | ||
); | ||
} |
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 './decorators'; |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ModerationManager } from '@lib/classes'; | ||
import type { PrismaClient } from '@prisma/client'; | ||
import type { ArrayString, NumberString } from '@skyra/env-utilities'; | ||
|
||
declare module '@sapphire/pieces' { | ||
interface Container { | ||
prisma: PrismaClient; | ||
moderationManager: ModerationManager; | ||
} | ||
} | ||
|
||
declare module '@skyra/env-utilities' { | ||
interface Env { | ||
TOKEN: string; | ||
CLIENT_ID: string; | ||
CLIENT_SECRET: string; | ||
OAUTH_REDIRECT: string; | ||
OAUTH_SCOPE: ArrayString; | ||
OAUTH_COOKIE: string; | ||
API_PORT: NumberString; | ||
API_ORIGIN: string; | ||
API_PREFIX: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ModerationType } from '@prisma/client'; | ||
|
||
export type APIPetResponse = Array<APIPetInterface>; | ||
|
||
export interface APIPetInterface { | ||
url: string; | ||
message?: string; | ||
} | ||
|
||
export const ModerationTypeNames = { | ||
[ModerationType.BAN]: 'Banned', | ||
[ModerationType.KICK]: 'Kicked', | ||
[ModerationType.MUTE]: 'Muted', | ||
[ModerationType.WARN]: 'Warned', | ||
}; | ||
|
||
export const ModerationTypeStrings = { | ||
[ModerationType.BAN]: 'Ban', | ||
[ModerationType.KICK]: 'Kick', | ||
[ModerationType.MUTE]: 'Mute', | ||
[ModerationType.WARN]: 'Warn', | ||
}; |
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 |
---|---|---|
@@ -1,23 +1 @@ | ||
import type { ModerationManager } from '@lib/classes'; | ||
import { type PrismaClient, ModerationType } from '@prisma/client'; | ||
|
||
export type APIPetResponse = Array<APIPetInterface>; | ||
|
||
export interface APIPetInterface { | ||
url: string; | ||
message?: string; | ||
} | ||
|
||
export const ModerationTypeNames = { | ||
[ModerationType.BAN]: 'Banned', | ||
[ModerationType.KICK]: 'Kicked', | ||
[ModerationType.MUTE]: 'Muted', | ||
[ModerationType.WARN]: 'Warned', | ||
}; | ||
|
||
declare module '@sapphire/pieces' { | ||
interface Container { | ||
prisma: PrismaClient; | ||
moderationManager: ModerationManager; | ||
} | ||
} | ||
export * from './Util'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,23 @@ | ||
import { rateLimit } from '@lib/api'; | ||
import { ApplyOptions } from '@sapphire/decorators'; | ||
import { | ||
methods, | ||
Route, | ||
type ApiRequest, | ||
type ApiResponse, | ||
} from '@sapphire/plugin-api'; | ||
|
||
@ApplyOptions<Route.Options>({ | ||
route: 'hello-world', | ||
}) | ||
export class HelloWorldRoute extends Route { | ||
@rateLimit(5000) | ||
public [methods.GET](_request: ApiRequest, response: ApiResponse) { | ||
response.json({ message: 'Hello World' }); | ||
} | ||
|
||
@rateLimit(5000) | ||
public [methods.POST](_request: ApiRequest, response: ApiResponse) { | ||
response.json({ message: 'Hello World' }); | ||
} | ||
} |
Oops, something went wrong.