refactor: minor fixes everywhere

This commit is contained in:
Ryan 2025-01-05 23:43:08 -05:00
parent ebde4f1310
commit 565dbb6f47
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
11 changed files with 44 additions and 47 deletions

View File

@ -38,9 +38,9 @@ def range_type(string, min=0, max=100):
try:
value = int(string)
except ValueError:
raise argparse.ArgumentTypeError(f"value not a valid integer")
raise argparse.ArgumentTypeError("value is not a valid integer")
if min <= value <= max:
return value
else:
raise argparse.ArgumentTypeError(f"value not in range {min}-{max}")
raise argparse.ArgumentTypeError(f"value is not in range {min}-{max}")

View File

@ -1,5 +1,16 @@
from . import bot, tools, utils, voice
from .utils import *
from .utils import Command, match, match_token, tokenize
__all__ = [
"bot",
"tools",
"utils",
"voice",
"Command",
"match",
"match_token",
"tokenize",
]
def __reload_module__():

View File

@ -1,7 +1,6 @@
import re
import arguments
import commands
import utils
@ -67,7 +66,7 @@ async def clear(message):
if args.delete_command:
try:
await message.delete()
except:
except Exception:
pass
regex = None
@ -101,5 +100,5 @@ async def clear(message):
message,
f"purged **{messages}/{args.count} {'message' if args.count == 1 else 'messages'}**",
)
except:
except Exception:
pass

View File

@ -299,9 +299,6 @@ async def volume(message):
if not command_allowed(message):
return
if not message.guild.voice_client:
return
tokens = commands.tokenize(message.content)
parser = arguments.ArgumentParser(tokens[0], "set the current volume level")
parser.add_argument(
@ -314,10 +311,7 @@ async def volume(message):
return
if not message.guild.voice_client.source:
await utils.reply(
message,
f"nothing is playing!",
)
await utils.reply(message, "nothing is playing!")
return
if args.volume is None:

View File

@ -13,6 +13,7 @@ RELOADABLE_MODULES = [
"constants",
"core",
"events",
"tasks",
"utils",
"voice",
"youtubedl",

20
core.py
View File

@ -7,13 +7,13 @@ import textwrap
import time
import traceback
import disnake
import disnake_paginator
import commands
import constants
import core
import utils
from state import client, command_locks, last_used
from state import client, command_locks, idle_tracker
async def on_message(message, edited=False):
@ -27,8 +27,10 @@ async def on_message(message, edited=False):
if not matched:
return
global last_used
last_used = time.time()
idle_tracker["last_used"] = time.time()
if idle_tracker["is_idle"]:
idle_tracker["is_idle"] = False
await client.change_presence(status=disnake.Status.online)
if len(matched) > 1:
await utils.reply(
@ -50,7 +52,7 @@ async def on_message(message, edited=False):
and v.__name__ in constants.RELOADABLE_MODULES,
globals().values(),
):
core.rreload(reloaded_modules, module)
rreload(reloaded_modules, module)
await utils.add_check_reaction(message)
case C.EXECUTE if message.author.id in constants.OWNERS:
@ -85,7 +87,7 @@ async def on_message(message, edited=False):
if len(output) > 2000:
output = output.replace("`", "\\`")
await disnake_paginator.ButtonPaginator(
prefix=f"```\n",
prefix="```\n",
suffix="```",
invalid_user_function=utils.invalid_user_handler,
color=constants.EMBED_COLOR,
@ -129,9 +131,9 @@ async def on_message(message, edited=False):
async def on_voice_state_update(_, before, after):
is_empty = lambda channel: [m.id for m in (channel.members if channel else [])] == [
client.user.id
]
def is_empty(channel):
return [m.id for m in (channel.members if channel else [])] == [client.user.id]
c = None
if is_empty(before.channel):
c = before.channel

View File

@ -17,15 +17,6 @@ async def on_ready():
),
).start()
threading.Thread(
name="check_idle",
target=asyncio.run_coroutine_threadsafe,
args=(
tasks.check_idle(),
client.loop,
),
).start()
async def on_message(message):
await core.on_message(message)

View File

@ -12,4 +12,5 @@ async def on_ready():
await events.on_ready()
if __name__ == "__main__":
client.run(constants.SECRETS["TOKEN"])

View File

@ -20,14 +20,13 @@ class LimitedSizeDict(collections.OrderedDict):
self.popitem(last=False)
command_locks = LimitedSizeDict()
message_responses = LimitedSizeDict()
players = {}
intents = disnake.Intents.default()
intents.message_content = True
intents.members = True
client = disnake.Client(intents=intents)
last_used = time.time()
command_locks = LimitedSizeDict()
idle_tracker = {"is_idle": False, "last_used": time.time()}
message_responses = LimitedSizeDict()
players = {}
start_time = time.time()

View File

@ -3,7 +3,7 @@ import time
import disnake
from state import client, last_used, players
from state import client, idle_tracker, players
async def cleanup():
@ -17,12 +17,9 @@ async def cleanup():
for target in targets:
del players[target]
async def check_idle():
while True:
await asyncio.sleep(3600)
if time.time() - last_used >= 3600:
if (
not idle_tracker["is_idle"]
and time.time() - idle_tracker["last_used"] >= 3600
):
await client.change_presence(status=disnake.Status.idle)
else:
await client.change_presence(status=disnake.Status.online)
idle_tracker["is_idle"] = True

View File

@ -5,7 +5,9 @@ from state import message_responses
def format_duration(duration: int):
format_plural = lambda noun, count: noun if count == 1 else noun + "s"
def format_plural(noun, count):
return noun if count == 1 else noun + "s"
segments = []
weeks, duration = divmod(duration, 604800)