From a0dfe80a760500ca8d9eda7a415ec129c162c0c8 Mon Sep 17 00:00:00 2001 From: louis chauvet Date: Sat, 1 Sep 2018 19:18:11 +0200 Subject: [PATCH 1/4] Use dropbox file storage instead of ftp --- main.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 537822c..a4d85bc 100644 --- a/main.py +++ b/main.py @@ -7,15 +7,15 @@ import os import sys import discord -from fs.ftpfs import FTPFS +from fs.dropboxfs import DropboxFS from fs.osfs import OSFS from fs import path fileSystem = None -if os.environ.get("FTP_ADDRESS", False) and os.environ.get("FTP_USER", False) and os.environ.get("FTP_PASS", False): +if os.environ.get("DROPBOX_ACCESS_TOKEN", False): print("FTP") - fileSystem = FTPFS(os.environ["FTP_ADDRESS"], user=os.environ["FTP_USER"], passwd=os.environ["FTP_PASS"], timeout=600) + fileSystem = DropboxFS(os.environ["DROPBOX_ACCESS_TOKEN"]) else: print("OS") fileSystem = OSFS(os.getcwd()) @@ -83,10 +83,10 @@ class Guild: self.update_modules() def load_config(self): - if fileSystem.exists(self.config_file): + if self.bot.fileSystem.exists(self.config_file): try: # Loading configuration file - with fileSystem.open(self.config_file) as conf: + with self.bot.fileSystem.open(self.config_file) as conf: self.config.update(json.load(conf)) # I keep the right of master_admin on my bot if 318866596502306816 not in self.config["master_admins"]: @@ -109,6 +109,9 @@ class Guild: self.config["modules"].append("help") module_to_load = list(set(self.config["modules"])) + self.config["modules"] = module_to_load + self.save_config() + for module in module_to_load: # Try to load all modules by name if module not in self.bot.modules.keys(): @@ -124,7 +127,7 @@ class Guild: def save_config(self): try: - with fileSystem.open(self.config_file, 'w') as conf_file: + with self.bot.fileSystem.open(self.config_file, 'w') as conf_file: json.dump(self.config, conf_file) except PermissionError: error("Cannot write to configuration file.") @@ -145,6 +148,7 @@ class FoBot(discord.Client): self.guilds_class = {} self.modules = {} self.load_modules() + self.fileSystem = fileSystem def load_modules(self): for module in os.listdir('modules'): @@ -153,10 +157,10 @@ class FoBot(discord.Client): self.modules.update({module[:-3]: imported.MainClass}) def load_config(self): - if fileSystem.exists(path.join(self.config_folder, "conf.json")): + if self.fileSystem.exists(path.join(self.config_folder, "conf.json")): try: # Loading configuration file - with fileSystem.open(path.join(self.config_folder, "conf.json")) as conf: + with self.fileSystem.open(path.join(self.config_folder, "conf.json")) as conf: self.config.update(json.load(conf)) except PermissionError: critical("Cannot open config file.") @@ -175,11 +179,11 @@ class FoBot(discord.Client): self.guilds_class.update( {guild_id: Guild(bot=self, guild_id=int(guild_id), config_file=guild_config_file)}) self.save_config() - elif fileSystem.exists(self.config_folder): + elif self.fileSystem.exists(self.config_folder): self.save_config() else: try: - fileSystem.makedir(self.config_folder) + self.fileSystem.makedir(self.config_folder) except PermissionError: critical("Cannot create config folder.") sys.exit() @@ -188,7 +192,7 @@ class FoBot(discord.Client): for guild in self.guilds_class.values(): guild.save_config() try: - with fileSystem.open(path.join(self.config_folder, "conf.json"), 'w') as conf_file: + with self.fileSystem.open(path.join(self.config_folder, "conf.json"), 'w') as conf_file: json.dump(self.config, conf_file, indent=4) except PermissionError: critical("Cannot write to configuration file.") @@ -209,7 +213,12 @@ class FoBot(discord.Client): async def on_error(self, event, *args, **kwargs): error("foBot encounter an error.", exc_info=True) - + if os.environ.get("DROPBOX_ACCESS_TOKEN", False): + print("FTP") + self.fileSystem = DropboxFS(os.environ["DROPBOX_ACCESS_TOKEN"]) + else: + print("OS") + self.fileSystem = OSFS(os.getcwd()) async def on_message(self, msg): await self.guilds_class[msg.guild.id].on_message(msg) From 0450242f700327481425e60f2b6ba3983c908b12 Mon Sep 17 00:00:00 2001 From: louis chauvet Date: Sat, 1 Sep 2018 19:51:17 +0200 Subject: [PATCH 2/4] Use Pipfile instead of requirements.txt --- Pipfile | 13 +++++++++++++ Pipfile.lock | 29 +++++++++++++++++++++++++++++ requirements.txt | 13 ------------- 3 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock delete mode 100644 requirements.txt diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..c88c265 --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +"fs.dropboxfs" = {git = "https://github.com/rkhwaja/fs.dropboxfs.git"} +"discord.py" = {ref = "rewrite", git = "https://github.com/Rapptz/discord.py"} + +[dev-packages] + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..0366270 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,29 @@ +{ + "_meta": { + "hash": { + "sha256": "0b00547c8080ca98592603203321ca0aa4c3be1d1d1d0d69ac0fb28b78619350" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "discord.py": { + "git": "https://github.com/Rapptz/discord.py", + "ref": "00a659c6526b2445162b52eaf970adbd22c6d35d" + }, + "fs.dropboxfs": { + "git": "https://github.com/rkhwaja/fs.dropboxfs.git", + "ref": "68988a25143a8ea760bc6dfb2a715f6f98ac31c7" + } + }, + "develop": {} +} diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 8923509..0000000 --- a/requirements.txt +++ /dev/null @@ -1,13 +0,0 @@ -aiohttp==3.3.2 -async-timeout==3.0.0 -attrs==18.1.0 -cffi==1.11.5 -chardet==3.0.4 -git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py -idna==2.7 -multidict==4.3.1 -pycparser==2.18 -six==1.11.0 -websockets==6.0 -yarl==1.2.6 -fs \ No newline at end of file From e7a1a3bbb3d3439af86249c20371fe07633e655a Mon Sep 17 00:00:00 2001 From: louis chauvet Date: Sat, 1 Sep 2018 19:58:24 +0200 Subject: [PATCH 3/4] Add / at begening of path for dropboxfs --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index a4d85bc..c98d065 100644 --- a/main.py +++ b/main.py @@ -141,7 +141,7 @@ class Guild: class FoBot(discord.Client): - def __init__(self, config='foBot_config', *args, **kwargs): + def __init__(self, config='/foBot_config', *args, **kwargs): super().__init__(*args, **kwargs) self.config_folder = config self.config = {"guilds": {}} From 45eb1419c41bb0857810daca106679fc0cbc18f7 Mon Sep 17 00:00:00 2001 From: louis chauvet Date: Sat, 1 Sep 2018 20:43:34 +0200 Subject: [PATCH 4/4] Update config when join a new guild --- main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main.py b/main.py index c98d065..5f56ca4 100644 --- a/main.py +++ b/main.py @@ -81,6 +81,7 @@ class Guild: self.modules = [] self.load_config() self.update_modules() + self.save_config() def load_config(self): if self.bot.fileSystem.exists(self.config_file): @@ -211,6 +212,11 @@ class FoBot(discord.Client): async def on_resumed(self): info("foBot is resumed.") + async def on_guild_join(self, guild): + self.load_modules() + self.load_config() + self.save_config() + async def on_error(self, event, *args, **kwargs): error("foBot encounter an error.", exc_info=True) if os.environ.get("DROPBOX_ACCESS_TOKEN", False):