mostly working idfk
This commit is contained in:
parent
b0ba03f97d
commit
4e111b410d
12 changed files with 822 additions and 291 deletions
37
modules/permission.py
Normal file
37
modules/permission.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
from functools import wraps
|
||||
import discord
|
||||
|
||||
import discord.ext
|
||||
import discord.ext.commands
|
||||
|
||||
from modules.settings import Settings as SettingsManager
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger("goober")
|
||||
|
||||
settings_manager = SettingsManager()
|
||||
settings = settings_manager.settings
|
||||
|
||||
|
||||
class PermissionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def requires_admin():
|
||||
async def wrapper(ctx: discord.ext.commands.Context):
|
||||
if ctx.author.id not in settings["bot"]["owner_ids"]:
|
||||
await ctx.send(
|
||||
"You don't have the necessary permissions to run this command!"
|
||||
)
|
||||
return False
|
||||
|
||||
command = ctx.command
|
||||
if not command:
|
||||
logger.info(f"Unknown command ran {ctx.message}")
|
||||
else:
|
||||
logger.info(
|
||||
f'Command {settings["bot"]["prefix"]}{command.name} @{ctx.author.name}'
|
||||
)
|
||||
return True
|
||||
|
||||
return discord.ext.commands.check(wrapper)
|
|
@ -37,29 +37,27 @@ def is_positive(sentence):
|
|||
return sentiment_score > 0.6 # had to raise the bar because it kept saying "death to jews" was fine and it kept reacting to them
|
||||
|
||||
async def send_message(ctx, message=None, embed=None, file=None, edit=False, message_reference=None):
|
||||
if edit and message_reference:
|
||||
try:
|
||||
try:
|
||||
if edit and message_reference:
|
||||
await message_reference.edit(content=message, embed=embed)
|
||||
except Exception as e:
|
||||
await ctx.send(f"{RED}{(_('edit_fail'))} {e}{RESET}")
|
||||
else:
|
||||
return message_reference
|
||||
|
||||
send_kwargs = {}
|
||||
if message:
|
||||
send_kwargs['content'] = message
|
||||
if embed:
|
||||
send_kwargs['embed'] = embed
|
||||
if file:
|
||||
send_kwargs['file'] = file
|
||||
|
||||
if hasattr(ctx, "respond"):
|
||||
sent_message = None
|
||||
if embed:
|
||||
sent_message = await ctx.respond(embed=embed, ephemeral=False)
|
||||
elif message:
|
||||
sent_message = await ctx.respond(message, ephemeral=False)
|
||||
if file:
|
||||
sent_message = await ctx.respond(file=file, ephemeral=False)
|
||||
return await ctx.respond(**send_kwargs, ephemeral=False)
|
||||
else:
|
||||
sent_message = None
|
||||
if embed:
|
||||
sent_message = await ctx.send(embed=embed)
|
||||
elif message:
|
||||
sent_message = await ctx.send(message)
|
||||
if file:
|
||||
sent_message = await ctx.send(file=file)
|
||||
return sent_message
|
||||
return await ctx.send(**send_kwargs)
|
||||
|
||||
except Exception as e:
|
||||
await ctx.send(f"{RED}{(_('edit_fail'))} {e}{RESET}")
|
||||
|
||||
|
||||
def preprocess_message(message):
|
||||
message = message
|
||||
|
|
58
modules/settings.py
Normal file
58
modules/settings.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import json
|
||||
import os
|
||||
from typing import List, TypedDict
|
||||
import copy
|
||||
|
||||
class MiscBotOptions(TypedDict):
|
||||
ping_line: str
|
||||
active_song: str
|
||||
positive_gifs: List[str]
|
||||
block_profanity: bool
|
||||
|
||||
class BotSettings(TypedDict):
|
||||
prefix: str
|
||||
owner_ids: List[int]
|
||||
blacklisted_users: List[int]
|
||||
user_training: bool
|
||||
allow_show_mem_command: bool
|
||||
react_to_messages: bool
|
||||
misc: MiscBotOptions
|
||||
enabled_cogs: List[str]
|
||||
active_memory: str
|
||||
|
||||
class SettingsType(TypedDict):
|
||||
bot: BotSettings
|
||||
locale: str
|
||||
name: str
|
||||
auto_update: bool
|
||||
disable_checks: bool
|
||||
splash_text_loc: str
|
||||
|
||||
class Settings:
|
||||
def __init__(self) -> None:
|
||||
self.path = os.path.join(".", "settings", "settings.json")
|
||||
if not os.path.exists(self.path):
|
||||
raise FileNotFoundError("settings.json file does not exist!")
|
||||
|
||||
with open(self.path, "r") as f:
|
||||
self._kv_store = json.load(f)
|
||||
|
||||
self.settings: SettingsType = self._kv_store # type: ignore
|
||||
self.original_settings = copy.deepcopy(self.settings)
|
||||
|
||||
def get_locale(self) -> str:
|
||||
# Return locale or None if missing
|
||||
return self.settings.get("locale", None)
|
||||
|
||||
def commit(self) -> None:
|
||||
with open(self.path, "w") as f:
|
||||
json.dump(self.settings, f, indent=4)
|
||||
self.original_settings = copy.deepcopy(self.settings)
|
||||
|
||||
def discard(self) -> None:
|
||||
self.settings = copy.deepcopy(self.original_settings)
|
||||
|
||||
# Usage
|
||||
instance = Settings()
|
||||
locale = instance.get_locale()
|
||||
print("Locale:", locale)
|
|
@ -50,12 +50,50 @@ def find_dotenv(start_path: pathlib.Path) -> pathlib.Path | None:
|
|||
current = current.parent
|
||||
return None
|
||||
|
||||
env_path = find_dotenv(pathlib.Path(__file__).parent)
|
||||
if env_path:
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}")
|
||||
def load_settings_json() -> dict | None:
|
||||
start_path = working_dir.resolve()
|
||||
current = start_path.resolve()
|
||||
while current != current.parent:
|
||||
candidate = current / "settings.json"
|
||||
if candidate.exists():
|
||||
try:
|
||||
with open(candidate, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
print(f"[VOLTA] {GREEN}Loaded settings.json locale '{data.get('locale')}' from {candidate}{RESET}")
|
||||
return data
|
||||
except Exception as e:
|
||||
print(f"[VOLTA] {RED}Failed to load settings.json at {candidate}: {e}{RESET}")
|
||||
return None
|
||||
current = current.parent
|
||||
|
||||
for root, dirs, files in os.walk(start_path):
|
||||
if "settings.json" in files:
|
||||
candidate = pathlib.Path(root) / "settings.json"
|
||||
try:
|
||||
with open(candidate, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
print(f"[VOLTA] {GREEN}Loaded settings.json locale '{data.get('locale')}' from {candidate}{RESET}")
|
||||
return data
|
||||
except Exception as e:
|
||||
print(f"[VOLTA] {RED}Failed to load settings.json at {candidate}: {e}{RESET}")
|
||||
return None
|
||||
|
||||
print(f"[VOLTA] {YELLOW}No settings.json found scanning up or down from {start_path}{RESET}")
|
||||
return None
|
||||
|
||||
settings = load_settings_json()
|
||||
if settings and "locale" in settings:
|
||||
LOCALE = settings["locale"]
|
||||
else:
|
||||
print(f"[VOLTA] {YELLOW}No .env file found from {__file__} upwards.{RESET}")
|
||||
env_path = find_dotenv(pathlib.Path(__file__).parent)
|
||||
if env_path:
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}")
|
||||
else:
|
||||
print(f"[VOLTA] {YELLOW}No .env file found from {__file__} upwards.{RESET}")
|
||||
|
||||
LOCALE = os.getenv("LOCALE") or None
|
||||
|
||||
|
||||
locales_dirs.extend(find_locales_dirs(module_dir))
|
||||
if working_dir != module_dir:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue