Skip to content

Commit

Permalink
refactor: 40% done + new qol
Browse files Browse the repository at this point in the history
  • Loading branch information
USERSATOSHI committed Oct 13, 2024
1 parent 5f8cfb4 commit a5600bb
Show file tree
Hide file tree
Showing 56 changed files with 4,473 additions and 3,358 deletions.
Empty file modified .husky/pre-commit
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions bin/add.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ test('log', () => {
name: 'package.json',
type: 'file',
content: `{
"name": "@akarui/${lib}",
"name": "@aoijs/${lib}",
"version": "0.0.1",
"description": "@akarui/${lib} - A Extension for Aoi.js",
"description": "@aoijs/${lib} - A Extension for Aoi.js",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/types/index.d.ts",
Expand Down
Empty file modified bin/index.mjs
100755 → 100644
Empty file.
20 changes: 9 additions & 11 deletions bin/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import path from 'node:path';
import { spawn } from 'node:child_process';

/**
* run all tests for the given library
* @param {object} options - the options object
* @param {string} options.library - the library to test
* Run all tests for the given library
* @param {object} options - The options object
* @param {string} options.library - The library to test
* @returns {Promise<void>}
*/
const test = async ({ library }) => {
console.log(`Running tests for ${library}`);

// find all test files in the library
// recursively check all folders for test files
const mainFolder = path.resolve(process.cwd(), `lib/${library}`);
// Resolve paths in a cross-platform way
const mainFolder = path.join(process.cwd(), 'lib', library);

const spwn = spawn(
'npx',
Expand All @@ -22,12 +21,15 @@ const test = async ({ library }) => {
'--color',
'--coverage',
'--coverageProvider=v8',
`--config=${process.cwd()}/jest.config.js`,
`--config=${path.join(process.cwd(), 'jest.config.js')}`,
// Specify the folder where Jest should look for tests
`${mainFolder}`,
],
{
stdio: 'inherit',
// Set cwd to the project root
cwd: mainFolder,
shell: true,
},
);

Expand All @@ -46,10 +48,6 @@ const test = async ({ library }) => {
spwn.on('close', () => {
console.log(`Tested ${library}`);
});

spwn.on('disconnect', () => {
console.log('Disconnected');
});
};

export default test;
13 changes: 5 additions & 8 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import globals from 'globals';
import tseslint from 'typescript-eslint';
// import tseslint from 'typescript-eslint';
import jsdoc from 'eslint-plugin-jsdoc';
import tsdoc from 'eslint-plugin-tsdoc';

Expand All @@ -24,7 +24,7 @@ export default [

...compat.extends('xo-typescript'),
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
...tseslint.configs.stylisticTypeChecked,
// ...tseslint.configs.stylisticTypeChecked,
{
// enable object curly spacing
rules: {
Expand Down Expand Up @@ -52,12 +52,9 @@ export default [
],
'@typescript-eslint/prefer-literal-enum-member': [
'error',
[
'error',
{
allowBitwiseExpressions: true,
},
],
{
allowBitwiseExpressions: true,
},
],
},
},
Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default {
isolatedModules: true,
},
},
// Ensure the testMatch pattern works cross-platform
testMatch: ['<rootDir>/src/**/*.test.ts'],
},
// Add more libraries as needed
],
Expand Down
6 changes: 3 additions & 3 deletions lib/aoi.db/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@akarui/aoi.db",
"name": "@aoijs/aoi.db",
"version": "0.0.1",
"description": "@akarui/aoi.db - A Extension for Aoi.js",
"description": "@aoijs/aoi.db - A Extension for Aoi.js",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
Expand Down Expand Up @@ -44,6 +44,6 @@
},
"readme": "https://github.com/Aoijs/aoi.js/blob/main/README.md",
"dependencies": {
"@akarui/structures": "workspace:^"
"@aoijs/structures": "workspace:^"
}
}
2 changes: 2 additions & 0 deletions lib/aoi.js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
coverage
index.mjs
index.js
9 changes: 9 additions & 0 deletions lib/aoi.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
},
"readme": "https://github.com/Aoijs/aoi.js/blob/monorepo/README.md",
"dependencies": {
"@aoijs/structures": "workspace:^",
"@discordjs/collection": "^2.1.1",
"@discordjs/ws": "^2.0.0",
"discord.js": "^14.16.2",
"uglify-js": "^3.18.0"
},
"devDependencies": {
"@types/uglify-js": "^3.17.5",
"@vladfrangu/async_event_emitter": "^2.4.6",
"discord-api-types": "^0.37.100"
}
}
92 changes: 90 additions & 2 deletions lib/aoi.js/src/classes/AoiClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,91 @@
export default class AoiClient {
import Transpiler from '@aoi.js/core/Transpiler.js';
import { CommandManager } from '@aoi.js/managers/Command.js';
import FunctionManager from '@aoi.js/managers/Function.js';
import { type IAoiClientOptions } from '@aoi.js/typings/interface.js';
import { type AoiClientProps } from '@aoi.js/typings/type.js';
import { Client, DefaultWebSocketManagerOptions, Partials, type ClientOptions } from 'discord.js';

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
interface AoiClient extends AoiClientProps {
client: Client;
}


// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
class AoiClient {
client!: Client;

}
transpiler: Transpiler;
database = null;
managers!: {
commands: CommandManager;
functions: FunctionManager;
};

readonly #options: IAoiClientOptions;


constructor(options: IAoiClientOptions) {
this.#validateOptions(options);
this.#options = options;

const transpilerOptions = {
minify: options.transpilerOptions?.minify ?? true,
customFunctions: {},
};

this.transpiler = new Transpiler(transpilerOptions, this);
this.managers = {
commands: new CommandManager(this),
functions: new FunctionManager(this),
};

if (options.testMode) return;

const djsOptions: ClientOptions = options.djsClientOptions ?? { intents: 0 };
djsOptions.partials ||= [
Partials.GuildMember,
Partials.Channel,
Partials.Message,
Partials.Reaction,
Partials.User,
Partials.GuildScheduledEvent,
Partials.ThreadMember,
];
djsOptions.intents = options.intents;

this.client = new Client(djsOptions);
}

async start() {
await this.client.login(this.#options.token);
}

#validateOptions(options: IAoiClientOptions) {
if (options.intents === undefined) {
throw new SyntaxError('Intents not provided, "Guilds" intent is required');
}

if (isNaN(options.intents)) {
throw new TypeError('Provided intents are not of type "number"');
}

if (!options.token) {
throw new SyntaxError('Token not provided');
}

if (!options.prefix?.length) {
throw new SyntaxError('Prefix not provided');
}
}

get prefix() {
return this.#options.prefix;
}

get options() {
return this.#options;
}
}

export default AoiClient;
121 changes: 59 additions & 62 deletions lib/aoi.js/src/classes/Command.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,78 @@
import Transpiler from '@aoi.js/core/Transpiler';
import { type CommandTypes } from '@aoi.js/typings/type.js';
import { type AsyncFunction, type CommandTypes } from '@aoi.js/typings/type.js';
import type AoiClient from './AoiClient';
import { type ICommandOptions } from '@aoi.js/typings/interface.js';
import { type Snowflake } from 'discord.js';

export default class Command {
[key: string]: unknown;
name!: string;
type!: CommandTypes;
code!: string | (() => Promise<void>);
aliases?: string[];
channel?: string | bigint;
channel?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
__path__!: string;
reverseRead?: boolean;
executeAt?: 'guild' | 'dm' | 'both';
// eslint-disable-next-line @typescript-eslint/naming-convention
__compiled__!: () => Promise<void>;
__compiled__!: AsyncFunction;

constructor(data: ICommandOptions, client: AoiClient) {
// this.name = data.name;
// this.type = data.type;
// this.code = data.code;
// this.aliases = data.aliases;
// this.__path__ = data.__path__;
// this.executeAt = data.executeAt ?? 'both';
// this.reverseRead = data.reverseRead ?? false;
// for (const key in data) {
// if (
// ![
// 'name',
// 'type',
// 'code',
// 'aliases',
// '__path__',
// 'executeAt',
// 'reverseRead',
// ].includes(key)
// )
// this[key] = data[key];
// }
// if (this.code instanceof Function) this.__compiled__ = this.code;
// else {
// let chan: Snowflake | undefined | AsyncFunction;
// if (this.channel) {
// if (
// typeof this.channel === 'string' &&
// this.channel.startsWith('$')
// ) {
// chan = new Transpiler(this.channel, {
// sendMessage: false,
// minify: true,
// customFunctions: client.managers.functions.functions.toJSON(),
// scopeData: {
// name: 'GLOBAL_CHANNEL',
// },
// client,
// }).func;
// } else if (typeof this.channel === 'string')
// chan = BigInt(this.channel);
// else chan = this.channel;
// }
// const func = new Transpiler(this.code, {
// sendMessage: true,
// minify: true,
// reverse: this.reverseRead,
// customFunctions: client.managers.functions.functions.toJSON(),
// client,
// scopeData: {
// functions:
// typeof chan === 'function'
// ? `${chan.toString()}`
// : undefined,
// useChannel:
// typeof chan === 'function' ? `${chan.name}()` : chan,
// },
// });
// this.__compiled__ = func.func;
this.name = data.name;
this.type = data.type;
this.code = data.code;
this.aliases = data.aliases;
this.__path__ = data.__path__;
this.executeAt = data.executeAt ?? 'both';
this.reverseRead = data.reverseRead ?? false;

const transpiler = client.transpiler;

for (const key in data) {
if (
![
'name',
'type',
'code',
'aliases',
'__path__',
'executeAt',
'reverseRead',
].includes(key)
)
this[key] = data[key];
}

if (this.code instanceof Function) this.__compiled__ = this.code;
else {
let channelId: Snowflake | undefined | AsyncFunction;
if (this.channel) {
if (
typeof this.channel === 'string' &&
this.channel.startsWith('$')
) {
channelId = transpiler.transpile(this.channel, {
sendMessage: false,
scopeData: {
name: 'GLOBAL_CHANNEL',
},
}).func;
} else channelId = this.channel;
}

const func = transpiler.transpile(this.code, {
sendMessage: true,
reverse: this.reverseRead,
scopeData: {
functions:
typeof channelId === 'function'
? [channelId.toString()]
: undefined,
useChannel:
typeof channelId === 'function' ? `${channelId.name}()` : channelId,
},
});
this.__compiled__ = func.func;
}
}
}
Loading

0 comments on commit a5600bb

Please sign in to comment.