Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr_refactor'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main.py
#	src/module/button.py
#	src/resources/const.py
  • Loading branch information
TLNBS2405 committed Mar 11, 2024
2 parents e28c6d7 + 7848228 commit db89205
Show file tree
Hide file tree
Showing 128 changed files with 186,081 additions and 7,006 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest
pip install lxml
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
python -m pip install -e .[dev]
- name: Run pre-commit checks
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
pre-commit run --all
- name: Test with pytest
run: |
python -m pytest
pytest
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
src/resources/config.json
src/json_movelist/
!src/json_movelist/empty.json
*.json
!**/static/**

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
34 changes: 34 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.2.1
hooks:
# Run the linter.
- id: ruff
args: [ "--select", "I", "--fix" ]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
language: system
pass_filenames: false
- repo: local
hooks:
- id: pytest-check
stages: [push]
types: [python]
name: pytest-check
entry: python -m pytest
language: system
pass_filenames: false
always_run: true
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ A Discord bot to receive Tekken 8 frame data primarily from [Wavu Wiki](https://
Clone this repository to a Linux server that has Python 3.10.0+ and install the dependencies with:

```bash
git clone [email protected]:TLNBS2405/heihachi.git
git clone [email protected]:AbhijeetKrishnan/heihachi.git
cd heihachi
python3 -m pip install -r requirements.txt
python3 -m pip install .
```
### Config

The Heihachi bot is configured using the `src/resources/config.json` file. A sample file is provided in `src/resources/config.sample.json`. You should copy this file to `src/resources/config.json` and fill in the required fields.
The Heihachi bot is configured using a `config.json` file. A sample file is provided in `static/config.sample.json`.

```json
{
"DISCORD_TOKEN": "YOUR_DISCORD_TOKEN",
"FEEDBACK_CHANNEL_ID": "feedback_channel_id",
"ACTION_CHANNEL_ID": "action_channel_id"
"ACTION_CHANNEL_ID": "action_channel_id",
"BLACKLIST": ["user1", "user2"],
"ID_BLACKLIST": [0, 1]
}
```
You can obtain your own Discord token by creating a Discord bot ([instructions](https://github.com/reactiflux/discord-irc/wiki/Creating-a-discord-bot-&-getting-a-token)).
You can obtain your own Discord token by creating a Discord bot ([instructions](https://discordpy.readthedocs.io/en/stable/discord.html)).
The bot must have the "Message Content Intent" enabled on the Discord Developer Portal. The bot uses a permissions integer
of 551903315968 (Send Messages, Embed Links, Use Slash Commands, Use Embedded Activities).

The `FEEDBACK_CHANNEL_ID` is the channel where the bot will send feedback messages. The bot supports the slash command `/fd feedback <message>` to allow users to provide feedback on the bot's operation or frame data, and have the bot repost it in a dedicated channel for easier tracking.
The `FEEDBACK_CHANNEL_ID` is the channel where the bot will send feedback messages. The bot supports the slash command `/feedback <message>` to allow users to provide feedback on the bot's operation or frame data, and have the bot repost it in a dedicated channel for easier tracking.

![Feedback](/assets/feedback_example.png)

Expand All @@ -34,14 +38,16 @@ The `ACTION_CHANNEL_ID` is the channel where the bot will send "actioned" messag

Channel IDs can be obtained by right-clicking on a channel and selecting "Copy Channel ID" at the very bottom.

### Running the bot
_The bot must have permission to read and send messages in the feedback and action channels._

The `BLACKLIST` and `ID_BLACKLIST` are lists of user IDs and channel IDs respectively, who are not allowed to use the bot. This is useful for blacklisting users who abuse the bot or are otherwise not welcome.

The executable is `src/main.py`. Don't forget to put this project into your `PYTHONPATH`!
### Running the bot

Execute the below command from the project's root directory -

```bash
PYTHONPATH=. python3 src/main.py
python3 src/main.py path/to/config.json --export_dir path/to/export/dir
```

## Commands
Expand All @@ -50,6 +56,19 @@ The bot supports the following slash commands -

| Command | Description |
| --- | --- |
| `/fd <character> <move>` | Get frame data of a move from a character |
| `/<character> <move>` | Same as above |
| `/fd <character> <move>` | Get frame data of a particular character's move |
| `/feedback <message>` | Send feedback to the bot owner |

## Testing

To install the development dependencies, run -

```bash
python3 -m pip install -e .[dev]
```

The bot uses `pytest` for testing. To run the tests, execute the below command from the project's root directory -

```bash
python3 -m pytest
```
75 changes: 75 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "heihachi"
version = "2.1.0"
description = "A Discord bot to receive Tekken 8 frame data primarily from Wavu Wiki"
readme = "README.md"
authors = [
{ name = "TLNBS2405", email = "[email protected]" },
{ name = "Abhijeet Krishnan", email = "[email protected]" },
]
license = {file = "LICENSE"}
classifiers = [
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
requires-python = ">= 3.10"
dependencies = [
"discord.py",
"beautifulsoup4",
"Requests",
"lxml",
"fast-autocomplete[levenshtein]"
]

[project.urls]
Repository = "https://github.com/AbhijeetKrishnan/heihachi"
"Bug Tracker" = "https://github.com/AbhijeetKrishnan/heihachi/issues"

[project.optional-dependencies]
dev = [
"pre-commit",
"mypy",
"pytest",
"types-requests"
]

[tool.ruff]
target-version = "py310"
line-length = 127
src = ["src"]

[tool.mypy]
python_version = "3.10"
strict = true
warn_return_any = true
warn_unused_configs = true
warn_redundant_casts = true
warn_unreachable = true
pretty = true
exclude = [
"build",
"dist",
"venv",
"tests",
]
files = [
"src/**/*.py",
]
ignore_missing_imports = true

