Compare commits
No commits in common. "2656458253d05d2fa14d1057888d014f63f82985" and "9a2edd5bcd9ab2c92c2f9d049a4185219be19f6c" have entirely different histories.
2656458253
...
9a2edd5bcd
|
@ -1,4 +1,3 @@
|
||||||
.env
|
.env
|
||||||
launch.sh
|
launch.sh
|
||||||
src/**/__pycache__
|
src/**/__pycache__
|
||||||
compteur_electrique_data
|
|
|
@ -1,7 +1,3 @@
|
||||||
"""
|
"""
|
||||||
Bot package - Discord bot manager
|
Bot package - Discord bot manager
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["Bot"]
|
|
||||||
|
|
||||||
from .bot import *
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ async def on_message(message: discord.Message):
|
||||||
if "bonjour" in words:
|
if "bonjour" in words:
|
||||||
await channel.send("bien dormii ?? (˶ᵔᵕᵔ˶)₊˚⊹♡")
|
await channel.send("bien dormii ?? (˶ᵔᵕᵔ˶)₊˚⊹♡")
|
||||||
|
|
||||||
if any(word in words for word in ["oui", "ui", "ouai", "ouaip", "yes", "yep"]) and not message.content.__contains__("?"):
|
if any(mot in words for mot in ["oui", "ui", "ouai", "ouaip", "yes", "yep"]) and not message.content.__contains__("?"):
|
||||||
await channel.send("trow bienn~ (˶ᵔᵕᵔ˶) ‹𝟹")
|
await channel.send("trow bienn~ (˶ᵔᵕᵔ˶) ‹𝟹")
|
||||||
|
|
||||||
if "<@1169377208893194275>" in words:
|
if "<@1169377208893194275>" in words:
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
from todos_manager import *
|
|
||||||
|
|
||||||
manager = TodosManager()
|
|
||||||
|
|
||||||
print(manager.create_todo("test", "test_todo"))
|
|
|
@ -1,12 +1,7 @@
|
||||||
import discord
|
from bot.bot import Bot
|
||||||
from bot import Bot
|
|
||||||
|
|
||||||
# Manager controlled by
|
# Manager controlled by
|
||||||
from todos_manager import *
|
from todos_manager import *
|
||||||
|
|
||||||
def setup_todos_manager_commands(bot: Bot):
|
def setup_todos_manager_commands(bot: Bot):
|
||||||
@bot.client.tree.command(name="create_todo", description="Create a new todo list")
|
pass
|
||||||
async def create_todo(interaction: discord.Interaction, todo_list_name: str):
|
|
||||||
bot.todos_manager.create_todo(interaction.user.id.__str__(), todo_list_name)
|
|
||||||
await interaction.response.send_message(f"Todo list \"{todo_list_name}\" successfully created !")
|
|
||||||
|
|
||||||
|
|
|
@ -1,123 +1,15 @@
|
||||||
import json
|
import discord
|
||||||
import os
|
import json
|
||||||
|
import os
|
||||||
class TodosManager:
|
|
||||||
def __init__(self, todos_dir=os.path.join("compteur_electrique_data", "todos")) -> None:
|
class TodosManager:
|
||||||
try:
|
def __init__(self, data_dir="compteur_electrique_data/todos/"):
|
||||||
os.makedirs(todos_dir, exist_ok=True)
|
try:
|
||||||
except:
|
os.makedirs(data_dir, exist_ok=True)
|
||||||
print(f"Error during creation of \"{todos_dir}\" directory")
|
except OSError as error:
|
||||||
exit(1)
|
print(f"Error during creation of \"{data_dir}\" directory")
|
||||||
self.todos_dir=todos_dir
|
exit(1)
|
||||||
|
self.data_dir=data_dir
|
||||||
# Verify if member todos file exist and is valid, try to correct the error by creating the file or making a backup
|
|
||||||
def verify_todos_file(self, member_id: str):
|
def create_todo(self, member: discord.Member, todo_list_name: str) -> int:
|
||||||
member_todos_path = os.path.join(self.todos_dir, f"user_{member_id}_todos.json")
|
pass
|
||||||
|
|
||||||
if os.path.exists(member_todos_path):
|
|
||||||
with open(member_todos_path, 'r') as user_todos_file:
|
|
||||||
try:
|
|
||||||
json.load(user_todos_file)
|
|
||||||
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
print(f"Error decoding JSON file {member_todos_path}: {e}")
|
|
||||||
|
|
||||||
backup_path = member_todos_path + ".backup"
|
|
||||||
with open(backup_path, 'w') as backup_file:
|
|
||||||
backup_file.write(user_todos_file.read())
|
|
||||||
|
|
||||||
print(f"Corrupted file saved under: {backup_path}")
|
|
||||||
|
|
||||||
# Reset member todos
|
|
||||||
self.write_todos(member_id, {})
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Create and init member todos
|
|
||||||
self.write_todos(member_id, {})
|
|
||||||
|
|
||||||
# Open member's todos and return its dict form
|
|
||||||
def get_todos(self, member_id: str) -> dict:
|
|
||||||
self.verify_todos_file(member_id)
|
|
||||||
|
|
||||||
member_todos_path = os.path.join(self.todos_dir, f"user_{member_id}_todos.json")
|
|
||||||
|
|
||||||
with open(member_todos_path, 'r') as member_todos_file:
|
|
||||||
member_todos = json.load(member_todos_file)
|
|
||||||
return member_todos
|
|
||||||
|
|
||||||
# Open a todo and return its entries as a list
|
|
||||||
def get_todo(self, member_id: str, todo_list_name: str) -> list | None:
|
|
||||||
member_todos = self.get_todos(member_id)
|
|
||||||
member_todo = member_todos.get(todo_list_name)
|
|
||||||
return member_todo
|
|
||||||
|
|
||||||
# Write todos as dict to the member's todos file, IT ASSUMES THAT YOU CALLED verify_todos_file BEFORE !
|
|
||||||
def write_todos(self, member_id: str, member_todos: dict) -> None:
|
|
||||||
member_todos_path = os.path.join(self.todos_dir, f"user_{member_id}_todos.json")
|
|
||||||
|
|
||||||
with open(member_todos_path, 'w') as member_todos_file:
|
|
||||||
json.dump(member_todos, member_todos_file, indent=4)
|
|
||||||
|
|
||||||
# Create a new todo if possible, return 0 if success, -1 if todo already exists
|
|
||||||
def create_todo(self, member_id: str, todo_list_name: str) -> int:
|
|
||||||
self.verify_todos_file(member_id)
|
|
||||||
|
|
||||||
member_todos = self.get_todos(member_id)
|
|
||||||
if member_todos.get(todo_list_name): return -1
|
|
||||||
|
|
||||||
member_todos[todo_list_name] = []
|
|
||||||
self.write_todos(member_id, member_todos)
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Remove a todo if possible, return 0 if success, -1 if todo list doesn't exists
|
|
||||||
def remove_todo(self, member_id: str, todo_list_name: str) -> int:
|
|
||||||
self.verify_todos_file(member_id)
|
|
||||||
|
|
||||||
member_todos = self.get_todos(member_id)
|
|
||||||
pop_result = member_todos.pop(todo_list_name, None)
|
|
||||||
|
|
||||||
self.write_todos(member_id, member_todos)
|
|
||||||
|
|
||||||
if pop_result:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Add an entry to member's selected todo list, return 0 if successs, -1 if entry_content is empty, -2 if todo list doesn't exists or -3 if entry is already in todo list
|
|
||||||
def add_todo_entry(self, member_id: str, todo_list_name: str, entry_content: str) -> int:
|
|
||||||
self.verify_todos_file(member_id)
|
|
||||||
|
|
||||||
if not entry_content or not entry_content.strip():
|
|
||||||
return -1
|
|
||||||
|
|
||||||
member_todos = self.get_todos(member_id)
|
|
||||||
member_todo = member_todos.get(todo_list_name)
|
|
||||||
if not member_todo:
|
|
||||||
return -2
|
|
||||||
|
|
||||||
if entry_content in member_todo:
|
|
||||||
return -3
|
|
||||||
|
|
||||||
member_todo.append(entry_content)
|
|
||||||
self.write_todos(member_id, member_todos)
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# Remove an entry to member's selected todo list, return 0 if success, -1 if entry doesn't exist or -2 if todo list doesn't exists
|
|
||||||
def remove_todo_entry(self, member_id: str, todo_list_name: str, entry_content: str) -> int:
|
|
||||||
self.verify_todos_file(member_id)
|
|
||||||
|
|
||||||
if not entry_content or not entry_content.strip():
|
|
||||||
return -1
|
|
||||||
|
|
||||||
member_todos = self.get_todos(member_id)
|
|
||||||
member_todo = member_todos.get(todo_list_name)
|
|
||||||
if not member_todo:
|
|
||||||
return -2
|
|
||||||
|
|
||||||
if entry_content in member_todo:
|
|
||||||
member_todo.remove(entry_content)
|
|
||||||
self.write_todos(member_id, member_todos)
|
|
||||||
|
|
||||||
return 0
|
|
Loading…
Reference in New Issue