✨ util
This commit is contained in:
+121
-13785
File diff suppressed because it is too large
Load Diff
+4
-2
@@ -1,8 +1,10 @@
|
||||
import "reflect-metadata";
|
||||
|
||||
// export * as Constants from "../util/Constants";
|
||||
export * from "./models/index";
|
||||
// export * from "../util/index";
|
||||
export * from "./interfaces/index";
|
||||
export * from "./entities/index";
|
||||
export * from "./util/index";
|
||||
import "./test";
|
||||
|
||||
// import Config from "../util/Config";
|
||||
// import db, { MongooseCache, toObject } from "./util/Database";
|
||||
|
||||
@@ -1,14 +1,3 @@
|
||||
import { User } from "./User";
|
||||
import { ClientStatus, Status } from "./Status";
|
||||
|
||||
export interface Presence {
|
||||
user: User;
|
||||
guild_id?: string;
|
||||
status: Status;
|
||||
activities: Activity[];
|
||||
client_status: ClientStatus;
|
||||
}
|
||||
|
||||
export interface Activity {
|
||||
name: string;
|
||||
type: ActivityType;
|
||||
@@ -0,0 +1,526 @@
|
||||
import { PublicUser, User, UserSettings } from "../entities/User";
|
||||
import { Channel } from "../entities/Channel";
|
||||
import { Guild } from "../entities/Guild";
|
||||
import { Member, PublicMember, UserGuildSettings } from "../entities/Member";
|
||||
import { Emoji } from "../entities/Emoji";
|
||||
import { Role } from "../entities/Role";
|
||||
import { Invite } from "../entities/Invite";
|
||||
import { Message, PartialEmoji } from "../entities/Message";
|
||||
import { VoiceState } from "../entities/VoiceState";
|
||||
import { ApplicationCommand } from "../entities/Application";
|
||||
import { Interaction } from "./Interaction";
|
||||
import { ConnectedAccount } from "../entities/ConnectedAccount";
|
||||
import { Relationship } from "../entities/Relationship";
|
||||
import { Presence } from "./Presence";
|
||||
|
||||
export interface Event {
|
||||
guild_id?: string;
|
||||
user_id?: string;
|
||||
channel_id?: string;
|
||||
created_at?: Date;
|
||||
event: EVENT;
|
||||
data?: any;
|
||||
}
|
||||
|
||||
// ! Custom Events that shouldn't get sent to the client but processed by the server
|
||||
|
||||
export interface InvalidatedEvent extends Event {
|
||||
event: "INVALIDATED";
|
||||
}
|
||||
|
||||
// ! END Custom Events that shouldn't get sent to the client but processed by the server
|
||||
|
||||
export interface ReadyEventData {
|
||||
v: number;
|
||||
user: PublicUser & {
|
||||
mobile: boolean;
|
||||
desktop: boolean;
|
||||
email: string | null;
|
||||
flags: bigint;
|
||||
mfa_enabled: boolean;
|
||||
nsfw_allowed: boolean;
|
||||
phone: string | null;
|
||||
premium: boolean;
|
||||
premium_type: number;
|
||||
verified: boolean;
|
||||
bot: boolean;
|
||||
};
|
||||
private_channels: Channel[]; // this will be empty for bots
|
||||
session_id: string; // resuming
|
||||
guilds: Guild[];
|
||||
analytics_token?: string;
|
||||
connected_accounts?: ConnectedAccount[];
|
||||
consents?: {
|
||||
personalization?: {
|
||||
consented?: boolean;
|
||||
};
|
||||
};
|
||||
country_code?: string; // e.g. DE
|
||||
friend_suggestion_count?: number;
|
||||
geo_ordered_rtc_regions?: string[]; // ["europe","russie","india","us-east","us-central"]
|
||||
experiments?: [number, number, number, number, number][];
|
||||
guild_experiments?: [
|
||||
// ? what are guild_experiments?
|
||||
// this is the structure of it:
|
||||
number,
|
||||
null,
|
||||
number,
|
||||
[[number, { e: number; s: number }[]]],
|
||||
[number, [[number, [number, number]]]],
|
||||
{ b: number; k: bigint[] }[]
|
||||
][];
|
||||
guild_join_requests?: any[]; // ? what is this? this is new
|
||||
shard?: [number, number];
|
||||
user_settings?: UserSettings;
|
||||
relationships?: Relationship[]; // TODO
|
||||
read_state: {
|
||||
entries: any[]; // TODO
|
||||
partial: boolean;
|
||||
version: number;
|
||||
};
|
||||
user_guild_settings?: {
|
||||
entries: UserGuildSettings[];
|
||||
version: number;
|
||||
partial: boolean;
|
||||
};
|
||||
application?: {
|
||||
id: string;
|
||||
flags: bigint;
|
||||
};
|
||||
merged_members?: Omit<Member, "settings" | "user">[][];
|
||||
// probably all users who the user is in contact with
|
||||
users?: {
|
||||
avatar: string | null;
|
||||
discriminator: string;
|
||||
id: string;
|
||||
username: string;
|
||||
bot: boolean;
|
||||
public_flags: bigint;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface ReadyEvent extends Event {
|
||||
event: "READY";
|
||||
data: ReadyEventData;
|
||||
}
|
||||
|
||||
export interface ChannelCreateEvent extends Event {
|
||||
event: "CHANNEL_CREATE";
|
||||
data: Channel;
|
||||
}
|
||||
|
||||
export interface ChannelUpdateEvent extends Event {
|
||||
event: "CHANNEL_UPDATE";
|
||||
data: Channel;
|
||||
}
|
||||
|
||||
export interface ChannelDeleteEvent extends Event {
|
||||
event: "CHANNEL_DELETE";
|
||||
data: Channel;
|
||||
}
|
||||
|
||||
export interface ChannelPinsUpdateEvent extends Event {
|
||||
event: "CHANNEL_PINS_UPDATE";
|
||||
data: {
|
||||
guild_id?: string;
|
||||
channel_id: string;
|
||||
last_pin_timestamp?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildCreateEvent extends Event {
|
||||
event: "GUILD_CREATE";
|
||||
data: Guild;
|
||||
}
|
||||
|
||||
export interface GuildUpdateEvent extends Event {
|
||||
event: "GUILD_UPDATE";
|
||||
data: Guild;
|
||||
}
|
||||
|
||||
export interface GuildDeleteEvent extends Event {
|
||||
event: "GUILD_DELETE";
|
||||
data: {
|
||||
id: string;
|
||||
unavailable?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildBanAddEvent extends Event {
|
||||
event: "GUILD_BAN_ADD";
|
||||
data: {
|
||||
guild_id: string;
|
||||
user: User;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildBanRemoveEvent extends Event {
|
||||
event: "GUILD_BAN_REMOVE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
user: User;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildEmojiUpdateEvent extends Event {
|
||||
event: "GUILD_EMOJI_UPDATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
emojis: Emoji[];
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildIntegrationUpdateEvent extends Event {
|
||||
event: "GUILD_INTEGRATIONS_UPDATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildMemberAddEvent extends Event {
|
||||
event: "GUILD_MEMBER_ADD";
|
||||
data: PublicMember & {
|
||||
guild_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildMemberRemoveEvent extends Event {
|
||||
event: "GUILD_MEMBER_REMOVE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
user: User;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildMemberUpdateEvent extends Event {
|
||||
event: "GUILD_MEMBER_UPDATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
roles: string[];
|
||||
user: User;
|
||||
nick?: string;
|
||||
joined_at?: Date;
|
||||
premium_since?: number;
|
||||
pending?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildMembersChunkEvent extends Event {
|
||||
event: "GUILD_MEMBERS_CHUNK";
|
||||
data: {
|
||||
guild_id: string;
|
||||
members: PublicMember[];
|
||||
chunk_index: number;
|
||||
chunk_count: number;
|
||||
not_found: string[];
|
||||
presences: Presence[];
|
||||
nonce?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildRoleCreateEvent extends Event {
|
||||
event: "GUILD_ROLE_CREATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
role: Role;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildRoleUpdateEvent extends Event {
|
||||
event: "GUILD_ROLE_UPDATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
role: Role;
|
||||
};
|
||||
}
|
||||
|
||||
export interface GuildRoleDeleteEvent extends Event {
|
||||
event: "GUILD_ROLE_DELETE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
role_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface InviteCreateEvent extends Event {
|
||||
event: "INVITE_CREATE";
|
||||
data: Omit<Invite, "guild" | "channel"> & {
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface InviteDeleteEvent extends Event {
|
||||
event: "INVITE_DELETE";
|
||||
data: {
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
code: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type MessagePayload = Omit<Message, "author_id"> & {
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
author: PublicUser;
|
||||
member: PublicMember;
|
||||
mentions: (PublicUser & { member: PublicMember })[];
|
||||
};
|
||||
|
||||
export interface MessageCreateEvent extends Event {
|
||||
event: "MESSAGE_CREATE";
|
||||
data: MessagePayload;
|
||||
}
|
||||
|
||||
export interface MessageUpdateEvent extends Event {
|
||||
event: "MESSAGE_UPDATE";
|
||||
data: MessagePayload;
|
||||
}
|
||||
|
||||
export interface MessageDeleteEvent extends Event {
|
||||
event: "MESSAGE_DELETE";
|
||||
data: {
|
||||
id: string;
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessageDeleteBulkEvent extends Event {
|
||||
event: "MESSAGE_DELETE_BULK";
|
||||
data: {
|
||||
ids: string[];
|
||||
channel_id: string;
|
||||
guild_id?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessageReactionAddEvent extends Event {
|
||||
event: "MESSAGE_REACTION_ADD";
|
||||
data: {
|
||||
user_id: string;
|
||||
channel_id: string;
|
||||
message_id: string;
|
||||
guild_id?: string;
|
||||
member?: PublicMember;
|
||||
emoji: PartialEmoji;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessageReactionRemoveEvent extends Event {
|
||||
event: "MESSAGE_REACTION_REMOVE";
|
||||
data: {
|
||||
user_id: string;
|
||||
channel_id: string;
|
||||
message_id: string;
|
||||
guild_id?: string;
|
||||
emoji: PartialEmoji;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessageReactionRemoveAllEvent extends Event {
|
||||
event: "MESSAGE_REACTION_REMOVE_ALL";
|
||||
data: {
|
||||
channel_id: string;
|
||||
message_id: string;
|
||||
guild_id?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface MessageReactionRemoveEmojiEvent extends Event {
|
||||
event: "MESSAGE_REACTION_REMOVE_EMOJI";
|
||||
data: {
|
||||
channel_id: string;
|
||||
message_id: string;
|
||||
guild_id?: string;
|
||||
emoji: PartialEmoji;
|
||||
};
|
||||
}
|
||||
|
||||
export interface PresenceUpdateEvent extends Event {
|
||||
event: "PRESENCE_UPDATE";
|
||||
data: Presence;
|
||||
}
|
||||
|
||||
export interface TypingStartEvent extends Event {
|
||||
event: "TYPING_START";
|
||||
data: {
|
||||
channel_id: string;
|
||||
user_id: string;
|
||||
timestamp: number;
|
||||
guild_id?: string;
|
||||
member?: PublicMember;
|
||||
};
|
||||
}
|
||||
|
||||
export interface UserUpdateEvent extends Event {
|
||||
event: "USER_UPDATE";
|
||||
data: User;
|
||||
}
|
||||
|
||||
export interface VoiceStateUpdateEvent extends Event {
|
||||
event: "VOICE_STATE_UPDATE";
|
||||
data: VoiceState & {
|
||||
member: PublicMember;
|
||||
};
|
||||
}
|
||||
|
||||
export interface VoiceServerUpdateEvent extends Event {
|
||||
event: "VOICE_SERVER_UPDATE";
|
||||
data: {
|
||||
token: string;
|
||||
guild_id: string;
|
||||
endpoint: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface WebhooksUpdateEvent extends Event {
|
||||
event: "WEBHOOKS_UPDATE";
|
||||
data: {
|
||||
guild_id: string;
|
||||
channel_id: string;
|
||||
};
|
||||
}
|
||||
|
||||
export type ApplicationCommandPayload = ApplicationCommand & {
|
||||
guild_id: string;
|
||||
};
|
||||
|
||||
export interface ApplicationCommandCreateEvent extends Event {
|
||||
event: "APPLICATION_COMMAND_CREATE";
|
||||
data: ApplicationCommandPayload;
|
||||
}
|
||||
|
||||
export interface ApplicationCommandUpdateEvent extends Event {
|
||||
event: "APPLICATION_COMMAND_UPDATE";
|
||||
data: ApplicationCommandPayload;
|
||||
}
|
||||
|
||||
export interface ApplicationCommandDeleteEvent extends Event {
|
||||
event: "APPLICATION_COMMAND_DELETE";
|
||||
data: ApplicationCommandPayload;
|
||||
}
|
||||
|
||||
export interface InteractionCreateEvent extends Event {
|
||||
event: "INTERACTION_CREATE";
|
||||
data: Interaction;
|
||||
}
|
||||
|
||||
export interface MessageAckEvent extends Event {
|
||||
event: "MESSAGE_ACK";
|
||||
data: {
|
||||
channel_id: string;
|
||||
message_id: string;
|
||||
version?: number;
|
||||
manual?: boolean;
|
||||
mention_count?: number;
|
||||
};
|
||||
}
|
||||
|
||||
export interface RelationshipAddEvent extends Event {
|
||||
event: "RELATIONSHIP_ADD";
|
||||
data: Relationship & {
|
||||
should_notify?: boolean;
|
||||
user: PublicUser;
|
||||
};
|
||||
}
|
||||
|
||||
export interface RelationshipRemoveEvent extends Event {
|
||||
event: "RELATIONSHIP_REMOVE";
|
||||
data: Omit<Relationship, "nickname">;
|
||||
}
|
||||
|
||||
// located in collection events
|
||||
|
||||
export enum EVENTEnum {
|
||||
Ready = "READY",
|
||||
ChannelCreate = "CHANNEL_CREATE",
|
||||
ChannelUpdate = "CHANNEL_UPDATE",
|
||||
ChannelDelete = "CHANNEL_DELETE",
|
||||
ChannelPinsUpdate = "CHANNEL_PINS_UPDATE",
|
||||
GuildCreate = "GUILD_CREATE",
|
||||
GuildUpdate = "GUILD_UPDATE",
|
||||
GuildDelete = "GUILD_DELETE",
|
||||
GuildBanAdd = "GUILD_BAN_ADD",
|
||||
GuildBanRemove = "GUILD_BAN_REMOVE",
|
||||
GuildEmojUpdate = "GUILD_EMOJI_UPDATE",
|
||||
GuildIntegrationsUpdate = "GUILD_INTEGRATIONS_UPDATE",
|
||||
GuildMemberAdd = "GUILD_MEMBER_ADD",
|
||||
GuildMemberRempve = "GUILD_MEMBER_REMOVE",
|
||||
GuildMemberUpdate = "GUILD_MEMBER_UPDATE",
|
||||
GuildMemberSpeaking = "GUILD_MEMBER_SPEAKING",
|
||||
GuildMembersChunk = "GUILD_MEMBERS_CHUNK",
|
||||
GuildRoleCreate = "GUILD_ROLE_CREATE",
|
||||
GuildRoleDelete = "GUILD_ROLE_DELETE",
|
||||
GuildRoleUpdate = "GUILD_ROLE_UPDATE",
|
||||
InviteCreate = "INVITE_CREATE",
|
||||
InviteDelete = "INVITE_DELETE",
|
||||
MessageCreate = "MESSAGE_CREATE",
|
||||
MessageUpdate = "MESSAGE_UPDATE",
|
||||
MessageDelete = "MESSAGE_DELETE",
|
||||
MessageDeleteBulk = "MESSAGE_DELETE_BULK",
|
||||
MessageReactionAdd = "MESSAGE_REACTION_ADD",
|
||||
MessageReactionRemove = "MESSAGE_REACTION_REMOVE",
|
||||
MessageReactionRemoveAll = "MESSAGE_REACTION_REMOVE_ALL",
|
||||
MessageReactionRemoveEmoji = "MESSAGE_REACTION_REMOVE_EMOJI",
|
||||
PresenceUpdate = "PRESENCE_UPDATE",
|
||||
TypingStart = "TYPING_START",
|
||||
UserUpdate = "USER_UPDATE",
|
||||
WebhooksUpdate = "WEBHOOKS_UPDATE",
|
||||
InteractionCreate = "INTERACTION_CREATE",
|
||||
VoiceStateUpdate = "VOICE_STATE_UPDATE",
|
||||
VoiceServerUpdate = "VOICE_SERVER_UPDATE",
|
||||
ApplicationCommandCreate = "APPLICATION_COMMAND_CREATE",
|
||||
ApplicationCommandUpdate = "APPLICATION_COMMAND_UPDATE",
|
||||
ApplicationCommandDelete = "APPLICATION_COMMAND_DELETE",
|
||||
}
|
||||
|
||||
export type EVENT =
|
||||
| "READY"
|
||||
| "CHANNEL_CREATE"
|
||||
| "CHANNEL_UPDATE"
|
||||
| "CHANNEL_DELETE"
|
||||
| "CHANNEL_PINS_UPDATE"
|
||||
| "GUILD_CREATE"
|
||||
| "GUILD_UPDATE"
|
||||
| "GUILD_DELETE"
|
||||
| "GUILD_BAN_ADD"
|
||||
| "GUILD_BAN_REMOVE"
|
||||
| "GUILD_EMOJI_UPDATE"
|
||||
| "GUILD_INTEGRATIONS_UPDATE"
|
||||
| "GUILD_MEMBER_ADD"
|
||||
| "GUILD_MEMBER_REMOVE"
|
||||
| "GUILD_MEMBER_UPDATE"
|
||||
| "GUILD_MEMBER_SPEAKING"
|
||||
| "GUILD_MEMBERS_CHUNK"
|
||||
| "GUILD_ROLE_CREATE"
|
||||
| "GUILD_ROLE_DELETE"
|
||||
| "GUILD_ROLE_UPDATE"
|
||||
| "INVITE_CREATE"
|
||||
| "INVITE_DELETE"
|
||||
| "MESSAGE_CREATE"
|
||||
| "MESSAGE_UPDATE"
|
||||
| "MESSAGE_DELETE"
|
||||
| "MESSAGE_DELETE_BULK"
|
||||
| "MESSAGE_REACTION_ADD"
|
||||
// TODO: add a new event: bulk add reaction:
|
||||
// | "MESSAGE_REACTION_BULK_ADD"
|
||||
| "MESSAGE_REACTION_REMOVE"
|
||||
| "MESSAGE_REACTION_REMOVE_ALL"
|
||||
| "MESSAGE_REACTION_REMOVE_EMOJI"
|
||||
| "PRESENCE_UPDATE"
|
||||
| "TYPING_START"
|
||||
| "USER_UPDATE"
|
||||
| "WEBHOOKS_UPDATE"
|
||||
| "INTERACTION_CREATE"
|
||||
| "VOICE_STATE_UPDATE"
|
||||
| "VOICE_SERVER_UPDATE"
|
||||
| "APPLICATION_COMMAND_CREATE"
|
||||
| "APPLICATION_COMMAND_UPDATE"
|
||||
| "APPLICATION_COMMAND_DELETE"
|
||||
| "MESSAGE_ACK"
|
||||
| "RELATIONSHIP_ADD"
|
||||
| "RELATIONSHIP_REMOVE"
|
||||
| CUSTOMEVENTS;
|
||||
|
||||
export type CUSTOMEVENTS = "INVALIDATED";
|
||||
@@ -0,0 +1,32 @@
|
||||
import { AllowedMentions, Embed } from "../entities/Message";
|
||||
|
||||
export interface Interaction {
|
||||
id: string;
|
||||
type: InteractionType;
|
||||
data?: {};
|
||||
guild_id: string;
|
||||
channel_id: string;
|
||||
member_id: string;
|
||||
token: string;
|
||||
version: number;
|
||||
}
|
||||
|
||||
export enum InteractionType {
|
||||
Ping = 1,
|
||||
ApplicationCommand = 2,
|
||||
}
|
||||
|
||||
export enum InteractionResponseType {
|
||||
Pong = 1,
|
||||
Acknowledge = 2,
|
||||
ChannelMessage = 3,
|
||||
ChannelMessageWithSource = 4,
|
||||
AcknowledgeWithSource = 5,
|
||||
}
|
||||
|
||||
export interface InteractionApplicationCommandCallbackData {
|
||||
tts?: boolean;
|
||||
content: string;
|
||||
embeds?: Embed[];
|
||||
allowed_mentions?: AllowedMentions;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ClientStatus, Status } from "./Status";
|
||||
import { Activity } from "./Activity";
|
||||
|
||||
export interface Presence {
|
||||
user_id: string;
|
||||
guild_id?: string;
|
||||
status: Status;
|
||||
activities: Activity[];
|
||||
client_status: ClientStatus;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export * from "./Activity";
|
||||
export * from "./Presence";
|
||||
export * from "./Interaction";
|
||||
export * from "./Event";
|
||||
export * from "./Status";
|
||||
@@ -1,30 +0,0 @@
|
||||
import "reflect-metadata";
|
||||
import { BaseEntity, BeforeInsert, BeforeUpdate, Column, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Snowflake } from "../util/Snowflake";
|
||||
import { IsString, validateOrReject } from "class-validator";
|
||||
|
||||
export class BaseClass extends BaseEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
@Column()
|
||||
@IsString()
|
||||
id: string;
|
||||
|
||||
constructor(props?: any, opts: { id?: string } = {}) {
|
||||
super();
|
||||
this.id = opts.id || Snowflake.generate();
|
||||
Object.defineProperties(this, props);
|
||||
}
|
||||
|
||||
@BeforeUpdate()
|
||||
@BeforeInsert()
|
||||
async validate() {
|
||||
await validateOrReject(this, {});
|
||||
}
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
global.BaseClass = BaseClass;
|
||||
|
||||
var test = new BaseClass({});
|
||||
|
||||
setTimeout(() => {}, 10000 * 1000);
|
||||
@@ -1,209 +0,0 @@
|
||||
import { Column, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Activity } from "./Activity";
|
||||
import { BaseClass } from "./BaseClass";
|
||||
import { ClientStatus, Status } from "./Status";
|
||||
import { validateOrReject, IsInt, IsEmail, IsPhoneNumber, IsBoolean, IsString, ValidateNested } from "class-validator";
|
||||
|
||||
export const PublicUserProjection = {
|
||||
username: true,
|
||||
discriminator: true,
|
||||
id: true,
|
||||
public_flags: true,
|
||||
avatar: true,
|
||||
accent_color: true,
|
||||
banner: true,
|
||||
bio: true,
|
||||
bot: true,
|
||||
};
|
||||
|
||||
export class User extends BaseClass {
|
||||
@Column()
|
||||
@IsString()
|
||||
username: string; // username max length 32, min 2 (should be configurable)
|
||||
|
||||
@Column()
|
||||
@IsInt()
|
||||
discriminator: string; // #0001 4 digit long string from #0001 - #9999
|
||||
|
||||
@Column()
|
||||
@IsString()
|
||||
avatar: string | null; // hash of the user avatar
|
||||
|
||||
@Column()
|
||||
@IsInt()
|
||||
accent_color: number | null; // banner color of user
|
||||
|
||||
@Column()
|
||||
banner: string | null; // hash of the user banner
|
||||
|
||||
@Column()
|
||||
@IsPhoneNumber()
|
||||
phone: string | null; // phone number of the user
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
desktop: boolean; // if the user has desktop app installed
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
mobile: boolean; // if the user has mobile app installed
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
premium: boolean; // if user bought nitro
|
||||
|
||||
@Column()
|
||||
premium_type: number; // nitro level
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
bot: boolean; // if user is bot
|
||||
|
||||
@Column()
|
||||
bio: string; // short description of the user (max 190 chars -> should be configurable)
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
system: boolean; // shouldn't be used, the api sents this field type true, if the generated message comes from a system generated author
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
nsfw_allowed: boolean; // if the user is older than 18 (resp. Config)
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
mfa_enabled: boolean; // if multi factor authentication is enabled
|
||||
|
||||
@Column()
|
||||
created_at: Date; // registration date
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
verified: boolean; // if the user is offically verified
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
disabled: boolean; // if the account is disabled
|
||||
|
||||
@Column()
|
||||
@IsBoolean()
|
||||
deleted: boolean; // if the user was deleted
|
||||
|
||||
@Column()
|
||||
@IsEmail()
|
||||
email: string | null; // email of the user
|
||||
|
||||
@Column()
|
||||
flags: bigint; // UserFlags
|
||||
|
||||
@Column()
|
||||
public_flags: bigint;
|
||||
|
||||
@Column("simple-array") // string in simple-array must not contain commas
|
||||
@IsString({ each: true })
|
||||
guilds: string[]; // array of guild ids the user is part of
|
||||
|
||||
@Column("simple-json")
|
||||
@ValidateNested() // TODO: https://github.com/typestack/class-validator#validating-nested-objects
|
||||
user_data: {
|
||||
valid_tokens_since: Date; // all tokens with a previous issue date are invalid
|
||||
hash: string; // hash of the password, salt is saved in password (bcrypt)
|
||||
fingerprints: string[]; // array of fingerprints -> used to prevent multiple accounts
|
||||
};
|
||||
|
||||
@Column("simple-json")
|
||||
@ValidateNested() // TODO: https://github.com/typestack/class-validator#validating-nested-objects
|
||||
presence: {
|
||||
status: Status;
|
||||
activities: Activity[];
|
||||
client_status: ClientStatus;
|
||||
};
|
||||
|
||||
@Column("simple-json")
|
||||
@ValidateNested() // TODO: https://github.com/typestack/class-validator#validating-nested-objects
|
||||
relationships: {
|
||||
id: string;
|
||||
nickname?: string;
|
||||
type: RelationshipType;
|
||||
}[];
|
||||
|
||||
@Column("simple-json")
|
||||
@ValidateNested() // TODO: https://github.com/typestack/class-validator#validating-nested-objects
|
||||
connected_accounts: {
|
||||
access_token: string;
|
||||
friend_sync: boolean;
|
||||
id: string;
|
||||
name: string;
|
||||
revoked: boolean;
|
||||
show_activity: boolean;
|
||||
type: string;
|
||||
verifie: boolean;
|
||||
visibility: number;
|
||||
}[];
|
||||
|
||||
@Column("simple-json")
|
||||
@ValidateNested() // TODO: https://github.com/typestack/class-validator#validating-nested-objects
|
||||
user_settings: {
|
||||
afk_timeout: number;
|
||||
allow_accessibility_detection: boolean;
|
||||
animate_emoji: boolean;
|
||||
animate_stickers: number;
|
||||
contact_sync_enabled: boolean;
|
||||
convert_emoticons: boolean;
|
||||
custom_status: {
|
||||
emoji_id: string | null;
|
||||
emoji_name: string | null;
|
||||
expires_at: number | null;
|
||||
text: string | null;
|
||||
};
|
||||
default_guilds_restricted: boolean;
|
||||
detect_platform_accounts: boolean;
|
||||
developer_mode: boolean;
|
||||
disable_games_tab: boolean;
|
||||
enable_tts_command: boolean;
|
||||
explicit_content_filter: number;
|
||||
friend_source_flags: { all: boolean };
|
||||
gateway_connected: boolean;
|
||||
gif_auto_play: boolean;
|
||||
guild_folders: // every top guild is displayed as a "folder"
|
||||
{
|
||||
color: number;
|
||||
guild_ids: string[];
|
||||
id: number;
|
||||
name: string;
|
||||
}[];
|
||||
guild_positions: string[]; // guild ids ordered by position
|
||||
inline_attachment_media: boolean;
|
||||
inline_embed_media: boolean;
|
||||
locale: string; // en_US
|
||||
message_display_compact: boolean;
|
||||
native_phone_integration_enabled: boolean;
|
||||
render_embeds: boolean;
|
||||
render_reactions: boolean;
|
||||
restricted_guilds: string[];
|
||||
show_current_game: boolean;
|
||||
status: "online" | "offline" | "dnd" | "idle";
|
||||
stream_notifications_enabled: boolean;
|
||||
theme: "dark" | "white"; // dark
|
||||
timezone_offset: number; // e.g -60
|
||||
};
|
||||
}
|
||||
|
||||
// Private user data that should never get sent to the client
|
||||
export interface PublicUser {
|
||||
id: string;
|
||||
discriminator: string;
|
||||
username: string;
|
||||
avatar: string | null;
|
||||
accent_color: number;
|
||||
banner: string | null;
|
||||
public_flags: bigint;
|
||||
bot: boolean;
|
||||
}
|
||||
|
||||
export enum RelationshipType {
|
||||
outgoing = 4,
|
||||
incoming = 3,
|
||||
blocked = 2,
|
||||
friends = 1,
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export * from "./Activity";
|
||||
export * from "./BaseClass";
|
||||
export * from "./Status";
|
||||
export * from "./User";
|
||||
@@ -21,7 +21,7 @@ export class BitField {
|
||||
* Checks whether the bitfield has a bit, or any of multiple bits.
|
||||
*/
|
||||
any(bit: BitFieldResolvable): boolean {
|
||||
return (this.bitfield & BitField.resolve.call(this, bit)) !== 0n;
|
||||
return (this.bitfield & BitField.resolve.call(this, bit)) !== BigInt(0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +61,7 @@ export class BitField {
|
||||
* @returns {BitField} These bits or new BitField if the instance is frozen.
|
||||
*/
|
||||
add(...bits: BitFieldResolvable[]): BitField {
|
||||
let total = 0n;
|
||||
let total = BigInt(0);
|
||||
for (const bit of bits) {
|
||||
total |= BitField.resolve.call(this, bit);
|
||||
}
|
||||
@@ -75,7 +75,7 @@ export class BitField {
|
||||
* @param {...BitFieldResolvable} [bits] Bits to remove
|
||||
*/
|
||||
remove(...bits: BitFieldResolvable[]) {
|
||||
let total = 0n;
|
||||
let total = BigInt(0);
|
||||
for (const bit of bits) {
|
||||
total |= BitField.resolve.call(this, bit);
|
||||
}
|
||||
@@ -127,15 +127,15 @@ export class BitField {
|
||||
* @param {BitFieldResolvable} [bit=0] - bit(s) to resolve
|
||||
* @returns {number}
|
||||
*/
|
||||
static resolve(bit: BitFieldResolvable = 0n): bigint {
|
||||
static resolve(bit: BitFieldResolvable = BigInt(0)): bigint {
|
||||
// @ts-ignore
|
||||
const FLAGS = this.FLAGS || this.constructor?.FLAGS;
|
||||
if ((typeof bit === "number" || typeof bit === "bigint") && bit >= 0n) return BigInt(bit);
|
||||
if ((typeof bit === "number" || typeof bit === "bigint") && bit >= BigInt(0)) return BigInt(bit);
|
||||
if (bit instanceof BitField) return bit.bitfield;
|
||||
if (Array.isArray(bit)) {
|
||||
// @ts-ignore
|
||||
const resolve = this.constructor?.resolve || this.resolve;
|
||||
return bit.map((p) => resolve.call(this, p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n);
|
||||
return bit.map((p) => resolve.call(this, p)).reduce((prev, p) => BigInt(prev) | BigInt(p), BigInt(0));
|
||||
}
|
||||
if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined") return FLAGS[bit];
|
||||
throw new RangeError("BITFIELD_INVALID: " + bit);
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import { VerifyOptions } from "jsonwebtoken";
|
||||
|
||||
export const JWTOptions: VerifyOptions = { algorithms: ["HS256"] };
|
||||
|
||||
export enum MessageType {
|
||||
DEFAULT = 0,
|
||||
RECIPIENT_ADD = 1,
|
||||
RECIPIENT_REMOVE = 2,
|
||||
CALL = 3,
|
||||
CHANNEL_NAME_CHANGE = 4,
|
||||
CHANNEL_ICON_CHANGE = 5,
|
||||
CHANNEL_PINNED_MESSAGE = 6,
|
||||
GUILD_MEMBER_JOIN = 7,
|
||||
USER_PREMIUM_GUILD_SUBSCRIPTION = 8,
|
||||
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1 = 9,
|
||||
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2 = 10,
|
||||
USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 = 11,
|
||||
CHANNEL_FOLLOW_ADD = 12,
|
||||
GUILD_DISCOVERY_DISQUALIFIED = 14,
|
||||
GUILD_DISCOVERY_REQUALIFIED = 15,
|
||||
GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16,
|
||||
GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17,
|
||||
THREAD_CREATED = 18,
|
||||
REPLY = 19,
|
||||
APPLICATION_COMMAND = 20,
|
||||
THREAD_STARTER_MESSAGE = 21,
|
||||
GUILD_INVITE_REMINDER = 22,
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import "reflect-metadata";
|
||||
import { createConnection } from "typeorm";
|
||||
import * as Models from "../entities";
|
||||
|
||||
// UUID extension option is only supported with postgres
|
||||
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class
|
||||
|
||||
var promise: Promise<any>;
|
||||
|
||||
export function initDatabase() {
|
||||
if (promise) return promise; // prevent initalizing multiple times
|
||||
|
||||
// @ts-ignore
|
||||
promise = createConnection({
|
||||
type: "sqlite",
|
||||
database: "database.db",
|
||||
entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
|
||||
synchronize: true,
|
||||
logging: false,
|
||||
});
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
initDatabase();
|
||||
@@ -0,0 +1,96 @@
|
||||
import { Channel } from "amqplib";
|
||||
import { RabbitMQ } from "./RabbitMQ";
|
||||
import EventEmitter from "events";
|
||||
import { EVENT, Event } from "../interfaces";
|
||||
const events = new EventEmitter();
|
||||
|
||||
export async function emitEvent(payload: Omit<Event, "created_at">) {
|
||||
const id = (payload.channel_id || payload.user_id || payload.guild_id) as string;
|
||||
if (!id) return console.error("event doesn't contain any id", payload);
|
||||
|
||||
if (RabbitMQ.connection) {
|
||||
const data = typeof payload.data === "object" ? JSON.stringify(payload.data) : payload.data; // use rabbitmq for event transmission
|
||||
await RabbitMQ.channel?.assertExchange(id, "fanout", { durable: false });
|
||||
|
||||
// assertQueue isn't needed, because a queue will automatically created if it doesn't exist
|
||||
const successful = RabbitMQ.channel?.publish(id, "", Buffer.from(`${data}`), { type: payload.event });
|
||||
if (!successful) throw new Error("failed to send event");
|
||||
} else {
|
||||
events.emit(id, payload);
|
||||
}
|
||||
}
|
||||
|
||||
export async function initEvent() {
|
||||
await RabbitMQ.init(); // does nothing if rabbitmq is not setup
|
||||
if (RabbitMQ.connection) {
|
||||
} else {
|
||||
// use event emitter
|
||||
}
|
||||
}
|
||||
|
||||
export interface EventOpts extends Event {
|
||||
acknowledge?: Function;
|
||||
channel?: Channel;
|
||||
cancel: Function;
|
||||
}
|
||||
|
||||
export interface ListenEventOpts {
|
||||
channel?: Channel;
|
||||
acknowledge?: boolean;
|
||||
}
|
||||
|
||||
export async function listenEvent(event: string, callback: (event: EventOpts) => any, opts?: ListenEventOpts) {
|
||||
if (RabbitMQ.connection) {
|
||||
// @ts-ignore
|
||||
return rabbitListen(opts?.channel || RabbitMQ.channel, event, callback, { acknowledge: opts?.acknowledge });
|
||||
} else {
|
||||
const cancel = () => {
|
||||
events.removeListener(event, callback);
|
||||
};
|
||||
events.addListener(event, (opts) => callback({ ...opts, cancel }));
|
||||
|
||||
return cancel;
|
||||
}
|
||||
}
|
||||
|
||||
async function rabbitListen(
|
||||
channel: Channel,
|
||||
id: string,
|
||||
callback: (event: EventOpts) => any,
|
||||
opts?: { acknowledge?: boolean }
|
||||
) {
|
||||
await channel.assertExchange(id, "fanout", { durable: false });
|
||||
const q = await channel.assertQueue("", { exclusive: true, autoDelete: true });
|
||||
|
||||
const cancel = () => {
|
||||
channel.cancel(q.queue);
|
||||
channel.unbindQueue(q.queue, id, "");
|
||||
};
|
||||
|
||||
channel.bindQueue(q.queue, id, "");
|
||||
channel.consume(
|
||||
q.queue,
|
||||
(opts) => {
|
||||
if (!opts) return;
|
||||
|
||||
const data = JSON.parse(opts.content.toString());
|
||||
const event = opts.properties.type as EVENT;
|
||||
|
||||
callback({
|
||||
event,
|
||||
data,
|
||||
acknowledge() {
|
||||
channel.ack(opts);
|
||||
},
|
||||
channel,
|
||||
cancel,
|
||||
});
|
||||
// rabbitCh.ack(opts);
|
||||
},
|
||||
{
|
||||
noAck: !opts?.acknowledge,
|
||||
}
|
||||
);
|
||||
|
||||
return cancel;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { BitField } from "./BitField";
|
||||
|
||||
export class Intents extends BitField {
|
||||
static FLAGS = {
|
||||
GUILDS: BigInt(1) << BigInt(0),
|
||||
GUILD_MEMBERS: BigInt(1) << BigInt(1),
|
||||
GUILD_BANS: BigInt(1) << BigInt(2),
|
||||
GUILD_EMOJIS: BigInt(1) << BigInt(3),
|
||||
GUILD_INTEGRATIONS: BigInt(1) << BigInt(4),
|
||||
GUILD_WEBHOOKS: BigInt(1) << BigInt(5),
|
||||
GUILD_INVITES: BigInt(1) << BigInt(6),
|
||||
GUILD_VOICE_STATES: BigInt(1) << BigInt(7),
|
||||
GUILD_PRESENCES: BigInt(1) << BigInt(8),
|
||||
GUILD_MESSAGES: BigInt(1) << BigInt(9),
|
||||
GUILD_MESSAGE_REACTIONS: BigInt(1) << BigInt(10),
|
||||
GUILD_MESSAGE_TYPING: BigInt(1) << BigInt(11),
|
||||
DIRECT_MESSAGES: BigInt(1) << BigInt(12),
|
||||
DIRECT_MESSAGE_REACTIONS: BigInt(1) << BigInt(13),
|
||||
DIRECT_MESSAGE_TYPING: BigInt(1) << BigInt(14),
|
||||
};
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
// https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
|
||||
// Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
|
||||
import { MemberDocument, MemberModel } from "../models/Member";
|
||||
import { ChannelDocument, ChannelModel } from "../models/Channel";
|
||||
import { ChannelPermissionOverwrite } from "../models/Channel";
|
||||
import { Role, RoleDocument, RoleModel } from "../models/Role";
|
||||
import { In } from "typeorm";
|
||||
import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities";
|
||||
import { BitField } from "./BitField";
|
||||
import { GuildDocument, GuildModel } from "../models/Guild";
|
||||
// TODO: check role hierarchy permission
|
||||
|
||||
var HTTPError: any;
|
||||
@@ -138,12 +135,12 @@ export class Permissions extends BitField {
|
||||
// ~ operator inverts deny (e.g. 011 -> 100)
|
||||
// & operator only allows 1 for both ~deny and permission (e.g. 010 & 100 -> 000)
|
||||
// | operators adds both together (e.g. 000 + 100 -> 100)
|
||||
}, init || 0n);
|
||||
}, init || BigInt(0));
|
||||
}
|
||||
|
||||
static rolePermission(roles: Role[]) {
|
||||
// adds all permissions of all roles together (Bit OR)
|
||||
return roles.reduce((permission, role) => permission | BigInt(role.permissions), 0n);
|
||||
return roles.reduce((permission, role) => permission | BigInt(role.permissions), BigInt(0));
|
||||
}
|
||||
|
||||
static finalPermission({
|
||||
@@ -201,10 +198,10 @@ export class Permissions extends BitField {
|
||||
}
|
||||
|
||||
export type PermissionCache = {
|
||||
channel?: ChannelDocument | null;
|
||||
member?: MemberDocument | null;
|
||||
guild?: GuildDocument | null;
|
||||
roles?: RoleDocument[] | null;
|
||||
channel?: Channel | null;
|
||||
member?: Member | null;
|
||||
guild?: Guild | null;
|
||||
roles?: Role[] | null;
|
||||
user_id?: string;
|
||||
};
|
||||
|
||||
@@ -219,23 +216,23 @@ export async function getPermission(
|
||||
if (!user_id) throw new HTTPError("User not found");
|
||||
|
||||
if (channel_id && !channel) {
|
||||
channel = await ChannelModel.findOne(
|
||||
channel = await Channel.findOneOrFail(
|
||||
{ id: channel_id },
|
||||
{ permission_overwrites: true, recipient_ids: true, owner_id: true, guild_id: true }
|
||||
).exec();
|
||||
{ select: ["permission_overwrites", "recipients", "owner", "guild"] }
|
||||
);
|
||||
if (!channel) throw new HTTPError("Channel not found", 404);
|
||||
if (channel.guild_id) guild_id = channel.guild_id;
|
||||
}
|
||||
|
||||
if (guild_id) {
|
||||
if (!guild) guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec();
|
||||
if (!guild) guild = await Guild.findOneOrFail({ id: guild_id }, { select: ["owner"] });
|
||||
if (!guild) throw new HTTPError("Guild not found");
|
||||
if (guild.owner_id === user_id) return new Permissions(Permissions.FLAGS.ADMINISTRATOR);
|
||||
|
||||
if (!member) member = await MemberModel.findOne({ guild_id, id: user_id }, "roles").exec();
|
||||
if (!member) member = await Member.findOneOrFail({ guild_id, id: user_id }, { select: ["roles"] });
|
||||
if (!member) throw new HTTPError("Member not found");
|
||||
|
||||
if (!roles) roles = await RoleModel.find({ guild_id, id: { $in: member.roles } }).exec();
|
||||
if (!roles) roles = await Role.find({ guild_id, id: In(member.roles) });
|
||||
}
|
||||
|
||||
var permission = Permissions.finalPermission({
|
||||
|
||||
+11
-10
@@ -1,18 +1,19 @@
|
||||
import amqp, { Connection, Channel } from "amqplib";
|
||||
import Config from "./Config";
|
||||
// import Config from "./Config";
|
||||
|
||||
export const RabbitMQ: { connection: Connection | null; channel: Channel | null; init: () => Promise<void> } = {
|
||||
connection: null,
|
||||
channel: null,
|
||||
init: async function () {
|
||||
const host = Config.get().rabbitmq.host;
|
||||
if (!host) return;
|
||||
console.log(`[RabbitMQ] connect: ${host}`);
|
||||
this.connection = await amqp.connect(host, {
|
||||
timeout: 1000 * 60,
|
||||
});
|
||||
console.log(`[RabbitMQ] connected`);
|
||||
this.channel = await this.connection.createChannel();
|
||||
console.log(`[RabbitMQ] channel created`);
|
||||
return;
|
||||
// const host = Config.get().rabbitmq.host;
|
||||
// if (!host) return;
|
||||
// console.log(`[RabbitMQ] connect: ${host}`);
|
||||
// this.connection = await amqp.connect(host, {
|
||||
// timeout: 1000 * 60,
|
||||
// });
|
||||
// console.log(`[RabbitMQ] connected`);
|
||||
// this.channel = await this.connection.createChannel();
|
||||
// console.log(`[RabbitMQ] channel created`);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
// https://github.com/discordjs/discord.js/blob/master/src/util/UserFlags.js
|
||||
// Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
|
||||
|
||||
import { BitField } from "./BitField";
|
||||
|
||||
export class UserFlags extends BitField {
|
||||
static FLAGS = {
|
||||
DISCORD_EMPLOYEE: BigInt(1) << BigInt(0),
|
||||
PARTNERED_SERVER_OWNER: BigInt(1) << BigInt(1),
|
||||
HYPESQUAD_EVENTS: BigInt(1) << BigInt(2),
|
||||
BUGHUNTER_LEVEL_1: BigInt(1) << BigInt(3),
|
||||
HOUSE_BRAVERY: BigInt(1) << BigInt(6),
|
||||
HOUSE_BRILLIANCE: BigInt(1) << BigInt(7),
|
||||
HOUSE_BALANCE: BigInt(1) << BigInt(8),
|
||||
EARLY_SUPPORTER: BigInt(1) << BigInt(9),
|
||||
TEAM_USER: BigInt(1) << BigInt(10),
|
||||
SYSTEM: BigInt(1) << BigInt(12),
|
||||
BUGHUNTER_LEVEL_2: BigInt(1) << BigInt(14),
|
||||
VERIFIED_BOT: BigInt(1) << BigInt(16),
|
||||
EARLY_VERIFIED_BOT_DEVELOPER: BigInt(1) << BigInt(17),
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { JWTOptions } from "./Constants";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { UserModel } from "../models";
|
||||
import jwt, { VerifyOptions } from "jsonwebtoken";
|
||||
import { User } from "../entities";
|
||||
|
||||
export const JWTOptions: VerifyOptions = { algorithms: ["HS256"] };
|
||||
|
||||
export function checkToken(token: string, jwtSecret: string): Promise<any> {
|
||||
return new Promise((res, rej) => {
|
||||
@@ -8,10 +9,10 @@ export function checkToken(token: string, jwtSecret: string): Promise<any> {
|
||||
jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => {
|
||||
if (err || !decoded) return rej("Invalid Token");
|
||||
|
||||
const user = await UserModel.findOne(
|
||||
const user = await User.findOneOrFail(
|
||||
{ id: decoded.id },
|
||||
{ "user_data.valid_tokens_since": true, bot: true, disabled: true, deleted: true }
|
||||
).exec();
|
||||
{ select: ["user_data", "bot", "disabled", "deleted"] }
|
||||
);
|
||||
if (!user) return rej("Invalid Token");
|
||||
// we need to round it to seconds as it saved as seconds in jwt iat and valid_tokens_since is stored in milliseconds
|
||||
if (decoded.iat * 1000 < user.user_data.valid_tokens_since.setSeconds(0, 0)) return rej("Invalid Token");
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
export * from "./Regex";
|
||||
export * from "./String";
|
||||
export * from "./BitField";
|
||||
export * from "./Database";
|
||||
export * from "./Intents";
|
||||
export * from "./MessageFlags";
|
||||
export * from "./Permissions";
|
||||
export * from "./Snowflake";
|
||||
export * from "./toBigInt";
|
||||
export * from "./RabbitMQ";
|
||||
export * from "./Event";
|
||||
export * from "./checkToken";
|
||||
Reference in New Issue
Block a user