Skip to content

Commit

Permalink
refactor: move /utilities into this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
didinele committed Jul 27, 2024
1 parent 026ac60 commit 29a976e
Show file tree
Hide file tree
Showing 63 changed files with 4,609 additions and 2,341 deletions.
14 changes: 7 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
.vscode/*
!.vscode/settings.json
.DS_STORE

node_modules
.env.private
**/node_modules

coverage/*
**/dist/*
**/coverage
**/dist
**/.turbo

.yarn/*
!.yarn/patches
Expand All @@ -14,7 +15,6 @@ coverage/*
!.yarn/sdks
!.yarn/versions
.pnp.*
logs-archive
.DS_STORE

.turbo
logs-archive
.env.private
3 changes: 0 additions & 3 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn commitlint --edit $1
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn build && yarn lint
yarn build && yarn lint && yarn test
10 changes: 9 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:20-alpine
FROM node:22-alpine
LABEL name "chatsift-next"

WORKDIR /usr/
Expand All @@ -11,6 +11,10 @@ COPY turbo.json package.json tsconfig.json yarn.lock .yarnrc.yml ./
COPY .yarn ./.yarn

COPY packages/core/package.json ./packages/core/package.json
COPY packages/discord-utils/package.json ./packages/discord-utils/package.json
COPY packages/parse-relative-time/package.json ./packages/parse-relative-time/package.json
COPY packages/pino-rotate-file/package.json ./packages/pino-rotate-file/package.json
COPY packages/readdir/package.json ./packages/readdir/package.json

COPY services/api/package.json ./services/api/package.json
COPY services/discord-proxy/package.json ./services/discord-proxy/package.json
Expand All @@ -24,6 +28,10 @@ COPY prisma ./prisma
RUN yarn prisma generate

COPY packages/core ./packages/core
COPY packages/discord-utils ./packages/discord-utils
COPY packages/parse-relative-time ./packages/parse-relative-time
COPY packages/pino-rotate-file ./packages/pino-rotate-file
COPY packages/readdir ./packages/readdir

COPY services/api ./services/api
COPY services/discord-proxy ./services/discord-proxy
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# automoderator
# chatsift

Monorepo for all of our bots and their common utilities, along with some NPM packages.

## Licensing

Expand Down
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const commonRuleset = merge(...common, {
rules: {
'no-eq-null': ['off'],
eqeqeq: ['error', 'always', { null: 'ignore' }],
'jsdoc/no-undefined-types': ['off'],
'import/no-duplicates': ['off'],
},
});

Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,34 @@
"scripts": {
"lint": "turbo run lint && prettier --check .",
"build": "turbo run build",
"test": "turbo run test",
"format": "prettier --write .",
"postinstall": "is-ci || husky install || true",
"update": "yarn upgrade-interactive",
"prisma": "dotenv -e .env.private -- prisma"
},
"dependencies": {
"prisma": "^5.16.2"
"prisma": "^5.17.0"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@commitlint/config-angular": "^19.3.0",
"@vitest/coverage-v8": "^2.0.4",
"dotenv-cli": "^7.4.2",
"eslint": "^8.57.0",
"eslint-config-neon": "^0.1.62",
"eslint-formatter-pretty": "^6.0.1",
"husky": "^9.0.11",
"husky": "^9.1.3",
"is-ci": "^3.0.1",
"lodash.merge": "^4.6.2",
"prettier": "^3.3.3",
"prisma-kysely": "^1.8.0",
"rimraf": "^5.0.9",
"turbo": "^2.0.6",
"typescript": "^5.5.3",
"typescript-eslint": "^7.16.0"
"tsup": "^8.2.3",
"turbo": "^2.0.9",
"typescript": "^5.5.4",
"typescript-eslint": "^7.17.0",
"vitest": "^2.0.4"
},
"resolutions": {
"discord-api-types": "0.37.84"
Expand Down
14 changes: 7 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"node": ">=16.9.0"
},
"devDependencies": {
"@types/node": "^20.14.10",
"@types/node": "^20.14.12",
"@types/pg": "^8.11.6",
"typescript": "^5.5.3"
"typescript": "^5.5.4"
},
"dependencies": {
"@chatsift/discord-utils": "^0.4.0",
"@chatsift/pino-rotate-file": "^0.4.0",
"@chatsift/discord-utils": "workspace:^",
"@chatsift/pino-rotate-file": "workspace:^",
"@discordjs/brokers": "^0.3.0",
"@discordjs/builders": "^1.8.2",
"@discordjs/core": "^1.2.0",
Expand All @@ -30,14 +30,14 @@
"@sapphire/bitfield": "^1.2.2",
"@sapphire/discord-utilities": "^3.3.0",
"bin-rw": "^0.1.0",
"coral-command": "^0.9.1",
"coral-command": "^0.10.0",
"inversify": "^6.0.2",
"ioredis": "5.3.2",
"kysely": "^0.27.4",
"murmurhash": "^2.0.1",
"pg": "^8.12.0",
"pino": "^9.3.1",
"pino-pretty": "^11.2.1",
"pino": "^9.3.2",
"pino-pretty": "^11.2.2",
"tslib": "^2.6.3"
}
}
3 changes: 1 addition & 2 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
"declaration": true,
"declarationMap": true
},
"include": ["./src/**/*.ts"],
"exclude": ["./**/__tests__"]
"include": ["./src/**/*.ts"]
}
23 changes: 23 additions & 0 deletions packages/discord-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# `@chatsift/discord-utils`

