Skip to content

Commit

Permalink
style: setup lint again (#114)
Browse files Browse the repository at this point in the history
* chore: setup lint again

* style: lint some more files

* chore: change import and bump deps

* chore: typecheck
  • Loading branch information
imranbarbhuiya authored Oct 5, 2022
1 parent c88b338 commit ea7a209
Show file tree
Hide file tree
Showing 78 changed files with 1,586 additions and 668 deletions.
15 changes: 4 additions & 11 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
{
"root": true,
"extends": "@sapphire",
"ignorePatterns": ["**/dist/*"],
"parser": "@typescript-eslint/parser",
"extends": ["mahir/common", "mahir/node", "mahir/typescript", "mahir/tsdoc", "mahir/prettier"],
"ignorePatterns": ["**/dist/*", "**/coverage/*"],
"rules": {
"@typescript-eslint/no-base-to-string": "off",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/no-unsafe-member-access": "error",
"@typescript-eslint/no-unsafe-argument": "error",
"@typescript-eslint/no-unsafe-assignment": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"no-console": "error"
"no-console": "error",
"import/extensions": "off"
}
}
10 changes: 10 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"endOfLine": "lf",
"printWidth": 150,
"quoteProps": "as-needed",
"semi": true,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "none",
"useTabs": true
}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"editor.insertSpaces": false,
"editor.detectIndentation": false,
"files.eol": "\n",
"cSpell.words": ["cooldown", "tagscript"]
"cSpell.words": ["cooldown", "tagscript"],
"editor.codeActionsOnSave": {
"source.fixAll": true
}
}
16 changes: 5 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"clean:full": "node scripts/clean-full.mjs",
"docs": "typedoc",
"format": "prettier --cache --write .",
"lint": "eslint packages --ext mjs,js,ts --fix --cache",
"lint": "eslint . --ext mjs,js,ts --fix --cache",
"update": "yarn upgrade-interactive",
"postinstall": "husky install .github/husky",
"test": "turbo run test",
Expand All @@ -22,18 +22,13 @@
"devDependencies": {
"@commitlint/cli": "^17.1.2",
"@commitlint/config-conventional": "^17.1.0",
"@favware/cliff-jumper": "^1.8.7",
"@favware/cliff-jumper": "^1.8.8",
"@favware/npm-deprecate": "^1.0.5",
"@sapphire/eslint-config": "^4.3.8",
"@sapphire/prettier-config": "^1.4.4",
"@types/node": "^18.6.3",
"@typescript-eslint/eslint-plugin": "^5.38.1",
"@typescript-eslint/parser": "^5.38.1",
"@types/node": "^18.8.2",
"@vitest/coverage-c8": "^0.23.4",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.24.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-config-mahir": "^0.0.8",
"husky": "^8.0.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
Expand Down Expand Up @@ -72,6 +67,5 @@
"ansi-regex": "^5.0.1",
"minimist": "^1.2.6"
},
"packageManager": "yarn@3.2.3",
"prettier": "@sapphire/prettier-config"
"packageManager": "yarn@3.2.3"
}
2 changes: 1 addition & 1 deletion packages/tagscript-plugin-discord/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"bot tag"
],
"devDependencies": {
"@favware/cliff-jumper": "^1.8.7",
"@favware/cliff-jumper": "^1.8.8",
"discord.js": "^14.5.0",
"tagscript": "workspace:^",
"tsup": "^6.2.3",
Expand Down
5 changes: 4 additions & 1 deletion packages/tagscript-plugin-discord/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './lib';
export * from './lib/interfaces';
export * from './lib/Transformer';
export * from './lib/Parsers';
export * from './lib/Utils';
16 changes: 6 additions & 10 deletions packages/tagscript-plugin-discord/src/lib/Parsers/AllowDeny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import { BaseParser, type IParser, type Context } from 'tagscript';
* it will send the response if one is given. Multiple role, user or channel
* requirements can be given, and should be split by a `,`.
*
* @usage
* @example
* ```yaml
* {require(user,role,channel):response}
* ```
*
* @alias allowlist, whitelist
*
* Aliases: allowlist, whitelist
* @example
* ```yaml
* {require(Moderator)}
Expand All @@ -27,7 +25,7 @@ export class RequiredParser extends BaseParser implements IParser {

public parse(ctx: Context) {
if (ctx.response.actions.require) return null;
const ids = ctx.tag.parameter!.split(',').map((s) => s.trim());
const ids = ctx.tag.parameter!.split(',').map((param) => param.trim());
ctx.response.actions.require = { ids, message: ctx.tag.payload };
return '';
}
Expand All @@ -40,13 +38,11 @@ export class RequiredParser extends BaseParser implements IParser {
* it will send the response if one is given. Multiple user, role or channel
* requirements can be given, and should be split by a `,`.
*
* @usage
* @example
* ```yaml
* {deny(user,role,channel):response}
* ```
*
* @alias denylist, blacklist
*
* Aliases: denylist, blacklist
* @example
* ```yaml
* {deny(Moderator)}
Expand All @@ -62,7 +58,7 @@ export class DenyParser extends BaseParser implements IParser {

public parse(ctx: Context) {
if (ctx.response.actions.deny) return null;
const ids = ctx.tag.parameter!.split(',').map((s) => s.trim());
const ids = ctx.tag.parameter!.split(',').map((param) => param.trim());
ctx.response.actions.deny = { ids, message: ctx.tag.payload };
return '';
}
Expand Down
9 changes: 3 additions & 6 deletions packages/tagscript-plugin-discord/src/lib/Parsers/Cooldown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import { BaseParser, type Context, type IParser } from 'tagscript';
* Payload can have `{retryAfter}`, `{name}` which will be replaced with the time remaining
* and the name of the tag respectively.
*
* @usage
* @example
* ```yaml
* { cooldown(seconds): response }
* ```
*
* @alias cd
*
* Aliases: cd
* @example
* ```yaml
* { cooldown(5): This tag is on cooldown. }
* { cooldown(5): The tag {name} is on cooldown. Please try again in {retryAfter}. }
* ```
*
*/
export class CooldownParser extends BaseParser implements IParser {
public constructor() {
Expand All @@ -28,7 +25,7 @@ export class CooldownParser extends BaseParser implements IParser {

public parse(ctx: Context) {
const { parameter, payload } = ctx.tag;
const cooldown = parseInt(parameter!, 10);
const cooldown = Number.parseInt(parameter!, 10);

ctx.response.actions.cooldown = {
cooldown,
Expand Down
29 changes: 16 additions & 13 deletions packages/tagscript-plugin-discord/src/lib/Parsers/Embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ import type { EmbedData, APIEmbed } from 'discord.js';
* formatted embed JSON or manually inputting
* the accepted embed properties.
*
* @remarks
* Embed can be used either by using a json string or by using the embed properties.
* @example
* Using JSON
* @usage
* ```yaml
* ```yaml
* { embed: json }
* ```
* @example
* ```yaml
* ```
* @example
* ```yaml
* { embed: { "title": "Hello!", "description": "This is a test embed." } }
* { embed: {
* "title": "Here's a random duck!",
* "image": { "url": "https://random-d.uk/api/randomimg" },
* "color": 15194415
* } }
* ```
*
* @example
* Using properties
* @usage
* ```yaml
* { embed(property): value }
* ```
*
* @example
* ```yaml
* { embed(color): 0x37b2cb }
* { embed(title): Rules }
* { embed(description): Follow these rules to ensure a good experience in our server! }
* { embed(field): Rule 1|Respect everyone you speak to.|false }
* ```
*
* @see The return type depends on user's input. So it might not be `EmbedData | APIEmbed`. So use a typeguard to check.
* @remarks
* The return type depends on user's input. So it might not be `EmbedData | APIEmbed`. So use a typeguard to check.
*/
export class EmbedParser extends BaseParser implements IParser {
public constructor() {
Expand All @@ -62,24 +62,27 @@ export class EmbedParser extends BaseParser implements IParser {
]
});
}

return this.returnEmbed(ctx, { [ctx.tag.parameter]: ctx.tag.payload });
}

/**
* This method is protected so that anyone can extend the embed json parser to allow urls
* @param payload
*
* @param payload - The payload to parse
* @returns
*/
protected parseEmbedJSON(payload: string): Awaitable<EmbedData | APIEmbed> {
protected parseEmbedJSON(payload: string): Awaitable<APIEmbed | EmbedData> {
return JSON.parse(payload);
}

private returnEmbed(ctx: Context, data: EmbedData | APIEmbed): string {
private returnEmbed(ctx: Context, data: APIEmbed | EmbedData): string {
ctx.response.actions.embed ??= {} as EmbedData;
const { fields, ...rest } = data;
if (fields) {
ctx.response.actions.embed.fields = [...(ctx.response.actions.embed.fields ?? []), ...fields];
}

// @ts-expect-error - The return type should be unknown
ctx.response.actions.embed = { ...ctx.response.actions.embed, ...rest };
return '';
Expand Down
2 changes: 1 addition & 1 deletion packages/tagscript-plugin-discord/src/lib/Parsers/Files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseParser, Context, IParser, split } from 'tagscript';
import { BaseParser, split, type Context, type IParser } from 'tagscript';

/**
* This parser allows sending files along with message using file url.
Expand Down
6 changes: 4 additions & 2 deletions packages/tagscript-plugin-discord/src/lib/Parsers/Format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export class DateFormatParser extends BaseParser implements IParser {
if (['unix', 'currenttime'].includes(declaration!)) {
return Date.now().toString();
}

const parameter = ctx.tag.parameter ?? 'f';
if (!['f', 'F', 't', 'T', 'R'].includes(parameter)) return null;
let payload: string | number = ctx.tag.payload ?? Date.now().toString();
let payload: number | string = ctx.tag.payload ?? Date.now().toString();
if (!/^\d+$/.test(payload)) {
payload = new Date(payload).getTime().toString();
}
if (payload.length > 10) payload = Math.floor(Number(payload) / 1000);

if (payload.length > 10) payload = Math.floor(Number(payload) / 1_000);

return `<t:${payload}:${parameter}>`;
}
Expand Down
17 changes: 13 additions & 4 deletions packages/tagscript-plugin-discord/src/lib/Transformer/Base.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import type { Lexer, ITransformer } from 'tagscript';
import type { GuildChannel } from '../interfaces';
import type { Role, User, GuildMember, Guild, CommandInteraction } from 'discord.js';
import { GuildChannel } from '../interfaces';
import type { Lexer, ITransformer } from 'tagscript';

export type outputResolvable = string | number | boolean | null | undefined;
export type outputResolvable = boolean | number | string | null | undefined;

/**
* A key value pair without sensitive information.
*
* @typeParam T - The base type.
*/
export interface SafeValues<T> {
[key: string]: outputResolvable | ((base: T) => outputResolvable);
}

/**
* Transformer for [Discord.js](https://discord.js.org/) objects.
*
* @typeParam T - The base type.
*/
export abstract class BaseTransformer<T extends GuildChannel | Role | User | GuildMember | Guild | CommandInteraction> implements ITransformer {
export abstract class BaseTransformer<T extends CommandInteraction | Guild | GuildChannel | GuildMember | Role | User> implements ITransformer {
protected base: T;

protected safeValues: SafeValues<T> = {};

public constructor(base: T, safeValues: SafeValues<T> = {}) {
this.base = base;
this.safeValues.id = this.base.id;
// eslint-disable-next-line @typescript-eslint/no-base-to-string
this.safeValues.mention = base.toString();
this.safeValues.name = 'name' in base ? base.name : '';
this.updateSafeValues();
Expand Down
20 changes: 12 additions & 8 deletions packages/tagscript-plugin-discord/src/lib/Transformer/Guild.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Guild } from 'discord.js';
import { BaseTransformer } from './Base';

import type { Guild } from 'discord.js';

/**
* Transformer for Discord [Guild](https://discord.js.org/#/docs/discord.js/stable/class/Guild)
*
* @properties
* Properties:
* ```yaml
* id: Gives guild id.
* name: Gives guild name.
Expand All @@ -27,14 +28,17 @@ import { BaseTransformer } from './Base';
* channelIds: Gives guild channels ids.
* channelNames: Gives guild channels names.
* channelCount: Gives guild channels count.
* emojiCount: Gives guild emojis count. (These values depends on cache so it might be inaccurate)
* stickerCount: Gives guild stickers count. (These values depends on cache so it might be inaccurate)
* bots: Gives guild bots count. (These values depends on cache so it might be inaccurate)
* humans: Gives guild humans count. (These values depends on cache so it might be inaccurate)
* emojiCount: Gives guild emojis count.
* stickerCount: Gives guild stickers count.
* bots: Gives guild bots count.
* humans: Gives guild humans count.
* afkTimeout: Gives guild afk timeout.
* afkChannel: Gives guild afk channel.
* verificationLevel: Gives guild verification level.
* ```
*
* @remarks
* Some properties like `emojiCount`, `stickerCount`, `bots`, `humans` depends on cache so it might be inaccurate.
*/
export class GuildTransformer extends BaseTransformer<Guild> {
protected override updateSafeValues() {
Expand All @@ -59,8 +63,8 @@ export class GuildTransformer extends BaseTransformer<Guild> {
this.safeValues.channelCount = this.base.channels.cache.size;
this.safeValues.emojiCount = this.base.emojis.cache.size;
this.safeValues.stickerCount = this.base.stickers.cache.size;
this.safeValues.bots = this.base.members.cache.filter((m) => m.user.bot).size;
this.safeValues.humans = this.base.members.cache.filter((m) => !m.user.bot).size;
this.safeValues.bots = this.base.members.cache.filter((member) => member.user.bot).size;
this.safeValues.humans = this.base.members.cache.filter((member) => !member.user.bot).size;
this.safeValues.afkTimeout = this.base.afkTimeout;
this.safeValues.afkChannel = `${this.base.afkChannel}`;
this.safeValues.verificationLevel = this.base.verificationLevel;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { GuildMember } from 'discord.js';
import { BaseTransformer } from './Base';

import type { GuildMember } from 'discord.js';

/**
* Transformer for [GuildMember](https://discord.js.org/#/docs/discord.js/stable/class/GuildMember).
*
* @properties
* Properties:
* ```yaml
* id: Gives member id.
* mention: Mentions the member.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GuildChannel } from '../interfaces';
import { BaseTransformer } from './Base';

import type { GuildChannel } from '../interfaces';

/**
* Transformer for Discord {@link GuildChannel}
*
* @properties
* Properties:
* ```yaml
* id: Gives channel id.
* mention: Mentions the channel.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommandInteraction } from 'discord.js';
import { BaseTransformer } from './Base';

import type { CommandInteraction } from 'discord.js';

export class InteractionTransformer extends BaseTransformer<CommandInteraction> {
protected override updateSafeValues() {
this.safeValues.applicationId = this.base.applicationId;
Expand Down
Loading

0 comments on commit ea7a209

Please sign in to comment.