diff --git a/bot.py b/bot.py index 4eb68fd..ed269d4 100644 --- a/bot.py +++ b/bot.py @@ -1,14 +1,14 @@ import asyncio import contextlib +from json import JSONDecodeError import logging import os -from pathlib import Path import sys import time import traceback from functools import cached_property from pkgutil import iter_modules -from typing import Optional, TypedDict +from typing import Optional import aiohttp import discord @@ -22,15 +22,11 @@ from utils import reddit from utils.api import API from utils.catbox import Catbox, Litterbox from utils.common import read_json, ArtemisError +from utils.constants import TEMP_DIR from utils.unogs import uNoGS from utils import config -class Status(TypedDict): - name: str - emoji: str - - logging.basicConfig( level=logging.INFO, format="{levelname} - {name}: {message}", @@ -42,30 +38,32 @@ log = logging.getLogger("artemis") logging.getLogger("discord").setLevel(logging.WARNING) logging.getLogger("aiocache").setLevel(logging.ERROR) -intents = discord.Intents( - messages=True, - message_content=True, - guilds=True, - members=True, - emojis=True, - reactions=True, - voice_states=True, -) -allowed_mentions = discord.AllowedMentions(everyone=False, replied_user=False) - -status: Status = read_json("data/status.json") - class Artemis(commands.Bot): session: aiohttp.ClientSession httpx_session: httpx.AsyncClient def __init__(self): + intents = discord.Intents( + messages=True, + message_content=True, + guilds=True, + members=True, + emojis=True, + reactions=True, + voice_states=True, + ) + + try: + status = read_json("data/status.json") + except (JSONDecodeError, FileNotFoundError): + status = {"name": None, "emoji": None} + super().__init__( command_prefix=commands.when_mentioned_or(config.prefix), help_command=HelpEmbedded(command_attrs={"hidden": True}, verify_checks=False), intents=intents, - allowed_mentions=allowed_mentions, + allowed_mentions=discord.AllowedMentions(everyone=False, replied_user=False), owner_id=134306884617371648, activity=discord.CustomActivity(name=status["name"], emoji=status["emoji"]), ) @@ -83,7 +81,7 @@ class Artemis(commands.Bot): self.invisible = discord.Colour(0x2F3136) async def maybe_send_restarted(self): - restart = Path("data/temp/restart") + restart = TEMP_DIR / "restart" if restart.exists(): chid, _, mid = restart.read_text().partition("-") restart.unlink() @@ -257,6 +255,8 @@ class HelpEmbedded(commands.MinimalHelpCommand): async def main(): + TEMP_DIR.mkdir(exist_ok=True) + async with Artemis() as bot: await bot.start(config.token) diff --git a/cogs/events.py b/cogs/events.py index 80ae130..a13c394 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -92,7 +92,7 @@ class Events(commands.Cog): return content = message.content.lower() - if content.startswith(("$")) and "$jsk" not in content: + if content.startswith(config.prefix) and f"{config.prefix}jsk" not in content: cmd_log.debug(f"{message.author.id}: {message.content}") await self.handle_triggers(message, content) diff --git a/cogs/media.py b/cogs/media.py index dadaa53..b4c57a0 100644 --- a/cogs/media.py +++ b/cogs/media.py @@ -23,7 +23,7 @@ from yt_dlp.utils import parse_duration import utils from utils.common import ArtemisError -from utils.constants import MAX_DISCORD_SIZE, MAX_LITTERBOX_SIZE +from utils.constants import MAX_DISCORD_SIZE, MAX_LITTERBOX_SIZE, TEMP_DIR from utils.catbox import CatboxError from utils.flags import DLFlags from utils.iso_639 import get_language_name @@ -32,7 +32,6 @@ from utils.views import DropdownView if TYPE_CHECKING: from bot import Artemis -TEMP_DIR = Path("data/temp/") yt_dlp.utils.bug_reports_message = lambda: "" DEFAULT_OPTS = { @@ -404,12 +403,11 @@ class Media(commands.Cog): async def monitor_download(): nonlocal msg, state - path = Path("./data/temp/") while not finished: content = "Processing..." if state == "downloading": match = None - files = list(path.iterdir()) + files = list(TEMP_DIR.iterdir()) if files: match = max(files, key=lambda f: f.stat().st_size) if match: diff --git a/cogs/owner.py b/cogs/owner.py index 3bc9db4..2841773 100644 --- a/cogs/owner.py +++ b/cogs/owner.py @@ -15,6 +15,7 @@ import pendulum import utils from utils.common import ArtemisError +from utils.constants import TEMP_DIR from utils.views import BaseView if TYPE_CHECKING: @@ -76,8 +77,7 @@ class Owner(commands.Cog, command_attrs={"hidden": True}): @commands.is_owner() async def restart(self, ctx: commands.Context): await ctx.message.add_reaction("🔄") - with open("data/temp/restart", "w") as f: - f.write(f"{ctx.channel.id}-{ctx.message.id}") + (TEMP_DIR / "restart").write_text(f"{ctx.channel.id}-{ctx.message.id}") await self.bot.close() @commands.command(aliases=["u"]) @@ -93,8 +93,7 @@ class Owner(commands.Cog, command_attrs={"hidden": True}): async def on_restart(self, interaction: discord.Interaction, button): await interaction.response.edit_message(view=None) await self.message.add_reaction("🔄") - with open("data/temp/restart", "w") as f: - f.write(f"{self.message.channel.id}-{self.message.id}") + (TEMP_DIR / "restart").write_text(f"{self.message.channel.id}-{self.message.id}") await self.ctx.bot.close() async def on_timeout(self): diff --git a/utils/constants.py b/utils/constants.py index b14a09f..46c0ae1 100644 --- a/utils/constants.py +++ b/utils/constants.py @@ -1,3 +1,4 @@ +from pathlib import Path from utils.common import read_json MAX_DISCORD_SIZE = 25 * 1024**2 @@ -5,6 +6,8 @@ MAX_API_SIZE = 200 * 1024**2 MAX_CATBOX_SIZE = 200 * 1024**2 MAX_LITTERBOX_SIZE = 1024**3 +TEMP_DIR = Path("data/temp/") + WIKT_LANGUAGES = read_json("data/wiktionary-languages.json") TEEHEE_EMOJIS = [