feat: wip: added some stuff
This commit is contained in:
parent
cbeb867f4c
commit
9bb30097a4
11 changed files with 586 additions and 224 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"typescript.tsdk": "node_modules/typescript/lib"
|
|
||||||
}
|
|
40
package.json
40
package.json
|
@ -38,16 +38,19 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sapphire/async-queue": "^1.1.4",
|
"@sapphire/async-queue": "^1.1.4",
|
||||||
"abort-controller": "^3.0.0",
|
"abort-controller": "^3.0.0",
|
||||||
|
"cache-manager": "^3.4.4",
|
||||||
|
"cache-manager-ioredis": "^2.1.0",
|
||||||
"fast-zlib": "^2.0.1",
|
"fast-zlib": "^2.0.1",
|
||||||
"form-data": "^4.0.0",
|
"form-data": "^4.0.0",
|
||||||
|
"ioredis": "^4.27.7",
|
||||||
"lodash": "^4.17.21",
|
"lodash": "^4.17.21",
|
||||||
"node-fetch": "^2.6.1",
|
"node-fetch": "^2.6.1",
|
||||||
"ws": "^7.5.3"
|
"ws": "^8.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@commitlint/cli": "^12.1.4",
|
"@commitlint/cli": "^13.1.0",
|
||||||
"@commitlint/config-conventional": "^12.1.4",
|
"@commitlint/config-conventional": "^13.1.0",
|
||||||
"@commitlint/cz-commitlint": "^12.1.4",
|
"@commitlint/cz-commitlint": "^13.1.0",
|
||||||
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
"@istanbuljs/nyc-config-typescript": "^1.0.1",
|
||||||
"@saithodev/semantic-release-gitea": "^2.1.0",
|
"@saithodev/semantic-release-gitea": "^2.1.0",
|
||||||
"@semantic-release/changelog": "^5.0.1",
|
"@semantic-release/changelog": "^5.0.1",
|
||||||
|
@ -55,33 +58,36 @@
|
||||||
"@semantic-release/git": "^9.0.0",
|
"@semantic-release/git": "^9.0.0",
|
||||||
"@semantic-release/npm": "^7.1.3",
|
"@semantic-release/npm": "^7.1.3",
|
||||||
"@semantic-release/release-notes-generator": "^9.0.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/chai": "^4.2.21",
|
||||||
"@types/eslint": "^7.28.0",
|
"@types/eslint": "^7.28.0",
|
||||||
"@types/lodash": "^4.14.171",
|
"@types/ioredis": "^4.26.7",
|
||||||
"@types/mocha": "^8.2.3",
|
"@types/lodash": "^4.14.172",
|
||||||
"@types/node": "^16.3.2",
|
"@types/mocha": "^9.0.0",
|
||||||
"@types/node-fetch": "^2.5.11",
|
"@types/node": "^16.4.13",
|
||||||
|
"@types/node-fetch": "^2.5.12",
|
||||||
"@types/semantic-release": "^17.2.1",
|
"@types/semantic-release": "^17.2.1",
|
||||||
"@types/sinon": "^10.0.2",
|
"@types/sinon": "^10.0.2",
|
||||||
"@types/source-map-support": "^0.5.4",
|
"@types/source-map-support": "^0.5.4",
|
||||||
"@types/ws": "^7.4.6",
|
"@types/ws": "^7.4.7",
|
||||||
"@typescript-eslint/eslint-plugin": "^4.28.3",
|
"@typescript-eslint/eslint-plugin": "^4.29.0",
|
||||||
"@typescript-eslint/parser": "^4.28.3",
|
"@typescript-eslint/parser": "^4.29.0",
|
||||||
"@typescript-eslint/typescript-estree": "^4.28.3",
|
"@typescript-eslint/typescript-estree": "^4.29.0",
|
||||||
"chai": "^4.3.4",
|
"chai": "^4.3.4",
|
||||||
"commitizen": "^4.2.4",
|
"commitizen": "^4.2.4",
|
||||||
"eslint": "^7.30.0",
|
"eslint": "^7.32.0",
|
||||||
"eslint-formatter-pretty": "^4.1.0",
|
"eslint-formatter-pretty": "^4.1.0",
|
||||||
"eslint-plugin-unicorn": "^34.0.1",
|
"eslint-plugin-unicorn": "^35.0.0",
|
||||||
"husky": "^7.0.1",
|
"husky": "^7.0.1",
|
||||||
"inquirer": "^8.1.2",
|
"inquirer": "^8.1.2",
|
||||||
"lint-staged": "^11.0.1",
|
"lint-staged": "^11.1.2",
|
||||||
"mocha": "^9.0.2",
|
"mocha": "^9.0.3",
|
||||||
"nyc": "^15.1.0",
|
"nyc": "^15.1.0",
|
||||||
"pinst": "^2.1.6",
|
"pinst": "^2.1.6",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"semantic-release": "^17.4.4",
|
"semantic-release": "^17.4.4",
|
||||||
"sinon": "^11.1.1",
|
"sinon": "^11.1.2",
|
||||||
"source-map-support": "^0.5.19",
|
"source-map-support": "^0.5.19",
|
||||||
"ts-node": "^10.1.0",
|
"ts-node": "^10.1.0",
|
||||||
"tsconfig-paths": "^3.10.1",
|
"tsconfig-paths": "^3.10.1",
|
||||||
|
|
|
@ -53,7 +53,7 @@ export class ApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
public async post<T>(options: IRequestOptions): Promise<T> {
|
public async post<T>(options: IRequestOptions): Promise<T> {
|
||||||
return this.manager.createRequest<T>({
|
return await this.manager.createRequest<T>({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: options.body,
|
body: options.body,
|
||||||
headers: options.headers,
|
headers: options.headers,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { ApiClient } from './apiClient';
|
import { ApiClient } from './apiClient';
|
||||||
|
|
||||||
import type { Client } from '../client/client';
|
import type { Client } from '../client/client';
|
||||||
import type { IApiCreateMessage } from '../utils/types';
|
import type { IApiCreateMessage, IApiCreateSlashCommand, IApiUser, IFile } from '../utils/types';
|
||||||
|
|
||||||
export class ApiHelper {
|
export class ApiHelper {
|
||||||
public apiClient: ApiClient;
|
public apiClient: ApiClient;
|
||||||
|
@ -16,11 +16,35 @@ export class ApiHelper {
|
||||||
this.apiClient = new ApiClient(this.client, this._token);
|
this.apiClient = new ApiClient(this.client, this._token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public createMessage(channelId: string, options: IApiCreateMessage): void {
|
// TODO: Return message object
|
||||||
this.apiClient.post({
|
public async createMessage(channelId: string, options: IApiCreateMessage, files?: IFile[]): Promise<void> {
|
||||||
|
await this.apiClient.post({
|
||||||
path: `/channels/${channelId}/messages`,
|
path: `/channels/${channelId}/messages`,
|
||||||
requireAuth: true,
|
requireAuth: true,
|
||||||
body: options,
|
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,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
import { ApiHelper } from '../api/apiHelper';
|
import { ApiHelper } from '../api/apiHelper';
|
||||||
|
import { ClientUser } from '../structures/clientUser';
|
||||||
import { GatewayClient } from '../gateway/gatewayClient';
|
import { GatewayClient } from '../gateway/gatewayClient';
|
||||||
import { defaults } from '../utils/defaults';
|
import { defaults } from '../utils/defaults';
|
||||||
|
|
||||||
import type { DeepRequired, IClientOptions } from '../utils/types';
|
import type { DeepRequired, IClientOptions } from '../utils/types';
|
||||||
|
|
||||||
export class Client extends EventEmitter {
|
export class Client extends EventEmitter {
|
||||||
public api: ApiHelper;
|
public readonly api: ApiHelper;
|
||||||
public readonly options: DeepRequired<IClientOptions>;
|
public readonly options: DeepRequired<IClientOptions>;
|
||||||
public ws: GatewayClient;
|
public readonly user: ClientUser;
|
||||||
|
public readonly ws: GatewayClient;
|
||||||
|
|
||||||
private _token: string;
|
private _token: string;
|
||||||
|
|
||||||
|
@ -20,6 +22,7 @@ export class Client extends EventEmitter {
|
||||||
this._token = token;
|
this._token = token;
|
||||||
|
|
||||||
this.api = new ApiHelper(this, this._token);
|
this.api = new ApiHelper(this, this._token);
|
||||||
|
this.user = new ClientUser(this);
|
||||||
this.ws = new GatewayClient(this, this._token);
|
this.ws = new GatewayClient(this, this._token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
src/structures/clientUser.ts
Normal file
9
src/structures/clientUser.ts
Normal 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
18
src/structures/user.ts
Normal 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
51
src/utils/cacheManager.ts
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,10 @@ export const defaults: IDefaultOptions = {
|
||||||
url: 'https://discord.com/api',
|
url: 'https://discord.com/api',
|
||||||
version: 9,
|
version: 9,
|
||||||
},
|
},
|
||||||
|
cache: {
|
||||||
|
cache: 'memory',
|
||||||
|
ttl: 60,
|
||||||
|
},
|
||||||
presence: {},
|
presence: {},
|
||||||
ws: {
|
ws: {
|
||||||
compression: true,
|
compression: true,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import type IORedis from 'ioredis';
|
||||||
|
|
||||||
// Interfaces
|
// Interfaces
|
||||||
export interface IApiClientOptions {
|
export interface IApiClientOptions {
|
||||||
offset?: number;
|
offset?: number;
|
||||||
|
@ -8,8 +10,20 @@ export interface IApiClientOptions {
|
||||||
version?: number;
|
version?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ICacheMemoryClientOptions {
|
||||||
|
cache?: 'memory';
|
||||||
|
ttl?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ICacheRedisClientOptions {
|
||||||
|
cache?: 'redis';
|
||||||
|
ttl?: number;
|
||||||
|
options: IORedis.RedisOptions;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IClientOptions {
|
export interface IClientOptions {
|
||||||
api?: IApiClientOptions;
|
api?: IApiClientOptions;
|
||||||
|
cache?: ICacheMemoryClientOptions | ICacheRedisClientOptions;
|
||||||
presence?: IUpdatePresence;
|
presence?: IUpdatePresence;
|
||||||
ws?: IWebSocketClientOptions;
|
ws?: IWebSocketClientOptions;
|
||||||
}
|
}
|
||||||
|
@ -77,9 +91,44 @@ export interface IWebSocketClientOptions {
|
||||||
version?: number;
|
version?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Api Rest Request Interfaces
|
// Api Interfaces
|
||||||
// TODO: Add message components
|
// TODO: Add message components
|
||||||
|
// Dont add the files option. That goes into a separate option when creating a request
|
||||||
export interface IApiCreateMessage {
|
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?: {
|
allowed_mentions?: {
|
||||||
parse?: 'everyone' | 'roles' | 'users'[];
|
parse?: 'everyone' | 'roles' | 'users'[];
|
||||||
replied_user?: boolean;
|
replied_user?: boolean;
|
||||||
|
|
Reference in a new issue