Skip to content

Commit

Permalink
feat: v3 (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
promise authored Dec 19, 2024
1 parent 361d577 commit 7b46238
Show file tree
Hide file tree
Showing 45 changed files with 806 additions and 570 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
7 changes: 7 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,12 @@ jobs:
- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Mock .env
run: |
echo "CLIENT_TOKEN=hawk" > .env
echo "WORKER_TOKENS=tuah" >> .env
echo "DATABASE_URI=mock" >> .env
echo "OWNER_ID=mock" >> .env
- name: Run Jest
run: pnpm test
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.
11 changes: 0 additions & 11 deletions README.md

This file was deleted.

7 changes: 3 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
version: '3'

services:
bot:
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
- DISABLE_FUNNY_WORKER_NAMES=$DISABLE_FUNNY_WORKER_NAMES
volumes:
- ./logs:/app/logs
depends_on:
- db
db:
image: mongo:4@sha256:52c42cbab240b3c5b1748582cc13ef46d521ddacae002bbbda645cebed270ec0
image: mongo:8
restart: always
volumes:
- ./database:/data/db
95 changes: 95 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[![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)
[![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)

# Test Area

> *Endlessly tired of making and deleting test servers and having to enter that painful two-factor code? Pssh, that was so 2020...*
Test Area is a self-hosted Discord bot that creates temporary servers for you to test your other bots on.

## Motivation

This started as a side-gig in April 2021 after I got tired of making a lot of testing servers, then wanting to clean them up, only having to insert that bloody two-factor code.

<picture>
<source media="(prefers-color-scheme: dark)" srcset="./videos/pain-dark.gif">
<img src="./videos/pain-light.gif">
</picture>

Nowadays we have security keys, but the pain is still there. Wouldn't it be nice to just ... delete using a simple command? Maybe like this?

<picture>
<source media="(prefers-color-scheme: dark)" srcset="./videos/self-destruct-dark.gif">
<img src="./videos/self-destruct-light.gif">
</picture>

So, here we are.

## Prerequisites

You only need Docker (and Docker Compose) to self-host this bot.

## Installation

Test Area comes in a Docker image: `ghcr.io/biaw/test-area`, which it makes it really simple to set up using Docker.

1. You will need at least two bots set up.
- The first bot will be your main bot. Set it up like this:
- Installation: Set to User Install only, without an install link.
- Bot: Make sure it is private. Reset the token and save it.
- The second one will be your worker bot. Set it up like this:
- Installation: Set to Guild Install only, without an install link.
- Bot: Make sure it is private, and that all intents are enabled. Reset the token and save it.
2. Copy the `example.env` file to `.env` in a new folder, and fill in its values. The first bot token goes in `CLIENT_TOKEN`, and the second bot token goes in `WORKER_TOKENS`. The `OWNER_ID` is your Discord user ID. The rest is optional.
3. Copy the following `docker-compose.yml` file to the same folder:

```yaml
services:
bot:
image: ghcr.io/biaw/test-area:latest
restart: always
env_file: .env
environment:
- 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
- DISABLE_FUNNY_WORKER_NAMES=$DISABLE_FUNNY_WORKER_NAMES
volumes:
- ./logs:/app/logs
depends_on:
- db
db:
image: mongo:8
restart: always
volumes:
- ./database:/data/db
```
4. Run `docker-compose up -d` in the folder where you saved the `docker-compose.yml` file.
5. Invite the user bot to your server using this link: https://canary.discord.com/oauth2/authorize?client_id=YOUR_BOT_ID
6. You're done! You can now use the bot using the main `/testarea` command.

## FAQ

### Why do I need two bots?

A simple answer is that a Discord bot can only create new guilds when it's in less than 10 guilds. This is a limitation set by Discord. We've therefore implemented the simplest workaround possible: a second bot dedicated in creating guilds.

### Can I have more than 10 test areas?

Yes, you can. A worker bot can only create 10 guilds total, so if you want more then you will need to add a second worker. If you want more than 20, add a third worker, and so on.

You can do this by supplying multiple worker tokens in the `WORKER_TOKENS` environment variable, separated by a comma.

### Why does my worker bots have new names, like `harsh handsaw#7622` or `sumptous titanium#4604`?

When initializing a new worker, the worker needs a name for logging. So, naturally, as any chaotic evil programmer would do, I decided to add a name generator based on the bot ID.

However, you can disable this by setting the environment variable `DISABLE_FUNNY_WORKER_NAMES` to `true`.
Binary file added docs/videos/pain-dark.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/videos/pain-light.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/videos/self-destruct-dark.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/videos/self-destruct-light.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
CLIENT_ID=
CLIENT_TOKEN=

WORKER_TOKENS=

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

OWNER_ID=

THEME_COLOR=
# optional below
THEME_COLOR=#ffffff #(default blurple color)
AREA_LIMIT_PER_USER=#1 #(default 10, does not count for the owner)
DISABLE_FUNNY_WORKER_NAMES=#true #(default false)
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
Loading

0 comments on commit 7b46238

Please sign in to comment.