Skip to content

Commit

Permalink
feat: v3
Browse files Browse the repository at this point in the history
Signed-off-by: Promise <me@promise.solutions>
  • Loading branch information
promise committed Dec 19, 2024
1 parent 30aad7a commit 1e8c732
Show file tree
Hide file tree
Showing 39 changed files with 699 additions and 557 deletions.
2 changes: 1 addition & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"extends": [
"github>promise/renovate-config",
"github>promise/renovate-config:force-node-version(22)",
"github>promise/renovate-config:force-mongo-version(4)"
"github>promise/renovate-config:force-mongo-version(8)"
]
}
11 changes: 0 additions & 11 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,3 @@ repository:
description: "A Discord bot to easily create testing servers, without having to write in that 2FA code to delete it later"
private: false
topics: "discord, discord-bot, testing-tools, utility"

branches:
- name: main
protection:
required_status_checks:
checks:
- context: Test Build
- context: ESLint
- context: DeepScan
- context: Jest
- context: CodeQL
5 changes: 0 additions & 5 deletions .github/workflows/docker-compose-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ name: Docker Compose

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
Expand Down
26 changes: 13 additions & 13 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ jobs:
runs-on: self-hosted

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

- name: Login to ghcr.io
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to ghcr.io
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: ${{ github.ref == 'refs/heads/main' && 'Build and Push' || 'Test Build' }}
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
push: ${{ github.ref == 'refs/heads/main' }}
tags: ghcr.io/biaw/test-area:latest
- name: ${{ github.ref == 'refs/heads/main' && 'Build and Push' || 'Test Build' }}
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
push: ${{ github.ref == 'refs/heads/main' }}
tags: ghcr.io/biaw/test-area:latest
9 changes: 0 additions & 9 deletions .vscode/extensions.json

This file was deleted.

9 changes: 0 additions & 9 deletions .vscode/settings.json

This file was deleted.

3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
FROM node:22-alpine@sha256:6e80991f69cc7722c561e5d14d5e72ab47c0d6b6cfb3ae50fb9cf9a7b30fdf97 AS base
RUN apk --no-cache add g++ gcc make python3

WORKDIR /app
ENV IS_DOCKER=true


# install prod dependencies

