Merge branch 'master' into pr/LoboMetalurgico/424

This commit is contained in:
Flam3rboy committed 2021-10-10 12:50:45 +02:00
commit bffe2a85bd
48 files changed
+207 -379

No files matched your search

+2 -55
View File
@@ -1,19 +1,8 @@
import "reflect-metadata";
import {
BaseEntity,
BeforeInsert,
BeforeUpdate,
EntityMetadata,
FindConditions,
ObjectIdColumn,
PrimaryColumn,
} from "typeorm";
import { BaseEntity, EntityMetadata, FindConditions, ObjectIdColumn, PrimaryColumn } from "typeorm";
import { Snowflake } from "../util/Snowflake";
import "missing-native-js-functions";
// TODO use class-validator https://typeorm.io/#/validation with class annotators (isPhone/isEmail) combined with types from typescript-json-schema
// btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects
export class BaseClassWithoutId extends BaseEntity {
constructor(props?: any) {
super();
@@ -42,7 +31,7 @@ export class BaseClassWithoutId extends BaseEntity {
for (const key in props) {
if (!properties.has(key)) continue;
// @ts-ignore
const setter = this[`set${key.capitalize()}`];
const setter = this[`set${key.capitalize()}`]; // use setter function if it exists
if (setter) {
setter.call(this, props[key]);
@@ -53,12 +42,6 @@ export class BaseClassWithoutId extends BaseEntity {
}
}
@BeforeUpdate()
@BeforeInsert()
validate() {
return this;
}
toJSON(): any {
return Object.fromEntries(
this.metadata.columns // @ts-ignore
@@ -76,42 +59,6 @@ export class BaseClassWithoutId extends BaseEntity {
const repository = this.getRepository();
return repository.decrement(conditions, propertyPath, value);
}
// static async delete<T>(criteria: FindConditions<T>, options?: RemoveOptions) {
// if (!criteria) throw new Error("You need to specify delete criteria");
// const repository = this.getRepository();
// const promises = repository.metadata.relations.map(async (x) => {
// if (x.orphanedRowAction !== "delete") return;
// const foreignKey =
// x.foreignKeys.find((key) => key.entityMetadata === repository.metadata) ||
// x.inverseRelation?.foreignKeys[0]; // find foreign key for this entity
// if (!foreignKey) {
// throw new Error(
// `Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
// );
// }
// const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
// if (!id) throw new Error("id missing in criteria options " + foreignKey.referencedColumnNames);
// if (x.relationType === "many-to-many") {
// return getConnection()
// .createQueryBuilder()
// .relation(this, x.propertyName)
// .of(id)
// .remove({ [foreignKey.columnNames[0]]: id });
// } else if (
// x.relationType === "one-to-one" ||
// x.relationType === "many-to-one" ||
// x.relationType === "one-to-many"
// ) {
// return (x.inverseEntityMetadata.target as any).delete({ [foreignKey.columnNames[0]]: id });
// }
// });
// await Promise.all(promises);
// return super.delete(criteria, options);
// }
}
export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn;
-3
View File
@@ -46,9 +46,6 @@ export enum MessageType {
@Entity("messages")
export class Message extends BaseClass {
@Column()
id: string;
@Column({ nullable: true })
@RelationId((message: Message) => message.channel)
channel_id: string;
-3
View File
@@ -3,9 +3,6 @@ import { BaseClass } from "./BaseClass";
@Entity("rate_limits")
export class RateLimit extends BaseClass {
@Column()
id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498
@Column() // no relation as it also
executor_id: string;
+6 -3
View File
@@ -198,7 +198,7 @@ export class User extends BaseClass {
// randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists
// if it all five times already exists, abort with USERNAME_TOO_MANY_USERS error
// else just continue
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the database?
for (let tries = 0; tries < 5; tries++) {
discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
exists = await User.findOne({ where: { discriminator, username: username }, select: ["id"] });
@@ -219,7 +219,7 @@ export class User extends BaseClass {
// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
const language = req.language === "en" ? "en-US" : req.language || "en-US";
const user = await new User({
const user = new User({
created_at: new Date(),
username: username,
discriminator,
@@ -246,7 +246,10 @@ export class User extends BaseClass {
},
settings: { ...defaultSettings, locale: language },
fingerprints: [],
}).save();
});
console.log(user);
await user.save();
if (Config.get().guild.autoJoin.enabled) {
for (const guild of Config.get().guild.autoJoin.guilds || []) {
-3
View File
@@ -12,9 +12,6 @@ export enum WebhookType {
@Entity("webhooks")
export class Webhook extends BaseClass {
@Column()
id: string;
@Column({ type: "simple-enum", enum: WebhookType })
type: WebhookType;
-6
View File
@@ -1,12 +1,6 @@
import "reflect-metadata";
// export * as Constants from "../util/Constants";
export * from "./util/index";
export * from "./interfaces/index";
export * from "./entities/index";
export * from "./dtos/index";
// import Config from "../util/Config";
// import db, { MongooseCache, toObject } from "./util/Database";
// export { Config };
+10
View File
@@ -0,0 +1,10 @@
import { Server, traverseDirectory } from "lambert-server";
const DEFAULT_FILTER = /^([^\.].*)(?<!\.d)\.(js)$/;
export function registerRoutes(server: Server, root: string) {
return traverseDirectory(
{ dirname: root, recursive: true, filter: DEFAULT_FILTER },
server.registerRoute.bind(server, root)
);
}
+1
View File
@@ -17,3 +17,4 @@ export * from "./Rights";
export * from "./Snowflake";
export * from "./String";
export * from "./Array";
export * from "./TraverseDirectory";