This commit is contained in:
artie 2024-03-02 12:16:06 +01:00
parent 5028ebadec
commit b84cedad3b
5 changed files with 31 additions and 31 deletions

44
bot.py
View File

@ -1,14 +1,14 @@
import asyncio import asyncio
import contextlib import contextlib
from json import JSONDecodeError
import logging import logging
import os import os
from pathlib import Path
import sys import sys
import time import time
import traceback import traceback
from functools import cached_property from functools import cached_property
from pkgutil import iter_modules from pkgutil import iter_modules
from typing import Optional, TypedDict from typing import Optional
import aiohttp import aiohttp
import discord import discord
@ -22,15 +22,11 @@ from utils import reddit
from utils.api import API from utils.api import API
from utils.catbox import Catbox, Litterbox from utils.catbox import Catbox, Litterbox
from utils.common import read_json, ArtemisError from utils.common import read_json, ArtemisError
from utils.constants import TEMP_DIR
from utils.unogs import uNoGS from utils.unogs import uNoGS
from utils import config from utils import config
class Status(TypedDict):
name: str
emoji: str
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format="{levelname} - {name}: {message}", format="{levelname} - {name}: {message}",
@ -42,30 +38,32 @@ log = logging.getLogger("artemis")
logging.getLogger("discord").setLevel(logging.WARNING) logging.getLogger("discord").setLevel(logging.WARNING)
logging.getLogger("aiocache").setLevel(logging.ERROR) 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): class Artemis(commands.Bot):
session: aiohttp.ClientSession session: aiohttp.ClientSession
httpx_session: httpx.AsyncClient httpx_session: httpx.AsyncClient
def __init__(self): 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__( super().__init__(
command_prefix=commands.when_mentioned_or(config.prefix), command_prefix=commands.when_mentioned_or(config.prefix),
help_command=HelpEmbedded(command_attrs={"hidden": True}, verify_checks=False), help_command=HelpEmbedded(command_attrs={"hidden": True}, verify_checks=False),
intents=intents, intents=intents,
allowed_mentions=allowed_mentions, allowed_mentions=discord.AllowedMentions(everyone=False, replied_user=False),
owner_id=134306884617371648, owner_id=134306884617371648,
activity=discord.CustomActivity(name=status["name"], emoji=status["emoji"]), activity=discord.CustomActivity(name=status["name"], emoji=status["emoji"]),
) )
@ -83,7 +81,7 @@ class Artemis(commands.Bot):
self.invisible = discord.Colour(0x2F3136) self.invisible = discord.Colour(0x2F3136)
async def maybe_send_restarted(self): async def maybe_send_restarted(self):
restart = Path("data/temp/restart") restart = TEMP_DIR / "restart"
if restart.exists(): if restart.exists():
chid, _, mid = restart.read_text().partition("-") chid, _, mid = restart.read_text().partition("-")
restart.unlink() restart.unlink()
@ -257,6 +255,8 @@ class HelpEmbedded(commands.MinimalHelpCommand):
async def main(): async def main():
TEMP_DIR.mkdir(exist_ok=True)
async with Artemis() as bot: async with Artemis() as bot:
await bot.start(config.token) await bot.start(config.token)

View File

@ -92,7 +92,7 @@ class Events(commands.Cog):
return return
content = message.content.lower() 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}") cmd_log.debug(f"{message.author.id}: {message.content}")
await self.handle_triggers(message, content) await self.handle_triggers(message, content)

View File

@ -23,7 +23,7 @@ from yt_dlp.utils import parse_duration
import utils import utils
from utils.common import ArtemisError 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.catbox import CatboxError
from utils.flags import DLFlags from utils.flags import DLFlags
from utils.iso_639 import get_language_name from utils.iso_639 import get_language_name
@ -32,7 +32,6 @@ from utils.views import DropdownView
if TYPE_CHECKING: if TYPE_CHECKING:
from bot import Artemis from bot import Artemis
TEMP_DIR = Path("data/temp/")
yt_dlp.utils.bug_reports_message = lambda: "" yt_dlp.utils.bug_reports_message = lambda: ""
DEFAULT_OPTS = { DEFAULT_OPTS = {
@ -404,12 +403,11 @@ class Media(commands.Cog):
async def monitor_download(): async def monitor_download():
nonlocal msg, state nonlocal msg, state
path = Path("./data/temp/")
while not finished: while not finished:
content = "Processing..." content = "Processing..."
if state == "downloading": if state == "downloading":
match = None match = None
files = list(path.iterdir()) files = list(TEMP_DIR.iterdir())
if files: if files:
match = max(files, key=lambda f: f.stat().st_size) match = max(files, key=lambda f: f.stat().st_size)
if match: if match:

View File

@ -15,6 +15,7 @@ import pendulum
import utils import utils
from utils.common import ArtemisError from utils.common import ArtemisError
from utils.constants import TEMP_DIR
from utils.views import BaseView from utils.views import BaseView
if TYPE_CHECKING: if TYPE_CHECKING:
@ -76,8 +77,7 @@ class Owner(commands.Cog, command_attrs={"hidden": True}):
@commands.is_owner() @commands.is_owner()
async def restart(self, ctx: commands.Context): async def restart(self, ctx: commands.Context):
await ctx.message.add_reaction("🔄") await ctx.message.add_reaction("🔄")
with open("data/temp/restart", "w") as f: (TEMP_DIR / "restart").write_text(f"{ctx.channel.id}-{ctx.message.id}")
f.write(f"{ctx.channel.id}-{ctx.message.id}")
await self.bot.close() await self.bot.close()
@commands.command(aliases=["u"]) @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): async def on_restart(self, interaction: discord.Interaction, button):
await interaction.response.edit_message(view=None) await interaction.response.edit_message(view=None)
await self.message.add_reaction("🔄") await self.message.add_reaction("🔄")
with open("data/temp/restart", "w") as f: (TEMP_DIR / "restart").write_text(f"{self.message.channel.id}-{self.message.id}")
f.write(f"{self.message.channel.id}-{self.message.id}")
await self.ctx.bot.close() await self.ctx.bot.close()
async def on_timeout(self): async def on_timeout(self):

View File

@ -1,3 +1,4 @@
from pathlib import Path
from utils.common import read_json from utils.common import read_json
MAX_DISCORD_SIZE = 25 * 1024**2 MAX_DISCORD_SIZE = 25 * 1024**2
@ -5,6 +6,8 @@ MAX_API_SIZE = 200 * 1024**2
MAX_CATBOX_SIZE = 200 * 1024**2 MAX_CATBOX_SIZE = 200 * 1024**2
MAX_LITTERBOX_SIZE = 1024**3 MAX_LITTERBOX_SIZE = 1024**3
TEMP_DIR = Path("data/temp/")
WIKT_LANGUAGES = read_json("data/wiktionary-languages.json") WIKT_LANGUAGES = read_json("data/wiktionary-languages.json")
TEEHEE_EMOJIS = [ TEEHEE_EMOJIS = [