-
-
Notifications
You must be signed in to change notification settings - Fork 9
Fix Docker Compose & Add Boilerplate Project #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| * | ||
|
|
||
| !botcore/ | ||
| !docs/ | ||
| !tests/ | ||
|
|
||
| !pyproject.toml | ||
| !poetry.lock | ||
| !tox.ini |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,3 +134,6 @@ dmypy.json | |
|
|
||
| # Vscode | ||
| .vscode | ||
|
|
||
| # Development & prototyping environment | ||
| /bot/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| Local Development & Testing | ||
| =========================== | ||
|
|
||
| To test your features locally, there are a few possible approaches: | ||
|
|
||
| 1. Install your local copy of botcore into a pre-existing project such as bot | ||
| 2. Use the provided template from the :repo-file:`dev/bot <dev/bot>` folder | ||
|
|
||
| See below for more info on both approaches. | ||
|
|
||
| What's going to be common between them is you'll need to write code to test your feature. | ||
| This might mean adding new commands, modifying existing ones, changing utilities, etc. | ||
| The steps below should provide most of the groundwork you need, but the exact requirements will | ||
| vary by the feature you're working on. | ||
|
|
||
|
|
||
| Option 1 | ||
| -------- | ||
| 1. Navigate to the project you want to install bot-core in, such as bot or sir-lancebot | ||
| 2. Run ``pip install /path/to/botcore`` in the project's environment | ||
|
|
||
| - The path provided to install should be the root directory of this project on your machine. | ||
| That is, the folder which contains the ``pyproject.toml`` file. | ||
| - Make sure to install in the correct environment. Most Python Discord projects use | ||
| poetry, so you can run ``poetry run pip install /path/to/botcore``. | ||
|
|
||
| 3. You can now use features from your local bot-core changes. | ||
| To load new changes, run the install command again. | ||
|
|
||
|
|
||
| Option 2 | ||
| -------- | ||
| 1. Copy the :repo-file:`bot template folder <dev/bot>` to the root of the bot-core project. | ||
| This copy is going to be git-ignored, so you're free to modify it however you like. | ||
| 2. Run the project | ||
|
|
||
| - Locally: You can run it on your system using ``python -m bot`` | ||
| - Docker: You can run on docker using ``docker compose up -d bot``. | ||
|
|
||
| 3. Configure the environment variables used by the program. | ||
| You can set them in an ``.env`` file in the project root directory. The variables are: | ||
|
|
||
| - ``TOKEN`` (required): Discord bot token, with all intents enabled | ||
| - ``GUILD_ID`` (required): The guild the bot should monitor | ||
| - ``PREFIX``: The prefix to use for invoking bot commands. Defaults to mentions and ``!`` | ||
| - ``ALLOWED_ROLES``: A comma seperated list of role IDs which the bot is allowed to mention | ||
|
|
||
| 4. You can now test your changes. You do not need to do anything to reinstall the | ||
| library if you modify your code. | ||
|
|
||
| .. tip:: | ||
| The docker-compose included contains services from our other applications | ||
| to help you test out certain features. Use them as needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import asyncio | ||
| import logging | ||
| import os | ||
| import sys | ||
|
|
||
| import botcore | ||
|
|
||
| if os.name == "nt": | ||
| # Change the event loop policy on Windows to avoid exceptions on exit | ||
| asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
|
|
||
| # Some basic logging to get existing loggers to show | ||
| logging.getLogger().addHandler(logging.StreamHandler()) | ||
| logging.getLogger().setLevel(logging.DEBUG) | ||
| logging.getLogger("discord").setLevel(logging.ERROR) | ||
|
|
||
|
|
||
| class Bot(botcore.BotBase): | ||
| """Sample Bot implementation.""" | ||
|
|
||
| async def setup_hook(self) -> None: | ||
| """Load extensions on startup.""" | ||
| await super().setup_hook() | ||
| asyncio.create_task(self.load_extensions(sys.modules[__name__])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import asyncio | ||
| import os | ||
|
|
||
| import aiohttp | ||
| import discord | ||
| import dotenv | ||
| from discord.ext import commands | ||
|
|
||
| import botcore | ||
| from . import Bot | ||
|
|
||
| dotenv.load_dotenv() | ||
| botcore.utils.apply_monkey_patches() | ||
|
|
||
| roles = os.getenv("ALLOWED_ROLES") | ||
| roles = [int(role) for role in roles.split(",")] if roles else [] | ||
|
|
||
| bot = Bot( | ||
| guild_id=int(os.getenv("GUILD_ID")), | ||
| http_session=None, # type: ignore # We need to instantiate the session in an async context | ||
| allowed_roles=roles, | ||
| command_prefix=commands.when_mentioned_or(os.getenv("PREFIX", "!")), | ||
| intents=discord.Intents.all(), | ||
| description="Bot-core test bot.", | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Run the bot.""" | ||
| bot.http_session = aiohttp.ClientSession() | ||
| async with bot: | ||
| await bot.start(os.getenv("TOKEN")) | ||
|
|
||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from discord.ext import commands | ||
|
|
||
| from . import Bot | ||
|
|
||
|
|
||
| class Cog(commands.Cog): | ||
| """A simple discord.py cog.""" | ||
|
|
||
| def __init__(self, _bot: Bot): | ||
| self.bot = _bot | ||
|
|
||
| @commands.Cog.listener() | ||
| async def on_ready(self) -> None: | ||
| """Print a message when the client (re)connects.""" | ||
| print("Client is ready.") | ||
|
|
||
| @commands.command() | ||
| async def reload(self, ctx: commands.Context) -> None: | ||
| """Reload all available cogs.""" | ||
| message = await ctx.send(":hourglass_flowing_sand: Reloading") | ||
| for ext in list(self.bot.extensions): | ||
| await self.bot.reload_extension(ext) | ||
| await message.edit(content=":white_check_mark: Done") | ||
|
|
||
| @commands.command() | ||
| async def ping(self, ctx: commands.Context) -> None: | ||
| """Test if the bot is online.""" | ||
| await ctx.send("We are live!") | ||
|
|
||
|
|
||
| async def setup(_bot: Bot) -> None: | ||
| """Install the cog.""" | ||
| await _bot.add_cog(Cog(_bot)) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Modified version of python-discord/bot | ||
|
|
||
| version: "3.8" | ||
|
|
||
| x-restart-policy: &restart_policy | ||
| restart: unless-stopped | ||
|
|
||
| services: | ||
| postgres: | ||
| << : *restart_policy | ||
| image: postgres:13-alpine | ||
| environment: | ||
| POSTGRES_DB: pysite | ||
| POSTGRES_PASSWORD: pysite | ||
| POSTGRES_USER: pysite | ||
| healthcheck: | ||
| test: ["CMD-SHELL", "pg_isready -U pysite"] | ||
| interval: 2s | ||
| timeout: 1s | ||
| retries: 5 | ||
|
|
||
| redis: | ||
| << : *restart_policy | ||
| image: redis:5.0.9 | ||
| ports: | ||
| - "6379:6379" | ||
|
|
||
| snekbox: | ||
| << : *restart_policy | ||
| image: ghcr.io/python-discord/snekbox:latest | ||
| init: true | ||
| ipc: none | ||
| ports: | ||
| - "8060:8060" | ||
| privileged: true | ||
|
|
||
| web: | ||
| << : *restart_policy | ||
| image: ghcr.io/python-discord/site:latest | ||
| command: ["run", "--debug"] | ||
| ports: | ||
| - "8000:8000" | ||
| tty: true | ||
| environment: | ||
| DATABASE_URL: postgres://pysite:pysite@postgres:5432/pysite | ||
| METRICITY_DB_URL: postgres://pysite:pysite@postgres:5432/metricity | ||
| SECRET_KEY: suitable-for-development-only | ||
| STATIC_ROOT: /var/www/static | ||
|
|
||
| bot: | ||
| << : *restart_policy | ||
| build: | ||
| context: . | ||
| dockerfile: dev/Dockerfile | ||
| volumes: | ||
| - .:/app:ro | ||
| tty: true | ||
| env_file: | ||
| - .env | ||
| environment: | ||
| BOT_API_KEY: badbot13m0n8f570f942013fc818f234916ca531 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .. Stub file to expose the README to sphinx | ||
| .. include:: ../dev/README.rst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.