[tool.pytest.ini_options]
minversion = "6.0"
addopts = [
"-ra -q",
"--import-mode=importlib"
]
testpaths = [
"src/**/tests"
]
console_output_style = "progress"
6 changes: 0 additions & 6 deletions requirements.txt

This file was deleted.

Empty file removed src/__init__.py
Empty file.
2 changes: 2 additions & 0 deletions src/frame_service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .json_directory import JsonDirectory as JsonDirectory
from .wavu import Wavu as Wavu
1 change: 1 addition & 0 deletions src/frame_service/json_directory/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .json_directory import JsonDirectory as JsonDirectory
64 changes: 64 additions & 0 deletions src/frame_service/json_directory/json_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import json
import logging
import os

import requests

from framedb import Character, CharacterName, FrameService, Move, Url

logger = logging.getLogger("main")


class JsonDirectory(FrameService):
def __init__(self, char_meta_dir: str, movelist_dir: str) -> None:
self.name = f"JSON Directory ({movelist_dir})"
self.icon = None
self.movelist_dir = movelist_dir

try:
with open(char_meta_dir, "r") as f:
self.character_meta = json.load(f)
except Exception as e:
raise Exception(f"Could not load character meta data from {char_meta_dir}") from e

def get_frame_data(self, character: CharacterName, session: requests.Session | None = None) -> Character:
# TODO: duplicated across wavu and json_directory -> refactor
target_char_meta = None
for char_meta in self.character_meta:
if char_meta["name"] == character.value:
target_char_meta = char_meta
break
if target_char_meta is None:
raise Exception(f"Could not find character meta data for {character.value}")

name = CharacterName(target_char_meta["name"])
portrait = target_char_meta["portrait"]
page = target_char_meta["page"]

filepath = os.path.abspath(os.path.join(self.movelist_dir, f"{character.value}.json"))
with open(filepath, encoding="utf-8") as move_file:
move_file_contents = json.load(move_file)
movelist = {
move["id"]: Move(
id=move["id"],
name=move["name"],
input=move["input"],
target=move["target"],
damage=move["damage"],
on_block=move["on_block"],
on_hit=move["on_hit"],
on_ch=move["on_ch"],
startup=move["startup"],
recovery=move["recovery"],
notes=move["notes"],
image=move["image"],
video=move["video"],
alias=move["alias"],
)
for move in move_file_contents
}
char = Character(name, portrait, movelist, page)
return char

def get_move_url(self, character: Character, move: Move) -> Url | None:
return None
Loading

0 comments on commit db89205

Please sign in to comment.