Skip to content

Commit

Permalink
Merge branch 'main' into trace-loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
onerandomusername committed Apr 21, 2022
2 parents c1022b4 + a89f5d6 commit 2db6223
Show file tree
Hide file tree
Showing 48 changed files with 4,334 additions and 688 deletions.
57 changes: 0 additions & 57 deletions .codecov.yml

This file was deleted.

1 change: 0 additions & 1 deletion .env.template

This file was deleted.

51 changes: 47 additions & 4 deletions .github/workflows/lint_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,14 @@ jobs:
# black and flake8 action. As pre-commit does not support user installs,
# we set PIP_USER=0 to not do a user install.
- name: Run pre-commit hooks
id: pre-commit
run: export PIP_USER=0; SKIP="no-commit-to-branch,black,flake8" pre-commit run --all-files

# Run black seperately as we don't want to reformat the files
# just error if something isn't formatted correctly.
- name: Check files with black
id: black
if: always() && (steps.pre-commit.outcome == 'success' || steps.pre-commit.outcome == 'failure')
run: black . --check --diff --color

# Run flake8 and have it format the linting errors in the format of
Expand All @@ -108,6 +111,8 @@ jobs:
# Format used:
# ::error file={filename},line={line},col={col}::{message}
- name: Run flake8
id: flake8
if: always() && (steps.pre-commit.outcome == 'success' || steps.pre-commit.outcome == 'failure')
run: "flake8 \
--format='::error file=%(path)s,line=%(row)d,col=%(col)d::\
[flake8] %(code)s: %(text)s'"
Expand Down Expand Up @@ -162,12 +167,50 @@ jobs:
# This is saved to ./.coverage to be used by codecov to link a
# coverage report to github.
- name: Run tests and generate coverage report
run: python -m pytest -n auto --dist loadfile --cov --disable-warnings -q
id: run_tests
run: python -m pytest tests -n auto --dist loadfile --cov --disable-warnings -q

# This step will publish the coverage reports to codecov.io and
# This step will publish the coverage reports to coveralls.io and
# print a "job" link in the output of the GitHub Action
- name: Publish coverage report to codecov.io
run: python -m codecov
- name: Publish coverage report to coveralls.io
# upload coverage even if a test run failed
# this is a test, and may be removed in the future
if: always() && (steps.run_tests.outcome == 'success' || steps.run_tests.outcome == 'failure')
# important that we don't fail the workflow when coveralls is down
continue-on-error: true
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_FLAG_NAME: coverage-${{ runner.os }}-python-${{ env.PYTHON_VERSION }}
COVERALLS_PARALLEL: true
COVERALLS_SERVICE_NAME: github
run: python -m coveralls

coveralls-finish:
name: Indicate completion to coveralls.io
runs-on: ubuntu-latest
needs: test
# we don't want to fail the workflow when coveralls is down
continue-on-error: true
# we always want to ensure we attempt to send a finish to coveralls
if: always()
steps:
# Set up a consistent version of Python
- name: Set up Python 3.9
id: python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Coveralls Finished
continue-on-error: true
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_SERVICE_NAME: github
# NOTE: this has a small thing where this will not always be the same with the poetry.lock file
# given how this is installed for one api request, its not worth pinning to me.
# any bugs caused by this can be solved when they occur
run: |
python3 -m pip install --upgrade coveralls
python3 -m coveralls --finish
artifact:
name: Generate Artifact
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ celerybeat.pid

# Environments
.env
!template.env
!/tests/modmail/test.env
.venv
env/
Expand Down Expand Up @@ -134,10 +135,11 @@ dmypy.json
.idea/

# logs
logs
/logs/

# Configuration
*config.toml
/modmail_config.toml
/modmail_config.yaml

# Custom docker compose override
docker-compose.override.yml
31 changes: 27 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
## Pre-commit setup

repos:
# its possible to put this at the bottom, but we want the pre-commit-hooks to check these files as well.
- repo: local
hooks:
- id: ensure-default-configuration-is-exported
name: Export default configuration
language: python
entry: poetry run python -m scripts.export_new_config_to_default_config
files: '(app\.json|template\.env|modmail\/(config\.py|default_config(\.toml|\.yaml)))$'
require_serial: true
additional_dependencies:
# so apparently these are needed, but the versions don't have to be pinned since it uses the local env
# go figure.
- atoml
- attrs
- click
- coloredlogs
- desert
- discord.py
- marshmallow
- python-dotenv
- pyyaml

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.2.0
hooks:
- id: check-case-conflict
- id: check-added-large-files
Expand All @@ -11,6 +33,7 @@ repos:
- id: check-yaml
exclude: 'mkdocs.yml' # Exclude all mkdocs.yml as they use tags i.e. `!!!`
- id: pretty-format-json
exclude: 'app.json'
args: [--indent=4, --autofix]
- id: end-of-file-fixer
- id: no-commit-to-branch
Expand Down Expand Up @@ -42,19 +65,19 @@ repos:
- id: python-use-type-annotations

- repo: https://github.com/PyCQA/isort
rev: 5.9.3
rev: 5.10.1
hooks:
- id: isort

- repo: https://github.com/asottile/blacken-docs
rev: v1.11.0
rev: v1.12.1
hooks:
- id: blacken-docs
additional_dependencies:
- black

- repo: https://github.com/psf/black
rev: 21.8b0
rev: 22.3.0
hooks:
- id: black
language_version: python3
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Discord Modmail

