Skip to content

Commit

Permalink
Merge branch 'master' into uptime-datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
laggron42 authored Feb 21, 2025
2 parents e9c66e8 + 38a6703 commit 04f5bdd
Show file tree
Hide file tree
Showing 83 changed files with 1,906 additions and 909 deletions.
30 changes: 0 additions & 30 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

103 changes: 103 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Bug report
description: Create a bug report
labels:
- bug
body:
- type: markdown
attributes:
value: >
Thank you for reporting a bug!
Before filling this, try to contact us about the issue you have.
### If you are using the official Ballsdex bot
Join the [official Discord server](https://discord.gg/ballsdex) and create a post in the
[`bug-reports` forum](https://discord.com/channels/1049118743101452329/1049622402419675206).
### If you are hosting your own bot
Join the [developers server](https://discord.gg/PKKhee4fvy) and ask for help in the
`#help` channel.
---
If you are confident that you have found a bug, or were told to fill an issue, please fill
in the following information.
- type: dropdown
id: category
attributes:
label: Category
description: Where does the bug comes from
multiple: true
options:
- Django (admin panel and more)
- Admin (/admin commands)
- Balls (/balls command)
- Config (/config command)
- Countryballs (spawn algorithm and countryball catching)
- Info (/about and /help commands)
- Players (/player command)
- Trade (/trade command)
- Core (main code, autocompletions, models, anything you may find common to all commands)
- Other
validations:
required: true

- type: textarea
id: description
attributes:
label: Description
description: A clear and concise description of what the bug is.
validations:
required: true

- type: textarea
id: steps
attributes:
label: Steps to reproduce
description: Steps to reproduce the behavior.
placeholder: |
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
validations:
required: false

- type: textarea
id: expected
attributes:
label: Expected behavior
description: A clear and concise description of what you expected to happen.
validations:
required: false

- type: textarea
id: screenshots
attributes:
label: Screenshots
description: If relevant, include screenshots showing the problems.
placeholder: You can drag and drop images, or paste them.
validations:
required: false

- type: input
id: version
attributes:
label: Version
description: If self-hosting, inlcude the version of the bot you are using
placeholder: 2.24.2
validations:
required: false

- type: textarea
id: stack-trace
attributes:
label: Error stack trace
description: >
If self hosting and the bot produces an error, please copy the whole error
from your logs and paste them here
render: python traceback
validations:
required: false
14 changes: 0 additions & 14 deletions .github/ISSUE_TEMPLATE/feature_request.md

This file was deleted.

44 changes: 44 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Feature request
description: Suggest an idea for Ballsdex
labels:
- feature request
body:
- type: markdown
attributes:
value: >
Thank you for suggesting new features!
Before filling this, try to contact us about the idea you have.
### If you are using the official Ballsdex bot
Join the [official Discord server](https://discord.gg/ballsdex) and create a post in the
[`suggestions` forum](https://discord.com/channels/1049118743101452329/1049164568066281472).
### If you are hosting your own bot
Join the [developers server](https://discord.gg/PKKhee4fvy) and create a post in the
`suggestions` forum.
---
Then, please fill the following information.
- type: dropdown
id: selfhosting
attributes:
label: Self-hosting
description: Are you using the official Ballsdex bot or hosting your own instance?
options:
- Official Ballsdex bot
- Self-hosting
validations:
required: true

- type: textarea
id: description
attributes:
label: Description
description: A clear and concise description of what the feature is.
validations:
required: true
1 change: 1 addition & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: retke
24 changes: 24 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
### Description of the changes

<!--
Describe the changes you have made in this pull request.
Feel free to include screenshots of the new feature if applicable!
If the changes resolve an issue, please include "Resolves #XX" where "XX" is the issue number.
-->

### Were the changes in this PR tested?

<!--
Answer yes or no if you tested your changes locally.
If your change is only grammatical and doesn't change any logic, choose "Yes".
-->
Yes
No

<!--
If the change you introduced is big enough, make a list of checkboxes with all different
cases to test. You can also request additional help for testing thoroughly.
-->
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ repos:
- id: pyright
language_version: python3.13
additional_dependencies:
- discord
- discord.py==2.5.0
- cachetools
- rich
- Pillow
- prometheus_client
- tortoise-orm
- aerich==0.6.3
- redis
- django
- dj_database_url
Expand Down
62 changes: 34 additions & 28 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ Using Docker:

1. Install Docker.
2. Run `docker compose build` at the root of this repository.
3. Run `docker compose up -d postgres-db redis-cache`. This will not start the bot, only the
3. Run `docker compose up -d postgres-db`. This will not start the bot, only the
database and redis server.

----

Without docker, check how to install and setup PostgreSQL and Redis-server on your OS.
Without docker, check how to install and setup PostgreSQL on your OS.
Export the appropriate environment variables as described in the
[README](README.md#without-docker).

Expand All @@ -35,7 +35,6 @@ environment variables exported:
```bash
poetry shell
export BALLSDEXBOT_DB_URL="postgres://ballsdex:defaultballsdexpassword@localhost:5432/ballsdex"
export BALLSDEXBOT_REDIS_URL="redis://127.0.0.1"
```

If needed, feel free to change the host, port, user or password of the database or redis server.
Expand All @@ -50,13 +49,31 @@ You can do `python3 -m ballsdex -h` to see the available options.

### Starting the admin panel

**Warning: You need to run migrations at least once before starting the admin
panel without the other components.** You can either run the bot once or do `aerich upgrade`.

```bash
uvicorn ballsdex.core.admin:_app --host 0.0.0.0 --reload
cd admin_panel
export DJANGO_SETTINGS_MODULE=admin_panel.settings.dev
python3 manage.py migrate
python3 manage.py collectstatic --no-input
uvicorn --reload --reload-include "*.html" admin_panel.asgi:application
```

You will be running the admin panel with additional debug tools. There is the django debug
toolbar to inspect SQL queries, loading times, template loading and other tools. You also get
pyinstrument, allowing you to profile a page by appending `?profile` at the end.

> [!TIP]
> `python3 manage.py` contains a lot of commands, feel free to explore them! To name a few:
>
> - `shell` launches a Python REPL ready to interact with models and database
> - `dbshell` will launch `psql` with the right settings for the database
> - `check` performs general system checks to ensure everything works
> - `createsuperuser` creates a superuser account
> - `showmigrations` shows the applied/missing migrations
> [!WARNING]
> Do not use `python3 manage.py runserver` to run the server, since the bot relies on async code.
> Django must be started with an ASGI server, not the default WSGI.
## Integrating your IDE

To have proper autocompletion and type checking, your IDE must be aware of your poetry virtualenv.
Expand All @@ -70,30 +87,16 @@ Their configurations are already written in `pyproject.toml`, so it should work

## Migrations

When modifying the Tortoise models, you need to create a migration file to reflect the changes
everywhere. For this, we're using [aerich](https://github.com/tortoise/aerich).

### Applying the changes from remote

When new migrations are available, you can either start the bot to run them automatically, or
execute the following command:

```sh
aerich upgrade
```
If you are modifying models definition, you need migrations to update the database schema.

### Creating new migrations
First, synchronize your changes between `ballsdex/core/models.py` and
`admin_panel/bd_models/models.py`, they must be identical!

If you modified the models, `aerich` can automatically generate a migration file.
Then you can run `python3 manage.py makemigrations` to generate a migration file. Re-read its
contents to ensure there is only what you modified, and commit it.

**You need to make sure you have already ran previous migrations, and that your database
is not messy!** Aerich's behaviour can be odd if not in ideal conditions.

Execute the following command to generate migrations, and push the created files:

```sh
aerich migrate
```
You can read more about migrations
[here](https://docs.djangoproject.com/en/5.1/topics/migrations/), the engine is very extensive!

## Coding style

Expand All @@ -109,3 +112,6 @@ You can also run them manually:
```sh
pre-commit run -a
```

All rules are defined in `pyproject.toml`, meaning your editor will pick them up if you install
the right tools.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ COPY . /code
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi

# wait for postgres to be ready
CMD sleep 2
CMD ["sleep", "2"]
19 changes: 19 additions & 0 deletions admin_panel/admin_panel/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from typing import TYPE_CHECKING, Callable

if TYPE_CHECKING:
from django.http import HttpRequest, HttpResponse


class SecurityHeadersMiddleware:
def __init__(self, get_response: Callable[["HttpRequest"], "HttpResponse"]):
self.get_response = get_response

def __call__(self, request: "HttpRequest") -> "HttpResponse":
response = self.get_response(request)
response["Content-Security-Policy"] = (
"default-src 'self' http://*.discordapp.com http://discord.com; frame-ancestors 'self'"
)
response["X-XSS-Protection"] = "1; mode=block"
response["X-Robots-Tag"] = "none"
response["Referrer-Policy"] = "same-origin"
return response
15 changes: 14 additions & 1 deletion admin_panel/admin_panel/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@

from ballsdex.settings import read_settings, settings

read_settings(Path("../config.yml"))
try:
read_settings(Path("../config.yml"))
except FileNotFoundError:
from rich import print

print(
"[yellow][bold]Could not find ../config.yml file.[/bold] "
"Please run the bot once to generate this file.[/yellow]"
)

BASE_DIR = Path(__file__).resolve().parent.parent


SECRET_KEY = None

# WARNING: DO NOT ADD EXTERNAL HOSTS HERE!!
# If you want to expose your admin panel online, please follow this tutorial:
# https://github.com/Ballsdex-Team/BallsDex-DiscordBot/wiki/Serving-the-admin-panel-online
# THIS HAS SECURITY IMPLICATIONS, ENABLES PRIVILEGE ESCALATION AND REMOTE CODE EXECUTION

ALLOWED_HOSTS = [
"localhost",
"127.0.0.1",
Expand Down
Loading

0 comments on commit 04f5bdd

Please sign in to comment.