From 58548127e9d90d1f1787fab059bd439a7865d7fb Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:10:42 +0200 Subject: [PATCH 01/19] :truck: Renamed app folder to drawbot --- .gitignore | 8 ++++---- {app => drawbot}/.gitignore | 0 {app => drawbot}/__init__.py | 0 {app => drawbot}/bot.py | 8 ++++---- {app => drawbot}/cogs/informations.py | 4 ++-- {app => drawbot}/cogs/pronote.py | 4 ++-- {app => drawbot}/utils/__init__.py | 0 {app => drawbot}/utils/json_files.py | 6 +++--- {app => drawbot}/utils/pronote.py | 2 +- run.py | 2 +- 10 files changed, 17 insertions(+), 17 deletions(-) rename {app => drawbot}/.gitignore (100%) rename {app => drawbot}/__init__.py (100%) rename {app => drawbot}/bot.py (86%) rename {app => drawbot}/cogs/informations.py (96%) rename {app => drawbot}/cogs/pronote.py (98%) rename {app => drawbot}/utils/__init__.py (100%) rename {app => drawbot}/utils/json_files.py (89%) rename {app => drawbot}/utils/pronote.py (98%) diff --git a/.gitignore b/.gitignore index 8b912e3..2dd45e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ venv .idea -app/pronote.json -app/config.json -app/devoirs.json -app/grades.json +drawbot/pronote.json +drawbot/config.json +drawbot/devoirs.json +drawbot/grades.json __pycache__ /desktop.ini diff --git a/app/.gitignore b/drawbot/.gitignore similarity index 100% rename from app/.gitignore rename to drawbot/.gitignore diff --git a/app/__init__.py b/drawbot/__init__.py similarity index 100% rename from app/__init__.py rename to drawbot/__init__.py diff --git a/app/bot.py b/drawbot/bot.py similarity index 86% rename from app/bot.py rename to drawbot/bot.py index 291ddef..e6126bc 100644 --- a/app/bot.py +++ b/drawbot/bot.py @@ -4,8 +4,8 @@ from discord.ext import commands -from app import JsonData -from app.utils import json_wr +from drawbot import JsonData +from drawbot.utils import json_wr class Bot(commands.Bot): @@ -40,9 +40,9 @@ def __init__(self) -> None: self.remove_command("help") - for filename in os.listdir("app/cogs"): + for filename in os.listdir("drawbot/cogs"): if filename.endswith(".py"): - self.load_extension(f"app.cogs.{filename[:-3]}") + self.load_extension(f"drawbot.cogs.{filename[:-3]}") def run(self) -> None: super().run(self._token) diff --git a/app/cogs/informations.py b/drawbot/cogs/informations.py similarity index 96% rename from app/cogs/informations.py rename to drawbot/cogs/informations.py index cfb98ae..277f52c 100644 --- a/app/cogs/informations.py +++ b/drawbot/cogs/informations.py @@ -4,9 +4,9 @@ from discord.ext.commands import Context -from app import JsonDict +from drawbot import JsonDict -from app.utils import json_wr +from drawbot.utils import json_wr class Informations(commands.Cog): diff --git a/app/cogs/pronote.py b/drawbot/cogs/pronote.py similarity index 98% rename from app/cogs/pronote.py rename to drawbot/cogs/pronote.py index a979d08..9bc956c 100644 --- a/app/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -5,8 +5,8 @@ from discord.ext.commands import Context from discord.ext import commands, tasks -from app import JsonDict -from app.utils import json_wr, fetch_homeworks, fetch_grades +from drawbot import JsonDict +from drawbot.utils import json_wr, fetch_homeworks, fetch_grades class Pronote(commands.Cog): diff --git a/app/utils/__init__.py b/drawbot/utils/__init__.py similarity index 100% rename from app/utils/__init__.py rename to drawbot/utils/__init__.py diff --git a/app/utils/json_files.py b/drawbot/utils/json_files.py similarity index 89% rename from app/utils/json_files.py rename to drawbot/utils/json_files.py index 3b35b00..88fe465 100644 --- a/app/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -2,7 +2,7 @@ import os from typing import Optional, Literal -from app import JsonData +from drawbot import JsonData def json_wr( @@ -15,7 +15,7 @@ def json_wr( Parameters ---------- filename : str - Name of the file. It opens the file like "app/" + filename + ".json". + Name of the file. It opens the file like "drawbot/" + filename + ".json". mode : "r", "w", default="r" Action to perform in the file. "r" to load data, "w" to write in the file. @@ -31,7 +31,7 @@ def json_wr( if data is None: data = {} - filename = f"./app/{filename}.json" + filename = f"./drawbot/{filename}.json" if mode == "r": if not os.path.isfile(filename): diff --git a/app/utils/pronote.py b/drawbot/utils/pronote.py similarity index 98% rename from app/utils/pronote.py rename to drawbot/utils/pronote.py index 9dd66f9..2f0f5a0 100644 --- a/app/utils/pronote.py +++ b/drawbot/utils/pronote.py @@ -1,7 +1,7 @@ from typing import DefaultDict, Generator, Optional from collections import defaultdict -from app.utils import json_wr +from drawbot.utils import json_wr import pronotepy diff --git a/run.py b/run.py index 677907c..b363776 100644 --- a/run.py +++ b/run.py @@ -1,4 +1,4 @@ -from app.bot import Bot +from drawbot.bot import Bot def main() -> None: From 77460af34da67d04b412bd3b3149e36ce1a0d774 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:26:34 +0200 Subject: [PATCH 02/19] :see_no_evil: Updated gitignores --- .gitignore | 6 +++--- drawbot/cogs/.gitignore | 1 + vars/.gitignore | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 drawbot/cogs/.gitignore create mode 100644 vars/.gitignore diff --git a/.gitignore b/.gitignore index 2dd45e6..1517da8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ venv .idea drawbot/pronote.json -drawbot/config.json -drawbot/devoirs.json -drawbot/grades.json + __pycache__ /desktop.ini + +*.egg-info \ No newline at end of file diff --git a/drawbot/cogs/.gitignore b/drawbot/cogs/.gitignore new file mode 100644 index 0000000..46ce8a2 --- /dev/null +++ b/drawbot/cogs/.gitignore @@ -0,0 +1 @@ +dev.py diff --git a/vars/.gitignore b/vars/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/vars/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore From 58e64db6f36561902cb743e8252551eaaade2129 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:27:12 +0200 Subject: [PATCH 03/19] :package: Converting bot to a simple package --- setup.cfg | 4 ++++ setup.py | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..95aab77 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[metadata] +name = drawbot +version = 2.0.0 +author = Drawbu diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6b40b52 --- /dev/null +++ b/setup.py @@ -0,0 +1,4 @@ +from setuptools import setup + +if __name__ == '__main__': + setup() From b88ef7d1c139b8aa95377999f02233c82887226f Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:27:48 +0200 Subject: [PATCH 04/19] :truck: Added package entry point --- run.py => drawbot/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename run.py => drawbot/__main__.py (83%) diff --git a/run.py b/drawbot/__main__.py similarity index 83% rename from run.py rename to drawbot/__main__.py index b363776..047998e 100644 --- a/run.py +++ b/drawbot/__main__.py @@ -1,4 +1,4 @@ -from drawbot.bot import Bot +from drawbot import Bot def main() -> None: From 6779b946b9e306fcea2bddcba329b7dc82d1690b Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:28:23 +0200 Subject: [PATCH 05/19] :adhesive_bandage: Sorted types & imports --- drawbot/__init__.py | 6 ++---- drawbot/bot.py | 6 +++--- drawbot/cogs/informations.py | 5 ++--- drawbot/cogs/pronote.py | 4 ++-- drawbot/types.py | 5 +++++ drawbot/utils/json_files.py | 4 ++-- drawbot/utils/pronote.py | 3 +-- 7 files changed, 17 insertions(+), 16 deletions(-) create mode 100644 drawbot/types.py diff --git a/drawbot/__init__.py b/drawbot/__init__.py index 7782fa6..2635fd3 100644 --- a/drawbot/__init__.py +++ b/drawbot/__init__.py @@ -1,9 +1,7 @@ -from typing import Dict, Any, List, Union +from .bot import Bot __version__: str = '2.0.0' __author__: str = 'Drawbu' __maintainer__: str = 'Sigmanificient' -JsonDict = Dict[str, Any] -JsonList = List[Any] -JsonData = Union[JsonDict, JsonList] +__all__ = ('Bot',) diff --git a/drawbot/bot.py b/drawbot/bot.py index e6126bc..627fe13 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -4,8 +4,8 @@ from discord.ext import commands -from drawbot import JsonData -from drawbot.utils import json_wr +from .types import JsonData +from .utils import json_wr class Bot(commands.Bot): @@ -30,7 +30,7 @@ def __init__(self) -> None: if self.config.get(key, "") == "": print( f"Veuillez indiquer remplir la valeur \"{key}\" " - "dans le fichier config.json" + "dans le fichier vars/config.json" ) sys.exit() diff --git a/drawbot/cogs/informations.py b/drawbot/cogs/informations.py index 277f52c..8f6ab04 100644 --- a/drawbot/cogs/informations.py +++ b/drawbot/cogs/informations.py @@ -4,9 +4,8 @@ from discord.ext.commands import Context -from drawbot import JsonDict - -from drawbot.utils import json_wr +from ..types import JsonDict +from ..utils import json_wr class Informations(commands.Cog): diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index 9bc956c..c0e5fc0 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -5,8 +5,8 @@ from discord.ext.commands import Context from discord.ext import commands, tasks -from drawbot import JsonDict -from drawbot.utils import json_wr, fetch_homeworks, fetch_grades +from ..types import JsonDict +from ..utils import json_wr, fetch_homeworks, fetch_grades class Pronote(commands.Cog): diff --git a/drawbot/types.py b/drawbot/types.py new file mode 100644 index 0000000..34b400f --- /dev/null +++ b/drawbot/types.py @@ -0,0 +1,5 @@ +from typing import Dict, Any, List, Union + +JsonDict = Dict[str, Any] +JsonList = List[Any] +JsonData = Union[JsonDict, JsonList] diff --git a/drawbot/utils/json_files.py b/drawbot/utils/json_files.py index 88fe465..ed4ce13 100644 --- a/drawbot/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -2,7 +2,7 @@ import os from typing import Optional, Literal -from drawbot import JsonData +from ..types import JsonData def json_wr( @@ -31,7 +31,7 @@ def json_wr( if data is None: data = {} - filename = f"./drawbot/{filename}.json" + filename = f"vars/{filename}.json" if mode == "r": if not os.path.isfile(filename): diff --git a/drawbot/utils/pronote.py b/drawbot/utils/pronote.py index 2f0f5a0..9c6bedf 100644 --- a/drawbot/utils/pronote.py +++ b/drawbot/utils/pronote.py @@ -1,8 +1,7 @@ from typing import DefaultDict, Generator, Optional from collections import defaultdict -from drawbot.utils import json_wr - +from ..utils import json_wr import pronotepy From a0b389bffdd23ce17e0be28d95bc60b1ad7f994c Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:28:35 +0200 Subject: [PATCH 06/19] :memo: Updated documentation --- README.md | 10 +++++----- drawbot/.gitignore | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) delete mode 100644 drawbot/.gitignore diff --git a/README.md b/README.md index 17aec79..5c43e31 100644 --- a/README.md +++ b/README.md @@ -21,25 +21,25 @@ Help server: https://discord.gg/XGXydQyKhQ ```sh git clone https://github.com/drawbu/drawbot -cd /path +cd drawbot pip install -r requirements.txt ``` ## Launch ```sh -py run.py +py drawbot ``` ## Files documentation -The bot will create **2** json files in the **app** folder: +The bot will create **2** json files in the **vars** folder: - config.json - devoirs.json - grades.json -### app/ config.json +### vars/ config.json This file stocks your private logins and info's to make to bor running: @@ -66,5 +66,5 @@ If you can't connect to Pronote, check if your establishment is not using an ENT. In this case, see what you need to do with the help of the pronote wrapper project: [pronotepy](https://github.com/bain3/pronotepy) -### app/ devoirs.json +### vars/ devoirs.json Automatically generated JSON containing homeworks information. diff --git a/drawbot/.gitignore b/drawbot/.gitignore deleted file mode 100644 index 6fce82c..0000000 --- a/drawbot/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -cogs/dev.py -config.json From c2e6689649c65c43da25fc35f1502004fe8bcf3d Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:49:07 +0200 Subject: [PATCH 07/19] :package: Further improvements for package installation --- README.md | 38 +++++++++++++++----------------------- drawbot/cogs/__init__.py | 0 requirements.txt | 2 -- setup.cfg | 11 +++++++++++ vars/.gitignore | 1 + vars/config.json.example | 8 ++++++++ 6 files changed, 35 insertions(+), 25 deletions(-) create mode 100644 drawbot/cogs/__init__.py delete mode 100644 requirements.txt create mode 100644 vars/config.json.example diff --git a/README.md b/README.md index 5c43e31..1da2a37 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ Help server: https://discord.gg/XGXydQyKhQ | :warning: | We do not take responsibility for a possible leak of your passwords, which is why you need to host the bot yourself. | ## Installation + ```sh git clone https://github.com/drawbu/drawbot - cd drawbot -pip install -r requirements.txt +pip install -e . ``` ## Launch @@ -31,40 +31,32 @@ pip install -r requirements.txt py drawbot ``` +*If you are using a Linux distribution, you can use the `make` command to install dependencies and run the bot.* + ## Files documentation The bot will create **2** json files in the **vars** folder: -- config.json - devoirs.json - grades.json -### vars/ config.json +### vars/config.json -This file stocks your private logins and info's to make to bor running: +This file stocks your private logins and info's to make to bot running. +You can find a **config.example.json** file in the **vars** folder. -```json5 -{ - "token": "( ͡° ͜ʖ ͡°)", - "prefix": "!", - "channelID": "000000000000000000", - "username": "demonstration", - "password": "pronotevs", - "url": "https://demo.index-education.net/pronote/eleve.html?login=true" -} -``` -THESE ARE JUST EXAMPLES +copy that file as **config.json** and fill in the values as follows: -In `"token"`, you need to add your bot token.
-In `"prefix"`, the prefix your bot will use.
-In `"channelID"`, the ID of the Discord channel.
-In `"username"`, your Pronote username.
-In `"password"`, your Pronote password.
-In `"url"`, the url of your pronote client.
+- `"token"`: your bot token.
+- `"prefix"`: the prefix your bot will use.
+- `"channelID"`: the ID of the Discord channel.
+- `"username"`: your Pronote username.
+- `"password"`: your Pronote password.
+- `"url"`: the url of your pronote client.
If you can't connect to Pronote, check if your establishment is not using an ENT. In this case, see what you need to do with the help of the pronote wrapper project: [pronotepy](https://github.com/bain3/pronotepy) -### vars/ devoirs.json +### vars/devoirs.json Automatically generated JSON containing homeworks information. diff --git a/drawbot/cogs/__init__.py b/drawbot/cogs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 2cae1db..0000000 --- a/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -discord~=1.7.3 -pronotepy==2.4.0 diff --git a/setup.cfg b/setup.cfg index 95aab77..e7a28f5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,3 +2,14 @@ name = drawbot version = 2.0.0 author = Drawbu + +[options] +packages = + drawbot + drawbot.cogs + drawbot.utils +install_requires = + discord~=1.7.3 + pronotepy==2.4.0 +python_requires = > 3.8 +zip_safe = no diff --git a/vars/.gitignore b/vars/.gitignore index d6b7ef3..c8b7ddd 100644 --- a/vars/.gitignore +++ b/vars/.gitignore @@ -1,2 +1,3 @@ * +!config.json.example !.gitignore diff --git a/vars/config.json.example b/vars/config.json.example new file mode 100644 index 0000000..279724d --- /dev/null +++ b/vars/config.json.example @@ -0,0 +1,8 @@ +{ + "token": "( ͡° ͜ʖ ͡°)", + "prefix": "!", + "channelID": "000000000000000000", + "username": "demonstration", + "password": "pronotevs", + "url": "https://demo.index-education.net/pronote/eleve.html?login=true" +} \ No newline at end of file From a085d751965618963a8bb27ebe40f128c6960c1b Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:49:28 +0200 Subject: [PATCH 08/19] :wrench: Added a simple Makefile --- Makefile | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3a62b51 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +VENV = venv +VBIN = $(VENV)/bin + +all: start + +clean: + rm -rf venv + rm -rf *.egg-info + rm -rf __pycache__ + rm vars/*.json + +$(VBIN)/python: + python -m venv venv + chmod +x venv/bin/activate + ./venv/bin/activate + pip install -e . + +vars/config.json: + cp vars/config.json.example vars/config.json + +start: $(VBIN)/python vars/config.json + python drawbot + +.PHONY: all clean start From c7b112424c7d0d90ead9e9373aab14635613530e Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:50:03 +0200 Subject: [PATCH 09/19] :bug: Renamed types => json_types for circular import fix --- drawbot/bot.py | 4 ++-- drawbot/cogs/informations.py | 2 +- drawbot/cogs/pronote.py | 2 +- drawbot/{types.py => json_types.py} | 0 drawbot/utils/json_files.py | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename drawbot/{types.py => json_types.py} (100%) diff --git a/drawbot/bot.py b/drawbot/bot.py index 627fe13..2ccd62b 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -4,7 +4,7 @@ from discord.ext import commands -from .types import JsonData +from .json_types import JsonData from .utils import json_wr @@ -41,7 +41,7 @@ def __init__(self) -> None: self.remove_command("help") for filename in os.listdir("drawbot/cogs"): - if filename.endswith(".py"): + if filename.endswith(".py") and filename != "__init__.py": self.load_extension(f"drawbot.cogs.{filename[:-3]}") def run(self) -> None: diff --git a/drawbot/cogs/informations.py b/drawbot/cogs/informations.py index 8f6ab04..bb08a24 100644 --- a/drawbot/cogs/informations.py +++ b/drawbot/cogs/informations.py @@ -4,7 +4,7 @@ from discord.ext.commands import Context -from ..types import JsonDict +from ..json_types import JsonDict from ..utils import json_wr diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index c0e5fc0..f91c58d 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -5,7 +5,7 @@ from discord.ext.commands import Context from discord.ext import commands, tasks -from ..types import JsonDict +from ..json_types import JsonDict from ..utils import json_wr, fetch_homeworks, fetch_grades diff --git a/drawbot/types.py b/drawbot/json_types.py similarity index 100% rename from drawbot/types.py rename to drawbot/json_types.py diff --git a/drawbot/utils/json_files.py b/drawbot/utils/json_files.py index ed4ce13..0d07997 100644 --- a/drawbot/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -2,7 +2,7 @@ import os from typing import Optional, Literal -from ..types import JsonData +from ..json_types import JsonData def json_wr( From 19a8965a7156ceabaf5ccc3f7012846932db3933 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:53:20 +0200 Subject: [PATCH 10/19] :sparkles: Added user-friendly print on Login Failure --- drawbot/bot.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drawbot/bot.py b/drawbot/bot.py index 2ccd62b..50c93eb 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -2,6 +2,7 @@ import sys from typing import Optional +from discord import LoginFailure from discord.ext import commands from .json_types import JsonData @@ -44,8 +45,14 @@ def __init__(self) -> None: if filename.endswith(".py") and filename != "__init__.py": self.load_extension(f"drawbot.cogs.{filename[:-3]}") - def run(self) -> None: - super().run(self._token) + def run(self): + try: + super().run(self._token) + except LoginFailure: + print( + "Echec de la connexion au client." + "Veuillez vérifier que votre token est correct." + ) - async def on_ready(self) -> None: + async def on_ready(self): print(f"Connecté en temps que {self.user.name} !") From 30d45aaea7b0bb0fa042efefdf0adc6f5fe6b6b3 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 05:57:13 +0200 Subject: [PATCH 11/19] :art: Removed useless typing --- drawbot/__main__.py | 2 +- drawbot/bot.py | 2 +- drawbot/cogs/{informations.py => information.py} | 10 +++++----- drawbot/cogs/pronote.py | 6 +++--- drawbot/utils/json_files.py | 1 - 5 files changed, 10 insertions(+), 11 deletions(-) rename drawbot/cogs/{informations.py => information.py} (86%) diff --git a/drawbot/__main__.py b/drawbot/__main__.py index 047998e..88e19dd 100644 --- a/drawbot/__main__.py +++ b/drawbot/__main__.py @@ -1,7 +1,7 @@ from drawbot import Bot -def main() -> None: +def main(): """Entry point to run the bot client.""" bot = Bot() bot.run() diff --git a/drawbot/bot.py b/drawbot/bot.py index 50c93eb..bb52837 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -11,7 +11,7 @@ class Bot(commands.Bot): - def __init__(self) -> None: + def __init__(self): """Initialize the bot and load config for token and prefix.""" self.embed_color: int = 0x1E744F self._token: Optional[str] = None diff --git a/drawbot/cogs/informations.py b/drawbot/cogs/information.py similarity index 86% rename from drawbot/cogs/informations.py rename to drawbot/cogs/information.py index bb08a24..7f96b2a 100644 --- a/drawbot/cogs/informations.py +++ b/drawbot/cogs/information.py @@ -8,7 +8,7 @@ from ..utils import json_wr -class Informations(commands.Cog): +class Information(commands.Cog): def __init__(self, client): """Initialize the different commands.""" @@ -17,7 +17,7 @@ def __init__(self, client): @commands.command( name='help', aliases=('h', 'aide'), ) - async def help_command(self, ctx: Context) -> None: + async def help_command(self, ctx: Context): help_embed: Embed = discord.Embed( title="Help of the Pronote", description=( @@ -35,7 +35,7 @@ async def help_command(self, ctx: Context) -> None: @commands.command( name='channel', aliases=('here',), ) - async def change_channel(self, ctx: Context) -> None: + async def change_channel(self, ctx: Context): pronote_config: JsonDict = json_wr('pronote') pronote_config['channelID'] = ctx.channel.id json_wr('pronote', data=pronote_config) @@ -52,5 +52,5 @@ async def change_channel(self, ctx: Context) -> None: ) -def setup(client) -> None: - client.add_cog(Informations(client)) +def setup(client): + client.add_cog(Information(client)) diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index f91c58d..5e4e102 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -16,11 +16,11 @@ def __init__(self, client): self.client = client @commands.Cog.listener() - async def on_ready(self) -> None: + async def on_ready(self): self.refresh_pronote.start() @tasks.loop(seconds=300) - async def refresh_pronote(self) -> None: + async def refresh_pronote(self): date = time.strftime("%Y-%m-%d %H:%M", time.gmtime()) @@ -144,5 +144,5 @@ async def change_channel(self, ctx: Context) -> None: await ctx.send(embed=embed) -def setup(client) -> None: +def setup(client): client.add_cog(Pronote(client)) diff --git a/drawbot/utils/json_files.py b/drawbot/utils/json_files.py index 0d07997..0ba57d5 100644 --- a/drawbot/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -49,4 +49,3 @@ def json_wr( elif mode == "w": with open(filename, "w") as f: json.dump(data, f, indent=4) - return From 88c16dc27ecb425baaba96c57c0144cb3d9582ab Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 06:06:02 +0200 Subject: [PATCH 12/19] :recycle: Factorized fetch json --- drawbot/utils/pronote.py | 56 ++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/drawbot/utils/pronote.py b/drawbot/utils/pronote.py index 9c6bedf..a109ce2 100644 --- a/drawbot/utils/pronote.py +++ b/drawbot/utils/pronote.py @@ -1,6 +1,7 @@ from typing import DefaultDict, Generator, Optional from collections import defaultdict +from ..json_types import JsonData from ..utils import json_wr import pronotepy @@ -17,29 +18,7 @@ def fetch_homeworks(pronote_client: pronotepy.Client) -> Optional[Generator]: } ) - homeworks_file = json_wr("devoirs") - if homeworks == homeworks_file: - return - - json_wr("devoirs", "w", homeworks) - - homeworks_list: list = [] - for key, value in homeworks.items(): - for i in value: - i["date"] = key - homeworks_list.extend(value) - - homeworks_old_list: list = [] - for key, value in homeworks_file.items(): - for i in value: - i["date"] = key - homeworks_old_list.extend(value) - - for homework in homeworks_list: - if homework in homeworks_old_list: - continue - - yield homework + yield from fetch_from_json("devoirs", homeworks) def fetch_grades(pronote_client: pronotepy.Client) -> Optional[Generator]: @@ -59,26 +38,31 @@ def fetch_grades(pronote_client: pronotepy.Client) -> Optional[Generator]: } ) - grades_file = json_wr("grades") - if grades == grades_file: - return + yield from fetch_from_json("grades", grades) + + +def fetch_from_json(filename: str, json_data: JsonData): + json_file = json_wr(filename) + + if json_data == json_file: + yield from () - json_wr("grades", "w", grades) + json_wr(filename, "w", json_data) - grades_list: list = [] - for key, value in grades.items(): + json_vals: list = [] + for key, value in json_data.items(): for i in value: i["date"] = key - grades_list.extend(value) + json_vals.extend(value) - grades_old_list: list = [] - for key, value in grades_file.items(): + old_vals: list = [] + for key, value in json_file.items(): for i in value: i["date"] = key - grades_old_list.extend(value) + old_vals.extend(value) - for grade in grades_list: - if grade in grades_old_list: + for value in json_vals: + if value in old_vals: continue - yield grade + yield value From 155e4294bdac712c800c109d3e18e1e61c28db93 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 06:07:34 +0200 Subject: [PATCH 13/19] :art: Quick reformat --- drawbot/__init__.py | 8 +++---- drawbot/__main__.py | 2 +- drawbot/bot.py | 5 ++-- drawbot/cogs/information.py | 21 ++++++++-------- drawbot/cogs/pronote.py | 48 ++++++++++++++++--------------------- drawbot/utils/json_files.py | 4 +--- drawbot/utils/pronote.py | 4 ++-- setup.py | 2 +- 8 files changed, 43 insertions(+), 51 deletions(-) diff --git a/drawbot/__init__.py b/drawbot/__init__.py index 2635fd3..438d976 100644 --- a/drawbot/__init__.py +++ b/drawbot/__init__.py @@ -1,7 +1,7 @@ from .bot import Bot -__version__: str = '2.0.0' -__author__: str = 'Drawbu' -__maintainer__: str = 'Sigmanificient' +__version__: str = "2.0.0" +__author__: str = "Drawbu" +__maintainer__: str = "Sigmanificient" -__all__ = ('Bot',) +__all__ = ("Bot",) diff --git a/drawbot/__main__.py b/drawbot/__main__.py index 88e19dd..5b24601 100644 --- a/drawbot/__main__.py +++ b/drawbot/__main__.py @@ -7,5 +7,5 @@ def main(): bot.run() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/drawbot/bot.py b/drawbot/bot.py index bb52837..965ded6 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -10,7 +10,6 @@ class Bot(commands.Bot): - def __init__(self): """Initialize the bot and load config for token and prefix.""" self.embed_color: int = 0x1E744F @@ -22,7 +21,7 @@ def __init__(self): "channelID": "", "username": "", "password": "", - "url": "" + "url": "", } self.config: JsonData = json_wr("config") @@ -30,7 +29,7 @@ def __init__(self): for key in default_config.keys(): if self.config.get(key, "") == "": print( - f"Veuillez indiquer remplir la valeur \"{key}\" " + f'Veuillez indiquer remplir la valeur "{key}" ' "dans le fichier vars/config.json" ) sys.exit() diff --git a/drawbot/cogs/information.py b/drawbot/cogs/information.py index 7f96b2a..ffa294d 100644 --- a/drawbot/cogs/information.py +++ b/drawbot/cogs/information.py @@ -9,13 +9,13 @@ class Information(commands.Cog): - def __init__(self, client): """Initialize the different commands.""" self.client = client @commands.command( - name='help', aliases=('h', 'aide'), + name="help", + aliases=("h", "aide"), ) async def help_command(self, ctx: Context): help_embed: Embed = discord.Embed( @@ -24,21 +24,22 @@ async def help_command(self, ctx: Context): "Un bot qui traque vos devoir pronote " "et vous les notifient sur discord." ), - color=self.client.embed_color + color=self.client.embed_color, ).add_field( - name=f'{ctx.prefix}here', - value='change le salon d \'envoi des nouveaux devoirs' + name=f"{ctx.prefix}here", + value="change le salon d 'envoi des nouveaux devoirs", ) await ctx.send(embed=help_embed) @commands.command( - name='channel', aliases=('here',), + name="channel", + aliases=("here",), ) async def change_channel(self, ctx: Context): - pronote_config: JsonDict = json_wr('pronote') - pronote_config['channelID'] = ctx.channel.id - json_wr('pronote', data=pronote_config) + pronote_config: JsonDict = json_wr("pronote") + pronote_config["channelID"] = ctx.channel.id + json_wr("pronote", data=pronote_config) await ctx.send( embed=discord.Embed( @@ -47,7 +48,7 @@ async def change_channel(self, ctx: Context): "Le salon pour envoyer les nouveaux devoirs " "à bien été mis à jour" ), - color=self.client.embed_color + color=self.client.embed_color, ) ) diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index 5e4e102..6418ed6 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -10,7 +10,6 @@ class Pronote(commands.Cog): - def __init__(self, client): """Initialize the search for new homeworks.""" self.client = client @@ -28,7 +27,7 @@ async def refresh_pronote(self): pronote: pronotepy.Client = pronotepy.Client( self.client.config["url"], self.client.config["username"], - self.client.config["password"] + self.client.config["password"], ) except pronotepy.CryptoError: @@ -48,10 +47,8 @@ async def refresh_pronote(self): return try: - pronote_channel: discord.TextChannel = ( - await self.client.fetch_channel( - int(self.client.config.get("channelID")) - ) + pronote_channel: discord.TextChannel = await self.client.fetch_channel( + int(self.client.config.get("channelID")) ) except discord.errors.NotFound: print("Channel non-trouvé ou inexistant") @@ -74,7 +71,7 @@ def discord_timestamp(t_str: str) -> str: f"Pour le {discord_timestamp(homework['date'])}" ), description=homework["description"], - color=0x1E744F + color=0x1E744F, ) ) @@ -95,28 +92,26 @@ def discord_timestamp(t_str: str) -> str: f"Note + : **{grade['max']}**\n" f"Note - : **{grade['min']}**\n" ), - color=0x1E744F + color=0x1E744F, ) ) print( - (date if any(auths) else "") + ( - '' if not auths["homeworks"] + (date if any(auths) else "") + + ( + "" + if not auths["homeworks"] else f" - {new_homework_count} nouveaux devoirs" - ) + ( - '' if not auths["grades"] - else f" - {new_grades_count} nouveaux notes" - ) + (" !" if any(auths) else "") + ) + + ("" if not auths["grades"] else f" - {new_grades_count} nouveaux notes") + + (" !" if any(auths) else "") ) @commands.command(name="homeworks", aliases=("devoirs",)) async def change_channel(self, ctx: Context) -> None: homeworks: JsonDict = json_wr("devoirs") - embed = discord.Embed( - title="Prochains devoirs", - color=self.client.embed_color - ) + embed = discord.Embed(title="Prochains devoirs", color=self.client.embed_color) today = time.time() homeworks_dict = {} @@ -126,19 +121,18 @@ async def change_channel(self, ctx: Context) -> None: if date_timestamp >= today: homeworks_dict[date_timestamp] = homeworks_list - homeworks_dict = { - key: homeworks_dict[key] - for key in sorted(homeworks_dict) - } + homeworks_dict = {key: homeworks_dict[key] for key in sorted(homeworks_dict)} for date, homeworks_list in homeworks_dict.items(): embed.add_field( name=f"Pour le ", - value="\n".join([ - f"**- {h['subject']} :** {h['description']}" - for h in homeworks_list - ]), - inline=False + value="\n".join( + [ + f"**- {h['subject']} :** {h['description']}" + for h in homeworks_list + ] + ), + inline=False, ) await ctx.send(embed=embed) diff --git a/drawbot/utils/json_files.py b/drawbot/utils/json_files.py index 0ba57d5..f09b6cc 100644 --- a/drawbot/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -6,9 +6,7 @@ def json_wr( - filename: str, - mode: Literal["r", "w"] = "r", - data=None + filename: str, mode: Literal["r", "w"] = "r", data=None ) -> Optional[JsonData]: """Write and read json files. diff --git a/drawbot/utils/pronote.py b/drawbot/utils/pronote.py index a109ce2..63164cc 100644 --- a/drawbot/utils/pronote.py +++ b/drawbot/utils/pronote.py @@ -14,7 +14,7 @@ def fetch_homeworks(pronote_client: pronotepy.Client) -> Optional[Generator]: homeworks[str(homework.date)].append( { "subject": homework.subject.name, - "description": homework.description.replace("\n", " ") + "description": homework.description.replace("\n", " "), } ) @@ -34,7 +34,7 @@ def fetch_grades(pronote_client: pronotepy.Client) -> Optional[Generator]: "average": f"{g.average}/{g.out_of}", "coefficient": f"{g.coefficient}", "max": f"{g.max}/{g.out_of}", - "min": f"{g.min}/{g.out_of}" + "min": f"{g.min}/{g.out_of}", } ) diff --git a/setup.py b/setup.py index 6b40b52..7f1a176 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ from setuptools import setup -if __name__ == '__main__': +if __name__ == "__main__": setup() From bf55b47940a81b8fb7f56700d573d286f223d0c5 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 06:10:10 +0200 Subject: [PATCH 14/19] :truck: Moved readme to docs --- README.md => docs/README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => docs/README.md (100%) diff --git a/README.md b/docs/README.md similarity index 100% rename from README.md rename to docs/README.md From 848d1c1f9f0460e553aba6743d55a71aa70db153 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 06:16:52 +0200 Subject: [PATCH 15/19] :wrench: Improving setup.cfg & added pyproject.toml --- drawbot/cogs/pronote.py | 5 +---- pyproject.toml | 3 +++ setup.cfg | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 pyproject.toml diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index 6418ed6..0652cfb 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -127,10 +127,7 @@ async def change_channel(self, ctx: Context) -> None: embed.add_field( name=f"Pour le ", value="\n".join( - [ - f"**- {h['subject']} :** {h['description']}" - for h in homeworks_list - ] + f"**- {h['subject']} :** {h['description']}" for h in homeworks_list ), inline=False, ) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..383c0bb --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools>=57.0", "wheel>=0.36"] +build-backend = "setuptools.build_meta" diff --git a/setup.cfg b/setup.cfg index e7a28f5..64a8d2c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,6 +2,24 @@ name = drawbot version = 2.0.0 author = Drawbu +description = A Pronote notifier discord bot. +long_description = file: docs/README.md +long_description_content_type = text/markdown +license = MIT +license_file = LICENSE +platform = unix, linux, osx, cygwin, windows +project_urls = + Github repository=https://github.com/drawbu/drawbot + Discord=https://discord.gg/XGXydQyKhQ +classifiers = + Development Status :: 5 - Production/Stable + Intended Audience :: Developers + Topic :: Software Development :: Build Tools + License :: OSI Approved :: MIT License + Programming Language :: Python :: 3 :: Only + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 [options] packages = @@ -13,3 +31,8 @@ install_requires = pronotepy==2.4.0 python_requires = > 3.8 zip_safe = no + + +[options.extras_require] +dev = + black~=22.6.0 From 22ebebb10aa753b2e2fffa6a08d6a0e910d993f2 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 06:38:24 +0200 Subject: [PATCH 16/19] :adhesive_bandage: Fixed stupid codacy warning --- drawbot/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/drawbot/__init__.py b/drawbot/__init__.py index 438d976..3bf50f3 100644 --- a/drawbot/__init__.py +++ b/drawbot/__init__.py @@ -1,3 +1,4 @@ +"""A Pronote notifier discord bot.""" from .bot import Bot __version__: str = "2.0.0" From 5283073f4123efbe44671382b4a13836d44f2d30 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 17:15:06 +0200 Subject: [PATCH 17/19] :truck: Moved json types to json utils --- drawbot/bot.py | 3 +-- drawbot/cogs/__init__.py | 1 + drawbot/cogs/information.py | 3 +-- drawbot/cogs/pronote.py | 3 +-- drawbot/json_types.py | 5 ----- drawbot/utils/__init__.py | 12 +++++++++--- drawbot/utils/json_files.py | 6 ++++-- drawbot/utils/pronote.py | 2 +- 8 files changed, 18 insertions(+), 17 deletions(-) delete mode 100644 drawbot/json_types.py diff --git a/drawbot/bot.py b/drawbot/bot.py index 965ded6..cef0fab 100644 --- a/drawbot/bot.py +++ b/drawbot/bot.py @@ -5,8 +5,7 @@ from discord import LoginFailure from discord.ext import commands -from .json_types import JsonData -from .utils import json_wr +from .utils import json_wr, JsonData class Bot(commands.Bot): diff --git a/drawbot/cogs/__init__.py b/drawbot/cogs/__init__.py index e69de29..a1798b7 100644 --- a/drawbot/cogs/__init__.py +++ b/drawbot/cogs/__init__.py @@ -0,0 +1 @@ +"""Cogs sub-package for drawbot.""" diff --git a/drawbot/cogs/information.py b/drawbot/cogs/information.py index ffa294d..710eefd 100644 --- a/drawbot/cogs/information.py +++ b/drawbot/cogs/information.py @@ -4,8 +4,7 @@ from discord.ext.commands import Context -from ..json_types import JsonDict -from ..utils import json_wr +from ..utils import json_wr, JsonDict class Information(commands.Cog): diff --git a/drawbot/cogs/pronote.py b/drawbot/cogs/pronote.py index 0652cfb..ed23783 100644 --- a/drawbot/cogs/pronote.py +++ b/drawbot/cogs/pronote.py @@ -5,8 +5,7 @@ from discord.ext.commands import Context from discord.ext import commands, tasks -from ..json_types import JsonDict -from ..utils import json_wr, fetch_homeworks, fetch_grades +from ..utils import json_wr, fetch_homeworks, fetch_grades, JsonDict class Pronote(commands.Cog): diff --git a/drawbot/json_types.py b/drawbot/json_types.py deleted file mode 100644 index 34b400f..0000000 --- a/drawbot/json_types.py +++ /dev/null @@ -1,5 +0,0 @@ -from typing import Dict, Any, List, Union - -JsonDict = Dict[str, Any] -JsonList = List[Any] -JsonData = Union[JsonDict, JsonList] diff --git a/drawbot/utils/__init__.py b/drawbot/utils/__init__.py index daf5375..85b8da5 100644 --- a/drawbot/utils/__init__.py +++ b/drawbot/utils/__init__.py @@ -1,5 +1,11 @@ -from .json_files import json_wr +from .json_files import json_wr, JsonData, JsonDict, JsonList from .pronote import fetch_homeworks, fetch_grades - -__all__ = ("json_wr", "fetch_homeworks", "fetch_grades") +__all__ = ( + "json_wr", + "fetch_homeworks", + "fetch_grades", + "JsonData", + "JsonDict", + "JsonList", +) diff --git a/drawbot/utils/json_files.py b/drawbot/utils/json_files.py index f09b6cc..5ac0b17 100644 --- a/drawbot/utils/json_files.py +++ b/drawbot/utils/json_files.py @@ -1,8 +1,10 @@ import json import os -from typing import Optional, Literal +from typing import Dict, Any, List, Union, Optional, Literal -from ..json_types import JsonData +JsonDict = Dict[str, Any] +JsonList = List[Any] +JsonData = Union[JsonDict, JsonList] def json_wr( diff --git a/drawbot/utils/pronote.py b/drawbot/utils/pronote.py index 63164cc..48045d5 100644 --- a/drawbot/utils/pronote.py +++ b/drawbot/utils/pronote.py @@ -1,7 +1,7 @@ from typing import DefaultDict, Generator, Optional from collections import defaultdict -from ..json_types import JsonData +from .json_files import JsonData from ..utils import json_wr import pronotepy From a7968252db744cb6a4b569e7825add8e222bee25 Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 17:17:30 +0200 Subject: [PATCH 18/19] :rewind: Re-added config.json example in readme --- docs/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/README.md b/docs/README.md index 1da2a37..090c907 100644 --- a/docs/README.md +++ b/docs/README.md @@ -45,6 +45,18 @@ The bot will create **2** json files in the **vars** folder: This file stocks your private logins and info's to make to bot running. You can find a **config.example.json** file in the **vars** folder. +> `config.json` **Example** +```json5 +{ + "token": "( ͡° ͜ʖ ͡°)", + "prefix": "!", + "channelID": "000000000000000000", + "username": "demonstration", + "password": "pronotevs", + "url": "https://demo.index-education.net/pronote/eleve.html?login=true" +} +``` + copy that file as **config.json** and fill in the values as follows: - `"token"`: your bot token.
From 7decaea13fa9f61f2271a9a3fbb72ba0ecfdb80e Mon Sep 17 00:00:00 2001 From: sigmanificient Date: Sat, 9 Jul 2022 17:20:58 +0200 Subject: [PATCH 19/19] :adhesive_bandage: Fixed stupid codacy warning ~2 --- drawbot/utils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/drawbot/utils/__init__.py b/drawbot/utils/__init__.py index 85b8da5..07f9aaf 100644 --- a/drawbot/utils/__init__.py +++ b/drawbot/utils/__init__.py @@ -1,3 +1,4 @@ +"""Utilities for drawbot.""" from .json_files import json_wr, JsonData, JsonDict, JsonList from .pronote import fetch_homeworks, fetch_grades