[![Lint & Test](https://img.shields.io/github/workflow/status/discord-modmail/modmail/Lint%20&%20Test/main?label=Lint+%26+Test&logo=github&style=flat)](https://github.com/discord-modmail/modmail/actions/workflows/lint_test.yml "Lint and Test")
[![Code Coverage](https://img.shields.io/codecov/c/gh/discord-modmail/modmail/main?logo=codecov&style=flat&label=Code+Coverage)](https://app.codecov.io/gh/discord-modmail/modmail "Code Coverage")
[![Codacy Grade](https://img.shields.io/codacy/grade/78be21a49835484595aea556d5920638?logo=codacy&style=flat&label=Code+Quality)](https://www.codacy.com/gh/discord-modmail/modmail/dashboard "Codacy Grade")
[![Code Coverage](https://img.shields.io/coveralls/github/discord-modmail/modmail?logo=coveralls&style=flat&label=Code+Coverage)](https://coveralls.io/github/discord-modmail/modmail)
[![Python](https://img.shields.io/static/v1?label=Python&message=3.8+%7C+3.9&color=blue&logo=Python&style=flat)](https://www.python.org/downloads/ "Python 3.8 | 3.9")
[![License](https://img.shields.io/github/license/discord-modmail/modmail?style=flat&label=License)](./LICENSE "License file")
[![Code Style](https://img.shields.io/static/v1?label=Code%20Style&message=black&color=000000&style=flat)](https://github.com/psf/black "The uncompromising python formatter")
Expand Down
9 changes: 7 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
{
"env": {
"TOKEN": {
"description": "Discord bot token. This is from https://discord.com/developers/applications",
"MODMAIL_BOT_TOKEN": {
"description": "Discord bot token. Required to log in to discord.\nThis is obtainable from https://discord.com/developers/applications",
"required": true
},
"MODMAIL_BOT_PREFIX": {
"description": "Command prefix.",
"required": false,
"value": "?"
}
},
"name": "Modmail Bot",
Expand Down
10 changes: 9 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
### Breaking
- Bot now requires a `RELAY_CHANNEL_ID` configuration variable. (#53)
- This is where tickets with users will be relayed.
- At a later point in time, this will be additionally included in a configuration command.

### Added
- Threads system (#53)
- Messages can now be relayed between a user and a server.
- NOTE: There is not a database yet, so none of these messages are stored.
- Added Dispatcher system, although it is not hooked into important features like thread creation yet. (#71)
- Officially support python 3.10 (#119)
- Officially support windows and macos (#121)
- Completely rewrote configuration system (#75)

### Changed

Expand Down
27 changes: 22 additions & 5 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ $ poetry install

### Set up modmail config

1. Create a copy of `config-default.yml` named `config.yml` in the the `modmail/` directory.
1. Create a copy of `modmail/default_config.toml` named `modmail_config.toml` in the root of the repository.

=== "Linux, macOS"

<div class="termy">

```console
$ cp -v modmail/config-default.toml modmail/config.toml
$ cp -v modmail/default_config.toml modmail_config.toml
```

</div>
Expand All @@ -169,20 +169,37 @@ $ poetry install
<div class="termy">

```console
$ xcopy /f modmail/config-default.toml modmail/config.toml
$ xcopy /f modmail/default_config.toml modmail_config.toml
```

</div>

!!! note
If you would like, there is optional support for yaml configuration. Make sure that pyyaml is installed with `poetry install --extras yaml`,
and copy modmail/defaults_config.yaml to modmail_config.yaml.

2. Set the modmail bot prefix in `bot.prefix`.
3. In case you are a contributor set `dev.mode.plugin_dev` and `dev.mode.develop` to `true`. The `develop` variable enables the developer bot extensions and `plugin_dev` enables plugin-developer friendly bot extensions.
4. Create a text file named `.env` in your project root (that's the base folder of your repository):
- You can also copy the `.env.template` file to `.env`
- You can also copy the `template.env` file to `.env`

!!!note
The entire file name is literally `.env`

5. Open the file with any text editor and write the bot token to the files in this format: `TOKEN="my_token"`.
5. Open the file with any text editor and write the bot token to the files in this format: `MODMAIL_BOT_TOKEN="my_token"`.


Of the several supported configuration sources, they are loaded in a specific priority. In decreasing priority:
- `os.environ`
- .env
- modmail_config.yaml (if PyYaml is installed and the file exists)
- modmail_config.toml (if the above file does not exist)
- defaults

Internally, the actual parsing order may not match the above,
but the end configuration object will have the above priorty.



### Run The Project

Expand Down
17 changes: 16 additions & 1 deletion modmail/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import asyncio
import logging
import logging.handlers
import os

import coloredlogs

from modmail import log


LOG_FILE_SIZE = 8 * (2 ** 10) ** 2 # 8MB, discord upload limit
try:
import dotenv
except ModuleNotFoundError:
pass
else:
dotenv.load_dotenv(".env")

# On windows aiodns's asyncio support relies on APIs like add_reader (which aiodns uses)
# are not guaranteed to be available, and in particular are not available when using the
# ProactorEventLoop on Windows, this method is only supported with Windows SelectorEventLoop
if os.name == "nt":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

LOG_FILE_SIZE = 8 * (2**10) ** 2 # 8MB, discord upload limit


ROOT_LOG_LEVEL = log.get_logging_level()
Expand Down
2 changes: 1 addition & 1 deletion modmail/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def main() -> None:
"""Run the bot."""
patch_embed()
bot = ModmailBot()
bot.run(bot.config.bot.token)
bot.run(bot.config.user.bot.token)


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 2db6223

Please sign in to comment.