Skip to content

Commit

Permalink
fix: types, imports, strings, remove full removal of sharedmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
NedcloarBR committed May 3, 2024
1 parent 7ad9d66 commit 5ede6cf
Show file tree
Hide file tree
Showing 61 changed files with 1,877 additions and 309 deletions.
2 changes: 1 addition & 1 deletion Database
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3.8"

include:
- path: ./Lavalink/docker-compose.yml
env_file: .env
# - path: ./Lavalink/docker-compose.yml
# env_file: .env
- path: ./Database/docker-compose.yml
env_file: .env
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3",
"@nestjs/event-emitter": "^2.0.4",
"@nestjs/platform-express": "^10.3.8",
"@prisma/client": "5.11.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand Down Expand Up @@ -76,5 +77,8 @@
"typescript": "^5.4.2",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.4.0"
},
"prisma": {
"schema": "Database/prisma/schema.prisma"
}
}
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { config } from "./modules/config";

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
cache: true,
load: [],
load: [config],
}),
],
})
Expand Down
30 changes: 6 additions & 24 deletions src/common/guards/CommandConfig.guard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import {
LOCALIZATION_ADAPTER,
type NestedLocalizationAdapter,
} from "@necord/localization";
import {
Inject,
Injectable,
type CanActivate,
type ExecutionContext,
} from "@nestjs/common";
import type { Reflector } from "@nestjs/core";
import { LOCALIZATION_ADAPTER, type NestedLocalizationAdapter } from "@necord/localization";
import { type CanActivate, type ExecutionContext, Inject, Injectable } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import type { ChatInputCommandInteraction } from "discord.js";
import { NecordExecutionContext } from "necord";
import { CommandConfig } from "../decorators";
Expand All @@ -20,24 +12,14 @@ export class CommandConfigGuard implements CanActivate {
private readonly reflector: Reflector,
) {}

