Skip to content

Commit

Permalink
Replace backend, add colorblind mode and stats
Browse files Browse the repository at this point in the history
* Removes Notion as datastore as it was very slow
* Introduces simple JSON file-based backend
* Add `/colorblind` command
* Add `/stats` command

Co-authored-by: Tiia Aurora <[email protected]>
  • Loading branch information
coldino and TiiaAurora committed Feb 2, 2022
1 parent 028ae88 commit 7d5546b
Show file tree
Hide file tree
Showing 14 changed files with 403 additions and 308 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
DISCORD_TOKEN=
NOTION_SECRET=
DB_ID=

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.pytest_cache/
/.venv/
.env
database.json
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"cSpell.words": [
"disnake",
"dotenv",
"pydantic",
"Wordle"
],
"python.analysis.typeCheckingMode": "basic"
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Discord Wordy Bot Changelog

All notable changes to this project will be documented in this file.

## Version [1.0.0] - 2022-02-01

- Initial release using Notion as a backend for the bot

## Version [2.0.0] - 2022-02-02

### Added

- Added color blind mode as a toggle when using `/colorblind`
- Added language statistics to the `/stats` command
- Added command `/show` to show your current game
- Added Discord status for bot on start

### Changed

- Changed database from Notion to a local json file
- Changed dependencies - removed `notion-client` and added `pydantic`

8 changes: 4 additions & 4 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ verify_ssl = true
name = "pypi"

[packages]
python-dotenv = "*"
disnake = "*"
notion-client = "*"
python-dotenv = "==0.19.2"
disnake = "~=2.3.0"
pydantic = "==1.9.0"

[dev-packages]
ipykernel = "*"
pytest = "*"
pytest = "~=6.2.5"

[requires]
python_version = "3.10"
116 changes: 50 additions & 66 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
# Wordy Discord Bot
Wordy is a Wordle-like Discord bot but with a twist. It already supports 6 languages from the beginning:
English, Italian, French, German, Norwegian (Bokmål) and Austrian.
Wordy also supports colorblind mode and saves it for each user seperately.

## The Bot

Wordy uses [Disnake](https://docs.disnake.dev/en/latest/) to connect to the Discord API. Disnake was chosen to support slash commands. You must create a bot and access token within Discord before proceeding, saving it in the `.env` file.

## The Backend

Wordy connects to a Notion Database to save scores. To set up your own database go to http://notion.so create an account
and workspace and an inline database. Create an integration on https://developers.notion.com/ and share the database with the integration.
The Database ID and the Notion access token should then be put into the `.env` file.

The following columns are needed:
- Title-column: User
- Number-column: DiscordID
- Text-column: LastLang
- Number-column: Win
- Number-column: Lose
- Number-column: Surrender

The bot uses the Notion database to record stats for the `/stat` command for for your entertainment.
The stats of the players and their games are saved in a json database file. The path to the database can be changed in the .env file's `DATABASE_PATH` variable.

Games can then be played in text-rooms and also per direct message to the bot itself.

Expand All @@ -37,4 +26,4 @@ pipenv sync --python 3.10
pipenv shell
```

Alternatively to install the dependencies in your global Python install use `pip install -U python-dotenv disnake notion-client`.
Alternatively to install the dependencies in your global Python install use `pip install -U python-dotenv disnake pydantic`.
43 changes: 33 additions & 10 deletions game_store.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
from pprint import pprint
from wordy_types import ActiveGame
import os
from typing import Optional

from json_db import JSONDatabase
from wordy_types import ActiveGame, UserInfo

_active_games: dict[int, ActiveGame] = {}

_db = JSONDatabase(os.getenv('DATABASE_PATH', 'database.json'))

def fetch_stored_game(id: int):
return _active_games.get(id, None)

def get_info_for_user(id: int):
try:
raw = _db[str(id)]
return UserInfo.parse_obj(raw)
except KeyError:
user = UserInfo()
_db[str(id)] = user.dict()
return user


def set_info_for_user(id: int, info: UserInfo):
_db[str(id)] = info.dict()


def fetch_stored_game(id: int) -> Optional[ActiveGame]:
user = get_info_for_user(id)
return user.current_game


def store_game(id: int, game: ActiveGame):
_active_games[id] = game
user = get_info_for_user(id)
user.current_game = game
set_info_for_user(id, user)


def clear_game(id: int):
try:
del _active_games[id]
except KeyError:
pass
user = get_info_for_user(id)
user.current_game = None
set_info_for_user(id, user)


def write_to_disk():
_db.save()
Loading

0 comments on commit 7d5546b

Please sign in to comment.