refactor: minor fixes everywhere

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

View File

@ -38,9 +38,9 @@ def range_type(string, min=0, max=100):
try: try:
value = int(string) value = int(string)
except ValueError: except ValueError:
raise argparse.ArgumentTypeError(f"value not a valid integer") raise argparse.ArgumentTypeError("value is not a valid integer")
if min <= value <= max: if min <= value <= max:
return value return value
else: 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 . 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__(): def __reload_module__():

View File

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

View File

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

View File

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

18
core.py
View File

@ -7,13 +7,13 @@ import textwrap
import time import time
import traceback import traceback
import disnake
import disnake_paginator import disnake_paginator
import commands import commands
import constants import constants
import core
import utils 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): async def on_message(message, edited=False):
@ -27,8 +27,10 @@ async def on_message(message, edited=False):
if not matched: if not matched:
return return
global last_used idle_tracker["last_used"] = time.time()
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: if len(matched) > 1:
await utils.reply( await utils.reply(
@ -85,7 +87,7 @@ async def on_message(message, edited=False):
if len(output) > 2000: if len(output) > 2000:
output = output.replace("`", "\\`") output = output.replace("`", "\\`")
await disnake_paginator.ButtonPaginator( await disnake_paginator.ButtonPaginator(
prefix=f"```\n", prefix="```\n",
suffix="```", suffix="```",
invalid_user_function=utils.invalid_user_handler, invalid_user_function=utils.invalid_user_handler,
color=constants.EMBED_COLOR, color=constants.EMBED_COLOR,
@ -129,9 +131,9 @@ async def on_message(message, edited=False):
async def on_voice_state_update(_, before, after): async def on_voice_state_update(_, before, after):
is_empty = lambda channel: [m.id for m in (channel.members if channel else [])] == [ def is_empty(channel):
client.user.id return [m.id for m in (channel.members if channel else [])] == [client.user.id]
]
c = None c = None
if is_empty(before.channel): if is_empty(before.channel):
c = before.channel c = before.channel

View File

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

View File

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

View File

@ -20,14 +20,13 @@ class LimitedSizeDict(collections.OrderedDict):
self.popitem(last=False) self.popitem(last=False)
command_locks = LimitedSizeDict()
message_responses = LimitedSizeDict()
players = {}
intents = disnake.Intents.default() intents = disnake.Intents.default()
intents.message_content = True intents.message_content = True
intents.members = True intents.members = True
client = disnake.Client(intents=intents) 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() start_time = time.time()

View File

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

View File

@ -5,7 +5,9 @@ from state import message_responses
def format_duration(duration: int): 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 = [] segments = []
weeks, duration = divmod(duration, 604800) weeks, duration = divmod(duration, 604800)