Skip to content

Commit

Permalink
Merge branch 'master' into feat/custom-image-support
Browse files Browse the repository at this point in the history
  • Loading branch information
flaree authored Nov 22, 2024
2 parents c3b6dff + 412bf68 commit 2ee907e
Show file tree
Hide file tree
Showing 46 changed files with 2,939 additions and 1,910 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
# binary file excludsions
*.png binary
*.tff binary
*.ttf binary
2 changes: 1 addition & 1 deletion .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.12
python-version: 3.13
- uses: actions/cache@v3
with:
path: ~/.cache/pre-commit
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ venv
# database
*sqlite3*
*.rdb
pgbackups

# static
static
Expand Down
9 changes: 6 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ files: .*\.py$

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
exclude: settings\.py
Expand All @@ -13,12 +13,15 @@ repos:
files: ""
- id: debug-statements
files: .*\.py$
- id: mixed-line-ending
args:
- --fix=lf
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.11.0
rev: 24.8.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args:
Expand Down
71 changes: 34 additions & 37 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ Using Docker:

1. Install Docker.
2. Run `docker compose build` at the root of this repository.
3. Create an `.env` file like this:

```env
BALLSDEXBOT_TOKEN=your discord token
POSTGRES_PASSWORD=a random string
```

4. Run `docker compose up -d postgres-db redis-cache`. This will not start the bot.
3. Run `docker compose up -d postgres-db redis-cache`. This will not start the bot, only the
database and redis server.

----

Expand All @@ -28,43 +22,51 @@ Export the appropriate environment variables as described in the

### Installing the dependencies

1. Get Python 3.10 and pip.
1. Get Python 3.13 and pip.
2. Install poetry with `pip install poetry`.
3. Run `poetry install`.
4. You may run commands inside the virtualenv with `poetry run ...`, or use `poetry shell`.
5. Set up your IDE Python version to the one from Poetry. The path to the virtualenv can
be obtained with `poetry show -v`.

## Running the code

Before running any command, you must be in the poetry virtualenv, with the following
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.

### Starting the bot

- `poetry shell`
- ```bash
BALLSDEXBOT_DB_URL="postgres://ballsdex:password@localhost:5432/ballsdex" \
python3 -m ballsdex --dev --debug
```
```bash
python3 -m ballsdex --dev --debug
```

Replace `password` with the same value as the one in the `.env` file.
If appropriate, you may also replace `localhost` and `5432` for the host and the port.
You can do `python3 -m ballsdex -h` to see the available options.

### Starting the admin panel

**Warning: You need to run migrations from the bot at least once before starting the admin
panel without the other components.**
**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
```

If you're not actively working on the admin panel, you can just do `docker compose up admin-panel`.
Otherwise, follow these instructions to directly have the process without rebuilding.
## Integrating your IDE

- `poetry shell`
- ```bash
BALLSDEXBOT_DB_URL="postgres://ballsdex:password@localhost:5432/ballsdex" \
BALLSDEXBOT_REDIS_URL="redis://127.0.0.1" \
python3 -m ballsdex --dev --debug
```
To have proper autocompletion and type checking, your IDE must be aware of your poetry virtualenv.

Once again, replace `password` with the same value as the one in the `.env` file.
If appropriate, you may also replace `localhost` and `5432` for the host and the port.
The path to Python can be obtained with `poetry env info -p`, copy that and configure your editor
to use it. Some editors like VS code may detect your poetry env automatically when picking
versions.

You can also install extensions to work with black, flake8 and pyright (Pylance for VS code).
Their configurations are already written in `pyproject.toml`, so it should work as-is.

## Migrations

Expand All @@ -77,13 +79,9 @@ When new migrations are available, you can either start the bot to run them auto
execute the following command:

```sh
BALLSDEXBOT_DB_URL="postgres://ballsdex:password@localhost:5432/ballsdex" \
aerich upgrade
```

Once again, replace `password` with the same value as the one in the `.env` file.
If appropriate, you may also replace `localhost` and `5432` for the host and the port.

### Creating new migrations

If you modified the models, `aerich` can automatically generate a migration file.
Expand All @@ -94,14 +92,13 @@ 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
BALLSDEXBOT_DB_URL="postgres://ballsdex:password@localhost:5432/ballsdex" \
aerich migrate
```

## Coding style

The repo is validating code with `flake8` and formatting with `black`. They can be setup as a
pre-commit hook to make them run before committing files:
The code is formatted by `black`, style verified by `flake8`, and static checked by `pyright`.
They can be setup as a pre-commit hook to make them run before committing files:

