refactor(tools): clean up and use proper argument parser
This commit is contained in:
parent
47955cdfd5
commit
2060e25f75
@ -9,7 +9,7 @@ from .utils import *
|
|||||||
|
|
||||||
def __reload_module__():
|
def __reload_module__():
|
||||||
for name, module in globals().items():
|
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)
|
importlib.reload(module)
|
||||||
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
||||||
reloaded_modules.add(name)
|
reloaded_modules.add(name)
|
||||||
|
@ -1,23 +1,77 @@
|
|||||||
|
import importlib
|
||||||
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import arguments
|
||||||
import commands
|
import commands
|
||||||
|
import constants
|
||||||
|
from state import reloaded_modules
|
||||||
|
|
||||||
|
|
||||||
async def clear(message):
|
async def clear(message):
|
||||||
tokens = commands.tokenize(message.content)[1:]
|
tokens = commands.tokenize(message.content)
|
||||||
if len(tokens) < 2:
|
parser = arguments.ArgumentParser(
|
||||||
await message.reply("no count and/or regex supplied!", mention_author=False)
|
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
|
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(
|
message_count = len(
|
||||||
await message.channel.purge(
|
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:
|
try:
|
||||||
await message.reply(
|
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,
|
mention_author=False,
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
pass
|
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__()
|
||||||
|
@ -3,6 +3,7 @@ import inspect
|
|||||||
|
|
||||||
import arguments
|
import arguments
|
||||||
import commands
|
import commands
|
||||||
|
import constants
|
||||||
import utils
|
import utils
|
||||||
import ytdlp
|
import ytdlp
|
||||||
from state import client, playback_queue, reloaded_modules
|
from state import client, playback_queue, reloaded_modules
|
||||||
@ -227,7 +228,10 @@ def generate_queue_list(queue: list):
|
|||||||
|
|
||||||
def __reload_module__():
|
def __reload_module__():
|
||||||
for name, module in globals().items():
|
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)
|
importlib.reload(module)
|
||||||
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
||||||
reloaded_modules.add(name)
|
reloaded_modules.add(name)
|
||||||
|
@ -3,8 +3,9 @@ import os
|
|||||||
EMBED_COLOR = 0xFF6600
|
EMBED_COLOR = 0xFF6600
|
||||||
OWNERS = [531392146767347712]
|
OWNERS = [531392146767347712]
|
||||||
PREFIX = "%"
|
PREFIX = "%"
|
||||||
|
RELOAD_BLACKLISTED_MODULES = ["re", "argparse"]
|
||||||
|
|
||||||
ytdl_format_options = {
|
YTDL_OPTIONS = {
|
||||||
"default_search": "auto",
|
"default_search": "auto",
|
||||||
"format": "bestaudio/best",
|
"format": "bestaudio/best",
|
||||||
"ignoreerrors": False,
|
"ignoreerrors": False,
|
||||||
@ -19,6 +20,6 @@ ytdl_format_options = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
secrets = {
|
SECRETS = {
|
||||||
"TOKEN": os.getenv("BOT_TOKEN"),
|
"TOKEN": os.getenv("BOT_TOKEN"),
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ async def on_message(message):
|
|||||||
|
|
||||||
def __reload_module__():
|
def __reload_module__():
|
||||||
for name, module in globals().items():
|
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)
|
importlib.reload(module)
|
||||||
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
||||||
reloaded_modules.add(name)
|
reloaded_modules.add(name)
|
||||||
|
4
main.py
4
main.py
@ -36,7 +36,7 @@ async def on_message(message):
|
|||||||
commands.Command.RELOAD
|
commands.Command.RELOAD
|
||||||
]:
|
]:
|
||||||
for name, module in globals().items():
|
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)
|
importlib.reload(module)
|
||||||
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
||||||
reloaded_modules.add(name)
|
reloaded_modules.add(name)
|
||||||
@ -48,4 +48,4 @@ async def on_message(message):
|
|||||||
await events.on_message(message)
|
await events.on_message(message)
|
||||||
|
|
||||||
|
|
||||||
client.run(constants.secrets["TOKEN"])
|
client.run(constants.SECRETS["TOKEN"])
|
||||||
|
2
utils.py
2
utils.py
@ -14,7 +14,7 @@ async def invalid_user_handler(interaction):
|
|||||||
|
|
||||||
|
|
||||||
def filter_secrets(text: str) -> str:
|
def filter_secrets(text: str) -> str:
|
||||||
for secret_name, secret in constants.secrets.items():
|
for secret_name, secret in constants.SECRETS.items():
|
||||||
if not secret:
|
if not secret:
|
||||||
continue
|
continue
|
||||||
text = text.replace(secret, f"<{secret_name}>")
|
text = text.replace(secret, f"<{secret_name}>")
|
||||||
|
6
ytdlp.py
6
ytdlp.py
@ -9,7 +9,7 @@ import yt_dlp
|
|||||||
import constants
|
import constants
|
||||||
from state import reloaded_modules
|
from state import reloaded_modules
|
||||||
|
|
||||||
ytdl = yt_dlp.YoutubeDL(constants.ytdl_format_options)
|
ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS)
|
||||||
|
|
||||||
|
|
||||||
class YTDLSource(disnake.PCMVolumeTransformer):
|
class YTDLSource(disnake.PCMVolumeTransformer):
|
||||||
@ -45,11 +45,11 @@ class YTDLSource(disnake.PCMVolumeTransformer):
|
|||||||
|
|
||||||
def __reload_module__():
|
def __reload_module__():
|
||||||
for name, module in globals().items():
|
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)
|
importlib.reload(module)
|
||||||
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
if "__reload_module__" in dir(module) and name not in reloaded_modules:
|
||||||
reloaded_modules.add(name)
|
reloaded_modules.add(name)
|
||||||
module.__reload_module__()
|
module.__reload_module__()
|
||||||
|
|
||||||
global ytdl
|
global ytdl
|
||||||
ytdl = yt_dlp.YoutubeDL(constants.ytdl_format_options)
|
ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user