Expand Down
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Promise Solutions

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[![Docker test](https://img.shields.io/github/actions/workflow/status/biaw/test-area/docker-compose-test.yml?branch=main)](https://github.com/biaw/test-area/actions/workflows/docker-compose-test.yml)
[![Linting](https://img.shields.io/github/actions/workflow/status/biaw/test-area/linting.yml?branch=main&label=quality)](https://github.com/biaw/test-area/actions/workflows/linting.yml)
[![Testing](https://img.shields.io/github/actions/workflow/status/biaw/test-area/testing.yml?branch=main&label=test)](https://github.com/biaw/test-area/actions/workflows/testing.yml)
[![DeepScan grade](https://deepscan.io/api/teams/16173/projects/19537/branches/707763/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=16173&pid=19537&bid=707763)
[![discord.js version](https://img.shields.io/github/package-json/dependency-version/biaw/test-area/discord.js)](https://www.npmjs.com/package/discord.js)
[![GitHub Issues](https://img.shields.io/github/issues-raw/biaw/test-area.svg)](https://github.com/biaw/test-area/issues)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr-raw/biaw/test-area.svg)](https://github.com/biaw/test-area/pulls)
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ services:
build: .
restart: always
environment:
- CLIENT_ID=$CLIENT_ID
- CLIENT_TOKEN=$CLIENT_TOKEN
- WORKER_TOKENS=$WORKER_TOKENS
- DATABASE_URI=mongodb://db
- OWNER_ID=$OWNER_ID
- THEME_COLOR=$THEME_COLOR
- AREA_LIMIT_PER_USER=$AREA_LIMIT_PER_USER
volumes:
- ./logs:/app/logs
depends_on:
- db
db:
image: mongo:4@sha256:52c42cbab240b3c5b1748582cc13ef46d521ddacae002bbbda645cebed270ec0
image: mongo:8
restart: always
volumes:
- ./database:/data/db
5 changes: 3 additions & 2 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
CLIENT_ID=
CLIENT_TOKEN=

WORKER_TOKENS=

# not required when using docker-compose.yml
DATABASE_URI=

OWNER_ID=

# optional below
THEME_COLOR=
AREA_LIMIT_PER_USER=
11 changes: 2 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@
"main": "build",
"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"docker": "npm run docker:build && npm run docker:up",
"docker:build": "docker-compose --project-directory . build",
"docker:down": "docker-compose --project-directory . down",
"docker:logs": "docker-compose --project-directory . logs --tail=500 -f",
"docker:start": "npm run docker:up",
"docker:stop": "npm run docker:down",
"docker:up": "docker-compose --project-directory . up -d",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"start": "node .",
"test": "jest"
"test": "jest",
"watch": "tsc -w"
},
"dependencies": {
"@sapphire/type": "2.6.0",
Expand Down
18 changes: 11 additions & 7 deletions src/commands/applicationCommands.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type{ ApplicationCommandData, ApplicationCommandOptionData, ApplicationCommandSubCommandData, ApplicationCommandSubGroupData } from "discord.js";
import { ApplicationCommandOptionType, ApplicationCommandType } from "discord.js";
import type{ ChatInputCommand, ChatInputCommandExecutable, ChatInputCommandOptionData, ChatInputCommandOptionDataAutocomplete } from "./chatInput";
import type { ApplicationCommandData, ApplicationCommandOptionData, ApplicationCommandSubCommandData, ApplicationCommandSubGroupData } from "discord.js";
import { ApplicationCommandOptionType, ApplicationCommandType, ApplicationIntegrationType, InteractionContextType } from "discord.js";


import type{ ChatInputCommand, ChatInputCommandExecutable, ChatInputCommandOptionData, ChatInputCommandOptionDataAutocomplete, FirstLevelChatInputCommand } from "./chatInput";
import { allChatInputCommands } from "./chatInput";
import { allMenuCommands } from "./menu";

export default function getAllApplicationCommands(commandType?: "non-test-areas" | "test-areas"): ApplicationCommandData[] {
export default function getAllApplicationCommands(commandType?: FirstLevelChatInputCommand["applicableTo"][number]): ApplicationCommandData[] {
const applicationCommands: ApplicationCommandData[] = [];

for (const command of allChatInputCommands) {
if (!commandType || command.worksIn.includes(commandType)) {
if (!commandType || command.applicableTo.includes(commandType)) {
applicationCommands.push({
name: command.name,
description: command.description,
Expand All @@ -35,7 +37,8 @@ export default function getAllApplicationCommands(commandType?: "non-test-areas"
},
})),
},
...!command.public && commandType === "non-test-areas" && { defaultMemberPermissions: 0n },
integrationTypes: [commandType === "main" ? ApplicationIntegrationType.UserInstall : ApplicationIntegrationType.GuildInstall],
...commandType === "main" && { contexts: [InteractionContextType.BotDM, InteractionContextType.Guild, InteractionContextType.PrivateChannel] },
});
}
}
Expand All @@ -45,7 +48,8 @@ export default function getAllApplicationCommands(commandType?: "non-test-areas"
applicationCommands.push({
name: command.name,
type: command.type === "message" ? ApplicationCommandType.Message : ApplicationCommandType.User,
...!command.public && commandType === "non-test-areas" && { defaultMemberPermissions: 0n },
integrationTypes: [commandType === "main" ? ApplicationIntegrationType.UserInstall : ApplicationIntegrationType.GuildInstall],
...commandType === "main" && { contexts: [InteractionContextType.BotDM, InteractionContextType.Guild, InteractionContextType.PrivateChannel] },
});
}
}
Expand Down
57 changes: 32 additions & 25 deletions src/commands/chatInput/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
import type{ ApplicationCommandAutocompleteNumericOptionData, ApplicationCommandAutocompleteStringOptionData, ApplicationCommandBooleanOptionData, ApplicationCommandChannelOptionData, ApplicationCommandMentionableOptionData, ApplicationCommandNonOptionsData, ApplicationCommandNumericOptionData, ApplicationCommandRoleOptionData, ApplicationCommandStringOptionData, ApplicationCommandUserOptionData, Awaitable, ChatInputCommandInteraction } from "discord.js";
import type { ApplicationCommandAutocompleteNumericOptionData, ApplicationCommandAutocompleteStringOptionData, ApplicationCommandBooleanOptionData, ApplicationCommandChannelOptionData, ApplicationCommandMentionableOptionData, ApplicationCommandNonOptionsData, ApplicationCommandNumericOptionData, ApplicationCommandRoleOptionData, ApplicationCommandStringOptionData, ApplicationCommandUserOptionData, Awaitable, ChatInputCommandInteraction } from "discord.js";
import { readdirSync } from "fs";
import type{ Autocomplete } from "../../handlers/interactions/autocompletes";
import type { Autocomplete } from "../../handlers/interactions/autocompletes";

export type FirstLevelChatInputCommand = {
public?: true;
worksIn: Array<"non-test-areas" | "test-areas">;
} & (ChatInputCommandExecutable | ChatInputCommandGroup<SecondLevelChatInputCommand>) & ChatInputCommandMeta;

export type SecondLevelChatInputCommand = (ChatInputCommandExecutable | ChatInputCommandGroup<ThirdLevelChatInputCommand>) & ChatInputCommandMeta;

export type ThirdLevelChatInputCommand = ChatInputCommandExecutable & ChatInputCommandMeta;

export type ChatInputCommand = FirstLevelChatInputCommand | SecondLevelChatInputCommand | ThirdLevelChatInputCommand;
export interface ChatInputCommandMeta {
description: string;
name: string;
}

export interface ChatInputCommandExecutable {
execute(interaction: ChatInputCommandInteraction<"cached">): Awaitable<void>;
execute(interaction: ChatInputCommandInteraction): Awaitable<void>;
options?: [ChatInputCommandOptionData, ...ChatInputCommandOptionData[]];
}

export interface ChatInputCommandGroup<NthLevelChatInputCommand extends SecondLevelChatInputCommand | ThirdLevelChatInputCommand> {
subcommands: [NthLevelChatInputCommand, ...NthLevelChatInputCommand[]];
}

export interface ChatInputCommandMeta {
description: string;
name: string;
}
export type FirstLevelChatInputCommand =
& { applicableTo: Array<"main" | "workers"> }
& (ChatInputCommandExecutable | ChatInputCommandGroup<SecondLevelChatInputCommand>)
& ChatInputCommandMeta;

export type SecondLevelChatInputCommand =
& (ChatInputCommandExecutable | ChatInputCommandGroup<ThirdLevelChatInputCommand>)
& ChatInputCommandMeta;

export type ThirdLevelChatInputCommand =
& ChatInputCommandExecutable
& ChatInputCommandMeta;

export type ChatInputCommand =
| FirstLevelChatInputCommand
| SecondLevelChatInputCommand
| ThirdLevelChatInputCommand;

export type ChatInputCommandOptionDataAutocomplete =
| ({ autocomplete: Autocomplete<number> } & Omit<ApplicationCommandAutocompleteNumericOptionData, "autocomplete">)
| ({ autocomplete: Autocomplete<string> } & Omit<ApplicationCommandAutocompleteStringOptionData, "autocomplete">);

export type ChatInputCommandOptionDataNoAutocomplete =
| ApplicationCommandBooleanOptionData
| ApplicationCommandChannelOptionData
| ApplicationCommandMentionableOptionData
| ApplicationCommandNonOptionsData
| ApplicationCommandRoleOptionData
| ApplicationCommandUserOptionData
| Omit<ApplicationCommandNumericOptionData, "autocomplete">
| Omit<ApplicationCommandStringOptionData, "autocomplete">;
| ApplicationCommandBooleanOptionData
| ApplicationCommandChannelOptionData
| ApplicationCommandMentionableOptionData
| ApplicationCommandNonOptionsData
| ApplicationCommandRoleOptionData
| ApplicationCommandUserOptionData
| Omit<ApplicationCommandNumericOptionData, "autocomplete">
| Omit<ApplicationCommandStringOptionData, "autocomplete">;

export type ChatInputCommandOptionData = ChatInputCommandOptionDataAutocomplete | ChatInputCommandOptionDataNoAutocomplete;

Expand Down
15 changes: 9 additions & 6 deletions src/commands/chatInput/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { msToHumanShortTime } from "../../utils/time";
export default {
name: "ping",
description: "Ping the bot",
worksIn: ["test-areas", "non-test-areas"],
public: true,
applicableTo: ["main", "workers"],
async execute(interaction) {
const now = Date.now();
await interaction.deferReply();
return void interaction.editReply(`🏓 Server latency is \`${Date.now() - now}ms\`, shard latency is \`${Math.ceil(interaction.guild.shard.ping)}ms\` and my uptime is \`${msToHumanShortTime(interaction.client.uptime)}\`.`);
const start = Date.now();
const [interactionLatency, gatewayLatency] = await Promise.all([
interaction.deferReply().then(() => Date.now() - start),
interaction.client.rest.get("/gateway").then(() => Date.now() - start),
]);

return void interaction.editReply(`🏓 Interaction latency is \`${interactionLatency}ms\`, gateway latency is \`${gatewayLatency}ms\` and my uptime is \`${msToHumanShortTime(interaction.client.uptime)}\`.`);
},
} as FirstLevelChatInputCommand;
} satisfies FirstLevelChatInputCommand;
6 changes: 3 additions & 3 deletions src/commands/chatInput/self_destruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { buttonComponents } from "../../handlers/interactions/components";
export default {
name: "self_destruct",
description: "Delete the server",
worksIn: ["test-areas"],
applicableTo: ["workers"],
async execute(interaction) {
const testArea = await TestArea.findOne({ serverId: interaction.guildId });
const testArea = await TestArea.findOne({ guildId: interaction.guildId });
if (interaction.user.id !== testArea?.ownerId && interaction.user.id !== config.ownerId) return interaction.reply({ content: "❌ You are not the owner of this test area.", ephemeral: true });

buttonComponents.set(`${interaction.id}:confirm`, {
allowedUsers: [interaction.user.id],
async callback(button) {
await button.deferUpdate();
void button.guild.delete();
await testArea?.discordGuild?.delete();
},
});

Expand Down
16 changes: 6 additions & 10 deletions src/commands/chatInput/testarea/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type{ FirstLevelChatInputCommand } from "..";
import subcommandList from "./list";
import subcommandNew from "./new";
import subcommandRemove from "./remove";
import list from "./list";
import _new from "./new";
import remove from "./remove";

export default {
name: "testarea",
description: "Sub-command for administrating test areas",
worksIn: ["test-areas", "non-test-areas"],
subcommands: [
subcommandList,
subcommandNew,
subcommandRemove,
],
} as FirstLevelChatInputCommand;
applicableTo: ["main"],
subcommands: [list, _new, remove],
} satisfies FirstLevelChatInputCommand;
Loading

0 comments on commit 1e8c732

Please sign in to comment.