```sh
pre-commit install
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1

FROM python:3.12-bullseye
FROM python:3.13.0-bookworm

ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BallsDex Discord Bot

[![Discord server](https://discordapp.com/api/guilds/1049118743101452329/embed.png)](https://discord.gg/Qn2Rkdkxwc)
[![Discord server](https://img.shields.io/discord/1049118743101452329?color=7489d5&logo=discord&logoColor=ffffff)](https://discord.gg/Qn2Rkdkxwc)
[![Pre-commit](https://github.com/laggron42/BallsDex-DiscordBot/actions/workflows/pre-commit.yml/badge.svg)](https://github.com/laggron42/BallsDex-DiscordBot/actions/workflows/pre-commit.yml)
[![Issues](https://img.shields.io/github/issues/laggron42/BallsDex-DiscordBot)](https://github.com/laggron42/BallsDex-DiscordBot/issues)
[![discord.py](https://img.shields.io/badge/discord-py-blue.svg)](https://github.com/Rapptz/discord.py)
Expand Down
2 changes: 1 addition & 1 deletion ballsdex/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.19.1"
__version__ = "2.21.0"
9 changes: 8 additions & 1 deletion ballsdex/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CLIFlags(argparse.Namespace):
config_file: Path
reset_settings: bool
disable_rich: bool
disable_message_content: bool
debug: bool
dev: bool

Expand All @@ -58,6 +59,11 @@ def parse_cli_flags(arguments: list[str]) -> CLIFlags:
help="Reset the config file with the latest default configuration",
)
parser.add_argument("--disable-rich", action="store_true", help="Disable rich log format")
parser.add_argument(
"--disable-message-content",
action="store_true",
help="Disable usage of message content intent through the bot",
)
parser.add_argument("--debug", action="store_true", help="Enable debug logs")
parser.add_argument("--dev", action="store_true", help="Enable developer mode")
args = parser.parse_args(arguments, namespace=CLIFlags())
Expand All @@ -73,7 +79,7 @@ def reset_settings(path: Path):

def print_welcome():
print("[green]{0:-^50}[/green]".format(f" {settings.bot_name} bot "))
print("[green]{0: ^50}[/green]".format(f" Collect {settings.collectible_name}s "))
print("[green]{0: ^50}[/green]".format(f" Collect {settings.plural_collectible_name} "))
print("[blue]{0:^50}[/blue]".format("Discord bot made by El Laggron"))
print("")
print(" [red]{0:<20}[/red] [yellow]{1:>10}[/yellow]".format("Bot version:", bot_version))
Expand Down Expand Up @@ -288,6 +294,7 @@ def main():
command_prefix=when_mentioned_or(prefix),
dev=cli_flags.dev, # type: ignore
shard_count=settings.shard_count,
disable_messsage_content=cli_flags.disable_message_content,
)

exc_handler = functools.partial(global_exception_handler, bot)
Expand Down
19 changes: 15 additions & 4 deletions ballsdex/core/admin/resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import List
from typing import Any, List

from fastapi_admin.app import app
from fastapi_admin.enums import Method
Expand Down Expand Up @@ -161,6 +161,14 @@ class EconomyResource(Model):
]


class Emoji(displays.Display):
async def render(self, request: Request, value: Any):
return (
f'<img src="https://cdn.discordapp.com/emojis/{value}.png?size=40" '
f'title="ID: {value}" />'
)


@app.register
class BallResource(Model):
label = "Ball"
Expand All @@ -185,6 +193,7 @@ class BallResource(Model):
"country",
"short_name",
"catch_names",
"translations",
"created_at",
"regime",
"economy",
Expand All @@ -195,7 +204,8 @@ class BallResource(Model):
"tradeable",
Field(
name="emoji_id",
label="Emoji ID",
label="Emoji",
display=Emoji(),
),
Field(
name="wild_card",
Expand Down Expand Up @@ -252,7 +262,6 @@ class BallInstanceResource(Model):
),
filters.ForeignKey(model=Ball, name="ball", label="Ball"),
filters.ForeignKey(model=Special, name="special", label="Special"),
filters.Date(name="catch_date", label="Catch date"),
filters.Boolean(name="shiny", label="Shiny"),
filters.Boolean(name="favorite", label="Favorite"),
filters.Search(
Expand Down Expand Up @@ -319,7 +328,7 @@ class GuildConfigResource(Model):
placeholder="Filter by ID",
),
]
fields = ["guild_id", "spawn_channel", "enabled"]
fields = ["guild_id", "spawn_channel", "enabled", "silent"]


@app.register
Expand All @@ -345,6 +354,7 @@ class BlacklistedIDResource(Model):
fields = [
"discord_id",
"reason",
"date",
]


Expand All @@ -371,4 +381,5 @@ class BlacklistedGuildIDResource(Model):
fields = [
"discord_id",
"reason",
"date",
]
Loading

0 comments on commit 2ee907e

Please sign in to comment.