[![GitHub](https://img.shields.io/badge/License-GNU%20AGPLv3-yellow.svg)](https://github.com/chatsift/automoderator/blob/main/LICENSE)
[![npm](https://img.shields.io/npm/v/@chatsift/discord-utils?color=crimson&logo=npm)](https://www.npmjs.com/package/@chatsift/discord-utils)
[![TypeScript](https://github.com/chatsift/automoderator/actions/workflows/test.yml/badge.svg)](https://github.com/chatsift/automoderator/actions/workflows/test.yml)

Niche utilities for working with Discord's API

## Installation

- `npm install @chatsift/discord-utils`
- `pnpm install @chatsift/discord-utils`
- `yarn add @chatsift/discord-utils`

## Contributing

Please see the main [README.md](https://github.com/chatsift/automoderator) for info on how to contribute to this package or the other `@chatsift` packages.

## LICENSE

This project is licensed under the GNU AGPLv3 license.

It should, however, be noted that some packages are forks of other open source projects, and are therefore, sub-licensed.
42 changes: 42 additions & 0 deletions packages/discord-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@chatsift/discord-utils",
"description": "Niche utilities for working with Discord's raw API",
"version": "0.5.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"directories": {
"lib": "src"
},
"files": [
"dist"
],
"scripts": {
"build": "tsup && tsc",
"test": "vitest run",
"lint": "eslint src",
"prepack": "yarn build"
},
"repository": {
"type": "git",
"url": "https://github.com/chatsift/automoderator.git"
},
"bugs": {
"url": "https://github.com/chatsift/automoderator/issues"
},
"homepage": "https://github.com/chatsift/automoderator",
"devDependencies": {
"@types/node": "^20.14.12",
"tsup": "^8.2.3",
"typescript": "^5.5.4",
"vitest": "^2.0.4"
},
"dependencies": {
"discord-api-types": "^0.37.93",
"tslib": "^2.6.3"
}
}
59 changes: 59 additions & 0 deletions packages/discord-utils/src/__tests__/embed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { APIEmbed, APIEmbedField } from 'discord-api-types/v10';
import { describe, test, expect } from 'vitest';
import { addFields, ellipsis, MESSAGE_LIMITS, truncateEmbed } from '../embed.js';

describe('addFields', () => {
test('no existing fields', () => {
const embed: APIEmbed = {};

const field: APIEmbedField = { name: 'foo', value: 'bar' };
expect(addFields(embed, field)).toStrictEqual({ ...embed, fields: [field] });
});

test('existing fields', () => {
const field: APIEmbedField = { name: 'foo', value: 'bar' };
const embed: APIEmbed = { fields: [field] };

expect(addFields(embed, field)).toStrictEqual({ ...embed, fields: [field, field] });
});
});

describe('ellipsis', () => {
test('no ellipsis', () => {
expect(ellipsis('foo', 5)).toBe('foo');
});

test('ellipsis', () => {
expect(ellipsis('foobar', 4)).toBe('f...');
});

test('too long for ellipsis', () => {
expect(ellipsis('foo', 2)).toBe('fo');
});
});

describe('truncateEmbed', () => {
test('basic embed properties', () => {
const embed: APIEmbed = {
title: 'foo'.repeat(256),
description: 'bar'.repeat(4_096),
author: { name: 'baz'.repeat(256) },
footer: { text: 'qux'.repeat(2_048) },
};

const truncated = truncateEmbed(embed);

expect(truncated.title).toBe(ellipsis(embed.title!, MESSAGE_LIMITS.EMBEDS.TITLE));
expect(truncated.description).toBe(ellipsis(embed.description!, MESSAGE_LIMITS.EMBEDS.DESCRIPTION));
expect(truncated.author?.name).toBe(ellipsis(embed.author!.name, MESSAGE_LIMITS.EMBEDS.AUTHOR));
expect(truncated.footer?.text).toBe(ellipsis(embed.footer!.text, MESSAGE_LIMITS.EMBEDS.FOOTER));
});

test('fields', () => {
const embed: APIEmbed = {
fields: Array.from<APIEmbedField>({ length: 30 }).fill({ name: 'foo', value: 'bar' }),
};

expect(truncateEmbed(embed).fields).toHaveLength(MESSAGE_LIMITS.EMBEDS.FIELD_COUNT);
});
});
35 changes: 35 additions & 0 deletions packages/discord-utils/src/__tests__/sortChannels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { APIChannel } from 'discord-api-types/v10';
import { ChannelType } from 'discord-api-types/v10';
import { test, expect } from 'vitest';
import { sortChannels } from '../sortChannels.js';

test('sorting a list of channels', () => {
// Higher position than the category, but should come out on top
const first = {
id: '1',
position: 1,
type: ChannelType.GuildText,
} as unknown as APIChannel;

const second = {
id: '0',
position: 0,
type: ChannelType.GuildCategory,
} as unknown as APIChannel;

const third = {
id: '2',
position: 0,
type: ChannelType.GuildText,
parent_id: '0',
} as unknown as APIChannel;

const fourth = {
id: '3',
position: 1,
type: ChannelType.GuildText,
parent_id: '0',
} as unknown as APIChannel;

expect(sortChannels([first, second, third, fourth])).toStrictEqual([first, second, third, fourth]);
});
Loading

0 comments on commit 29a976e

Please sign in to comment.