refactor(tools): clean up and use proper argument parser

This commit is contained in:
2024-12-30 03:31:49 -05:00
parent 47955cdfd5
commit 2060e25f75
8 changed files with 75 additions and 16 deletions

View File

@@ -9,7 +9,7 @@ from .utils import *
def __reload_module__():
for name, module in globals().items():
if inspect.ismodule(module):
if inspect.ismodule(module) and name not in constants.RELOAD_BLACKLISTED_MODULES:
importlib.reload(module)
if "__reload_module__" in dir(module) and name not in reloaded_modules:
reloaded_modules.add(name)

View File

@@ -1,23 +1,77 @@
import importlib
import inspect
import re
import arguments
import commands
import constants
from state import reloaded_modules
async def clear(message):
tokens = commands.tokenize(message.content)[1:]
if len(tokens) < 2:
await message.reply("no count and/or regex supplied!", mention_author=False)
tokens = commands.tokenize(message.content)
parser = arguments.ArgumentParser(
tokens[0],
"bulk delete messages in the current channel matching certain criteria",
)
parser.add_argument(
"count",
type=int,
choices=range(1, 1001),
metavar="[1-1000]",
help="amount of messages to delete",
)
parser.add_argument(
"-r",
"--regex",
required=False,
help="delete messages with content matching this regex",
)
parser.add_argument(
"-i",
"--author-id",
type=int,
action="append",
help="delete messages whose author matches this id",
)
parser.add_argument(
"-o",
"--oldest-first",
action="store_true",
help="delete oldest messages first",
)
if not (args := await parser.parse_args(message, tokens)):
return
def check(m):
c = []
if r := args.regex:
c.append(re.match(r, m.content))
if i := args.author_id:
c.append(m.author.id in i)
return all(c)
message_count = len(
await message.channel.purge(
limit=int(tokens[0]), check=lambda m: re.match(tokens[1], m.content)
limit=args.count, check=check, oldest_first=args.oldest_first
)
)
try:
await message.reply(
f"successfully purged **{message_count} {'message' if message_count == 1 else 'messages'}**",
f"purged **{message_count} {'message' if message_count == 1 else 'messages'}**",
mention_author=False,
)
except:
pass
def __reload_module__():
for name, module in globals().items():
if (
inspect.ismodule(module)
and name not in constants.RELOAD_BLACKLISTED_MODULES
):
importlib.reload(module)
if "__reload_module__" in dir(module) and name not in reloaded_modules:
reloaded_modules.add(name)
module.__reload_module__()

View File

@@ -3,6 +3,7 @@ import inspect
import arguments
import commands
import constants
import utils
import ytdlp
from state import client, playback_queue, reloaded_modules
@@ -227,7 +228,10 @@ def generate_queue_list(queue: list):
def __reload_module__():
for name, module in globals().items():
if inspect.ismodule(module):
if (
inspect.ismodule(module)
and name not in constants.RELOAD_BLACKLISTED_MODULES
):
importlib.reload(module)
if "__reload_module__" in dir(module) and name not in reloaded_modules:
reloaded_modules.add(name)