Archived
0
0
Fork 0

feat: wip: added some stuff

This commit is contained in:
Daryl Ronningen 2021-08-07 15:05:20 -07:00
parent cbeb867f4c
commit 9bb30097a4
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
11 changed files with 586 additions and 224 deletions

View file

@ -1,3 +0,0 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

View file

@ -38,16 +38,19 @@
"dependencies": {
"@sapphire/async-queue": "^1.1.4",
"abort-controller": "^3.0.0",
"cache-manager": "^3.4.4",
"cache-manager-ioredis": "^2.1.0",
"fast-zlib": "^2.0.1",
"form-data": "^4.0.0",
"ioredis": "^4.27.7",
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"ws": "^7.5.3"
"ws": "^8.0.0"
},
"devDependencies": {
"@commitlint/cli": "^12.1.4",
"@commitlint/config-conventional": "^12.1.4",
"@commitlint/cz-commitlint": "^12.1.4",
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@commitlint/cz-commitlint": "^13.1.0",
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@saithodev/semantic-release-gitea": "^2.1.0",
"@semantic-release/changelog": "^5.0.1",
@ -55,33 +58,36 @@
"@semantic-release/git": "^9.0.0",
"@semantic-release/npm": "^7.1.3",
"@semantic-release/release-notes-generator": "^9.0.3",
"@types/cache-manager": "^3.4.2",
"@types/cache-manager-ioredis": "^2.0.2",
"@types/chai": "^4.2.21",
"@types/eslint": "^7.28.0",
"@types/lodash": "^4.14.171",
"@types/mocha": "^8.2.3",
"@types/node": "^16.3.2",
"@types/node-fetch": "^2.5.11",
"@types/ioredis": "^4.26.7",
"@types/lodash": "^4.14.172",
"@types/mocha": "^9.0.0",
"@types/node": "^16.4.13",
"@types/node-fetch": "^2.5.12",
"@types/semantic-release": "^17.2.1",
"@types/sinon": "^10.0.2",
"@types/source-map-support": "^0.5.4",
"@types/ws": "^7.4.6",
"@typescript-eslint/eslint-plugin": "^4.28.3",
"@typescript-eslint/parser": "^4.28.3",
"@typescript-eslint/typescript-estree": "^4.28.3",
"@types/ws": "^7.4.7",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
"@typescript-eslint/typescript-estree": "^4.29.0",
"chai": "^4.3.4",
"commitizen": "^4.2.4",
"eslint": "^7.30.0",
"eslint": "^7.32.0",
"eslint-formatter-pretty": "^4.1.0",
"eslint-plugin-unicorn": "^34.0.1",
"eslint-plugin-unicorn": "^35.0.0",
"husky": "^7.0.1",
"inquirer": "^8.1.2",
"lint-staged": "^11.0.1",
"mocha": "^9.0.2",
"lint-staged": "^11.1.2",
"mocha": "^9.0.3",
"nyc": "^15.1.0",
"pinst": "^2.1.6",
"rimraf": "^3.0.2",
"semantic-release": "^17.4.4",
"sinon": "^11.1.1",
"sinon": "^11.1.2",
"source-map-support": "^0.5.19",
"ts-node": "^10.1.0",
"tsconfig-paths": "^3.10.1",

View file

@ -53,7 +53,7 @@ export class ApiClient {
}
public async post<T>(options: IRequestOptions): Promise<T> {
return this.manager.createRequest<T>({
return await this.manager.createRequest<T>({
method: 'POST',
body: options.body,
headers: options.headers,

View file

@ -1,7 +1,7 @@
import { ApiClient } from './apiClient';
import type { Client } from '../client/client';
import type { IApiCreateMessage } from '../utils/types';
import type { IApiCreateMessage, IApiCreateSlashCommand, IApiUser, IFile } from '../utils/types';
export class ApiHelper {
public apiClient: ApiClient;
@ -16,11 +16,35 @@ export class ApiHelper {
this.apiClient = new ApiClient(this.client, this._token);
}
public createMessage(channelId: string, options: IApiCreateMessage): void {
this.apiClient.post({
// TODO: Return message object
public async createMessage(channelId: string, options: IApiCreateMessage, files?: IFile[]): Promise<void> {
await this.apiClient.post({
path: `/channels/${channelId}/messages`,
requireAuth: true,
body: options,
files: files,
});
}
public async getCurrentUser(): Promise<IApiUser> {
return await this.apiClient.get<IApiUser>({
path: '/users/@me',
requireAuth: true,
});
}
public async createSlashCommand(options: IApiCreateSlashCommand): Promise<void> {
await this.apiClient.post({
path: `/applications/${this.client.user.id}/commands`,
requireAuth: true,
body: options,
});
}
public async getUser(id: string): Promise<IApiUser> {
return await this.apiClient.get<IApiUser>({
path: `/users/${id}`,
requireAuth: true,
});
}
}

View file

@ -1,15 +1,17 @@
import _ from 'lodash';
import EventEmitter from 'events';
import { ApiHelper } from '../api/apiHelper';
import { ClientUser } from '../structures/clientUser';
import { GatewayClient } from '../gateway/gatewayClient';
import { defaults } from '../utils/defaults';
import type { DeepRequired, IClientOptions } from '../utils/types';
export class Client extends EventEmitter {
public api: ApiHelper;
public readonly api: ApiHelper;
public readonly options: DeepRequired<IClientOptions>;
public ws: GatewayClient;
public readonly user: ClientUser;
public readonly ws: GatewayClient;
private _token: string;
@ -20,6 +22,7 @@ export class Client extends EventEmitter {
this._token = token;
this.api = new ApiHelper(this, this._token);
this.user = new ClientUser(this);
this.ws = new GatewayClient(this, this._token);
}

View file

@ -0,0 +1,9 @@
import { User } from './user';
import type { Client } from '../client/client';
export class ClientUser extends User {
public constructor(client: Client) {
super(client);
}
}

18
src/structures/user.ts Normal file
View file

@ -0,0 +1,18 @@
import type { IApiUser } from '..';
import type { Client } from '../client/client';
export class User {
public client: Client;
public id: string;
private _options: IApiUser;
public constructor(client: Client, options: IApiUser) {
this.client = client;
this._options = options;
}
public async fetch(id: string): Promise<IApiUser> {
return this.client.api.getUser(id);
}
}

51
src/utils/cacheManager.ts Normal file
View file

@ -0,0 +1,51 @@
import cacheManager from 'cache-manager';
import redisStore from 'cache-manager-ioredis';
import type { Client } from '../client/client';
export class CacheManager {
public client: Client;
private readonly _cacheManager: cacheManager.Cache;
public constructor(client: Client) {
this.client = client;
if (this.client.options.cache.cache === 'redis') this._cacheManager = cacheManager.caching({
store: redisStore,
ttl: this.client.options.cache.ttl,
...this.client.options.cache.options,
});
else this._cacheManager = cacheManager.caching({
store: 'memory',
ttl: this.client.options.cache.ttl,
});
}
public async del(key: string): Promise<void> {
return new Promise((res, rej) => {
this._cacheManager.del(key, (err) => {
if (err) rej(err);
else res();
});
});
}
public async get<T>(key: string): Promise<T | undefined> {
return new Promise((res, rej) => {
this._cacheManager.get<T>(key, (err, result) => {
if (err) rej(err);
else res(result);
});
});
}
public async set<T>(key: string, value: T): Promise<T> {
return new Promise((res, rej) => {
this._cacheManager.set<T>(key, value, this.client.options.cache.ttl, (err) => {
if (err) rej(err);
else res(value);
});
});
}
}

View file

@ -10,6 +10,10 @@ export const defaults: IDefaultOptions = {
url: 'https://discord.com/api',
version: 9,
},
cache: {
cache: 'memory',
ttl: 60,
},
presence: {},
ws: {
compression: true,

View file

@ -1,3 +1,5 @@
import type IORedis from 'ioredis';
// Interfaces
export interface IApiClientOptions {
offset?: number;
@ -8,8 +10,20 @@ export interface IApiClientOptions {
version?: number;
}
export interface ICacheMemoryClientOptions {
cache?: 'memory';
ttl?: number;
}
export interface ICacheRedisClientOptions {
cache?: 'redis';
ttl?: number;
options: IORedis.RedisOptions;
}
export interface IClientOptions {
api?: IApiClientOptions;
cache?: ICacheMemoryClientOptions | ICacheRedisClientOptions;
presence?: IUpdatePresence;
ws?: IWebSocketClientOptions;
}
@ -77,9 +91,44 @@ export interface IWebSocketClientOptions {
version?: number;
}
// Api Rest Request Interfaces
// Api Interfaces
// TODO: Add message components
// Dont add the files option. That goes into a separate option when creating a request
export interface IApiCreateMessage {
allowed_mentions?: {
parse?: 'everyone' | 'roles' | 'users'[];
replied_user?: boolean;
roles?: string[];
users?: string[];
};
content?: string;
embeds?: IMessageEmbed[];
message_reference?: {
channel_id?: string;
fail_if_not_exists?: boolean;
guild_id?: string;
message_id?: string;
};
tts?: boolean;
}
export interface IApiUser {
avatar: string;
bot?: boolean;
discriminator: string;
email?: string;
flags?: number;
id: string;
locale?: string;
mfa_enabled?: boolean;
premium_type?: number;
public_flags?: number;
username: string;
system?: boolean;
verified?: boolean;
}
export interface IApiCreateSlashCommand {
allowed_mentions?: {
parse?: 'everyone' | 'roles' | 'users'[];
replied_user?: boolean;

595
yarn.lock

File diff suppressed because it is too large Load diff