public async canActivate(
executionContext: ExecutionContext,
): Promise<boolean> {
const commandConfig = this.reflector.get(
CommandConfig.KEY,
executionContext.getHandler(),
);
public async canActivate(executionContext: ExecutionContext): Promise<boolean> {
const commandConfig = this.reflector.get(CommandConfig.KEY, executionContext.getHandler());
const context = NecordExecutionContext.create(executionContext);
const args = context.getArgByIndex(0);
const interaction = args[0] as ChatInputCommandInteraction;

if (commandConfig.disable) {
interaction.reply(
this.translate.getTranslation(
"Tools.Command.Checker.Disable",
interaction.guildLocale,
),
);
interaction.reply(this.translate.getTranslation("Tools.Command.Checker.Disable", interaction.guildLocale));
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions src/common/guards/CommandPermissions.guard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CommandPermissions } from "@/common/decorators";
import type { Config } from "@/modules/shared/config/types";
import type { Config } from "@/modules/config/types";
import { formatArray } from "@/utils/Tools";
import { LOCALIZATION_ADAPTER, type NestedLocalizationAdapter } from "@necord/localization";
import { LOCALIZATION_ADAPTER, NestedLocalizationAdapter } from "@necord/localization";
import { type CanActivate, type ExecutionContext, Inject, Injectable } from "@nestjs/common";
import type { ConfigService } from "@nestjs/config";
import type { Reflector } from "@nestjs/core";
import { ConfigService } from "@nestjs/config";
import { Reflector } from "@nestjs/core";
import type { ChatInputCommandInteraction } from "discord.js";
import { NecordExecutionContext } from "necord";

Expand Down
3 changes: 1 addition & 2 deletions src/lib/sharding.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import path from "node:path";
import { Logger } from "@nestjs/common";
import type { ConfigService } from "@nestjs/config";
import { ShardingManager as _ShardingManager } from "discord.js";
import type { Config } from "../modules/shared/config/types";
import path from "node:path";

export class ShardingManager extends _ShardingManager {
public constructor(private readonly config: ConfigService) {
Expand Down
11 changes: 6 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Logger } from "@nestjs/common";
import { ShardingManager, TopGGAutoPoster } from "./lib";
import {NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { ShardingManager, TopGGAutoPoster } from "./lib";

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get<ConfigService>(ConfigService);
const app = await NestFactory.create(AppModule);
const configService = app.get<ConfigService>(ConfigService);
const logger = new Logger("Main");
const ShardManager = new ShardingManager(configService);
const TopGGPoster = new TopGGAutoPoster(configService.getOrThrow("TopGGToken"), ShardManager);
Expand All @@ -15,6 +15,7 @@ async function bootstrap() {
await ShardManager.init();
await TopGGPoster.init();
} catch (error) {
await app.listen(configService.get("PORT"));
logger.error("An error occurred when starting: ", (error as Error).message);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { inspect } from "node:util";
import { CommandConfig, CommandPermissions } from "@/common/decorators";
import { CommandConfigGuard, CommandPermissionsGuard } from "@/common/guards";
import { Config } from "@/modules/shared/config/types";
import { CurrentTranslate, TranslationFn, localizationMapByKey } from "@necord/localization";
import type { Config } from "@/modules/config/types";
import { CurrentTranslate, type TranslationFn, localizationMapByKey } from "@necord/localization";
import { UseGuards } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { EmbedBuilder, codeBlock } from "discord.js";
import { Ctx, Options, SlashCommandContext, Subcommand } from "necord";
import { Ctx, Options, type SlashCommandContext, Subcommand } from "necord";
import { DeveloperToolsCommand } from "../../DeveloperTools.decorator";
import { EvalDTO } from "./eval.dto";
import type { EvalDTO } from "./eval.dto";

@DeveloperToolsCommand()
export class EvalCommand {
Expand Down
112 changes: 112 additions & 0 deletions src/modules/config/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Config, ENVIRONMENT } from "./types";

export const config = (): Config => ({
ENVIRONMENT: process.env.ENVIRONMENT as ENVIRONMENT,
Database: {
Version: "Music",
URL: process.env.DATABASE_URL,
Name: process.env.DatabaseName,
Password: process.env.DatabasePassword,
Redis: {
Port: process.env.RedisPort,
Host: process.env.RedisHost,
},
},
API: {
JwtSecret: process.env.JWT_SECRET,
JwtExpire: "1d",
CookieSecret: process.env.COOKIE_SECRET,
MaxAge: 86400, // 1 day in seconds
},
TopGGToken: process.env.TopGGToken,
FallbackLocale: "pt-BR",
Discord: {
Token: process.env.Token,
DevToken: process.env.DevToken,
Client: {
Owners: ["330047048009252864"],
Secret: process.env.ClientSecret,
ID: "708822043420000366",
CallbackURL: process.env.CallbackURL,
},
Servers: {
NDCommunity: "679066351456878633",
TestGuild: "717094267243462688",
},
},
Debug: {
Client: true,
Translations: false,
Lavalink: false,
PremiumMusicPlayer: false,
},
Music: {
Lavalink: true,
Volumes: {
Lavalink: 0.75,
Player: 50,
},
Player: {
AutoLeaveEmpty: {
Channel: {
Enable: true,
Delay: 60000,
},
Queue: {
Enable: true,
Delay: 30000,
},
},
},
Client: {
selfDeaf: true,
serverDeaf: true,
},
},
Emojis: {
logo: "<:NDB:763741625079300117>",
fail: "<:NotixDeny:719560576015138830>",
accept: "<:NotixAllow:719560623960096789>",
success: "<:NotixAllow:719560623960096789>",
thing: "<a:OPensador:718195925327151134>",
loading: "<a:Carregando:718196232757182566>",
loading2: "<a:Carregando2:718196278646800424>",
delayping: "<:DelayPing:718196166399098901>",
Music: {
Youtube: "<:youtube:730741995416453150>",
Spotify: "<:Spotify:775154334832001044>",
SoundCloud: "<:soundcloud:932065538014842950>",
Deezer: "<:deezer:932065971336802334>",
Facebook: "<:facebook:932066080996864070>",
Apple: "<:Apple:852677662983716884>",
Twitch: "<:twitch:998725252098052197>",
},
},
URLList: {
Music: {
Youtube: "https://www.youtube.com",
ShortYoutube: "https://youtu.be",
SoundCloud: "https://soundcloud.com",
Spotify: "https://open.spotify.com",
Deezer: "https://www.deezer",
Facebook: "https://facebook.com",
Apple: "https://music.apple.com/",
Twitch: "https://www.twitch.tv/",
},
},
EvalBadKeys: [
"client.token",
"client.destroy",
"process.env.Token",
"process.env.DevToken",
"process.env.DATABASE_URL",
"process.env.DatabaseName",
"process.env.DatabasePassword",
"process.env.RedisPort",
"process.env.RedisHost",
"process.env.LavalinkHOST",
"process.env.LavalinkPassword",
"process.env.SpotifyClientId",
"process.env.SpotifyClientSecret",
],
});
35 changes: 35 additions & 0 deletions src/modules/config/JSONLocale.loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from "node:fs";
import path from "node:path";

export class JSONLocaleLoader {
public constructor(private readonly path: string) {}

public async loadTranslations() {
const locales: Record<string, Record<string, string>> = {};
const folders = fs.readdirSync(this.path);

for (const langFolder of folders) {
const langPath = path.join(this.path, langFolder);
const namespaces = fs.readdirSync(langPath);
const langData = {};

for (const namespace of namespaces) {
const namespacePath = path.join(langPath, namespace);
const files = fs.readdirSync(namespacePath);
const namespaceData = {};

for (const file of files) {
const filePath = path.join(namespacePath, file);
const jsonContent = fs.readFileSync(filePath, "utf-8");
const jsonData = JSON.parse(jsonContent);
namespaceData[file.replace(".json", "")] = jsonData;
}

langData[namespace] = namespaceData;
}

locales[langFolder] = langData;
}
return locales;
}
}
60 changes: 60 additions & 0 deletions src/modules/config/NecordConfig.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Injectable } from "@nestjs/common";
import type { ConfigService } from "@nestjs/config";
import { GatewayIntentBits, GatewayVersion, Options, Partials } from "discord.js";
import type { NecordModuleOptions } from "necord";
import { Config } from "./types";

@Injectable()
export class NecordConfigService {
public constructor(private readonly config: ConfigService) {}

createNecordOptions(): NecordModuleOptions {
return {
token: this.config.getOrThrow<Config["Discord"]>("Discord").Token,
skipRegistration: false,
shards: "auto",
rest: {
version: GatewayVersion,
offset: 0,
api: "https://discord.com/api/",
cdn: "https://cdn.discordapp.com",
},
failIfNotExists: true,
allowedMentions: {
parse: ["roles", "users"],
repliedUser: false,
},
makeCache: Options.cacheEverything(),
partials: [
Partials.Channel,
Partials.GuildMember,
Partials.GuildScheduledEvent,
Partials.Message,
Partials.Reaction,
Partials.ThreadMember,
Partials.User,
],
intents: [
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.DirectMessageTyping,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildModeration,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildIntegrations,
GatewayIntentBits.GuildInvites,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildPresences,
GatewayIntentBits.GuildScheduledEvents,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildWebhooks,
GatewayIntentBits.Guilds,
GatewayIntentBits.MessageContent,
GatewayIntentBits.AutoModerationConfiguration,
GatewayIntentBits.AutoModerationExecution,
],
} as never;
}
}
3 changes: 3 additions & 0 deletions src/modules/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./Config";
export * from "./JSONLocale.loader";
export * from "./NecordConfig.service";
Loading

0 comments on commit 5ede6cf

Please sign in to comment.